欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#数据绑定之将datatabel的data添加listView

程序员文章站 2024-02-22 23:44:10
功能:通过响应ui textbox 的值向listview 绑定新添加的纪录。 ui xaml 代码 复制代码 代码如下:...

c#数据绑定之将datatabel的data添加listView

功能:通过响应ui textbox 的值向listview 绑定新添加的纪录。

ui xaml 代码

复制代码 代码如下:

<grid margin="5">
            <grid.columndefinitions>
                <columndefinition width="auto"/>
                <columndefinition/>
            </grid.columndefinitions>
            <grid.rowdefinitions>
                <rowdefinition  height="auto"/>
                <rowdefinition height="auto"/>
                <rowdefinition height="auto"/>
                <rowdefinition height="auto"/>
                <rowdefinition height="auto"/>
                <rowdefinition height="auto"/>
                <rowdefinition/>
            </grid.rowdefinitions>
            <textblock  grid.row="0" grid.column="0" text="商品名称" horizontalalignment="right" verticalalignment="center" />
            <textbox grid.row="0" grid.column="1" margin="2" verticalalignment="center" text="{binding path=comname}" name="tbkname"/>
            <textblock  grid.row="1" grid.column="0" text="商品进货单价" horizontalalignment="right" verticalalignment="center" />
            <textbox grid.row="1" grid.column="1" margin="2" verticalalignment="center" text="{binding path=cominprice}" name="tbkinprice"/>
            <textblock  grid.row="2" grid.column="0" text="商品零售价格" horizontalalignment="right" verticalalignment="center" />
            <textbox grid.row="2" grid.column="1" margin="2" verticalalignment="center" text="{binding path=comoutprice}" name="tbkoutprice"/>
            <textblock  grid.row="3" grid.column="0" text="进货数量" horizontalalignment="right" verticalalignment="center" />
            <textbox grid.row="3" grid.column="1" margin="2" verticalalignment="center" text="{binding path=comoutprice}" name="tbkcomcount"/>

            <button grid.row="4" grid.column="1" content="添加新纪录" margin="2" padding="2" name="btnadd" click="btnadd_click" />

            <textblock grid.row="5" grid.columnspan="2"  horizontalalignment="stretch"  verticalalignment="center"  background="black" />

            <listview name="lstdisplay" itemssource="{binding}" grid.row="6" grid.columnspan="2">
                <listview.view>
                    <gridview>
                        <gridview.columns>
                            <gridviewcolumn  header="autoid" displaymemberbinding="{binding autoid}"/>
                            <gridviewcolumn header="商品名称" displaymemberbinding="{binding comname}"/>
                            <gridviewcolumn header="进货价格" displaymemberbinding="{binding path=cominprice,stringformat={}{0:c}}"/>
                            <gridviewcolumn header="零售价格" displaymemberbinding="{binding path=comoutprice,stringformat={}{0:c}}"/>
                            <gridviewcolumn header="进货数量" displaymemberbinding="{binding comcount}"/>
                        </gridview.columns>
                    </gridview>
                </listview.view>
            </listview>
        </grid>

c#后代码

复制代码 代码如下:

datatable commodity = newtable();

        public mainwindow()
        {
            initializecomponent();
            //datatable commodity = newtable();

        }

      

        private void btnadd_click(object sender, routedeventargs e)
        {

            addnewmember(commodity);
            lstdisplay.datacontext = commodity;

        }

        private void btndelete_click(object sender, routedeventargs e)
        {
            button b = sender as button;
            int32 autoid = convert.toint32(b.commandparameter);
            delmember(commodity, autoid);
            lstdisplay.datacontext = commodity;
            lstdisplay.items.refresh();

        }

        private datatable delmember(datatable table,int32 laction)
        {
            //deleting  a existed row in datatable commodity;
            datarow delrow = table.rows.find(laction);
            table.rows.remove(delrow);
            return table;
        }

        private  datatable addnewmember(datatable table)
        {
           

            //inserting a new row in datatable commodity;

            datarow newrow = table.newrow();
            newrow["comname"] = tbkname.text;
            newrow["cominprice"] = tbkinprice.text;
            newrow["comoutprice"] = tbkoutprice.text;
            newrow["comcount"] = tbkcomcount.text;
            table.rows.add(newrow);
            return table;
        }

 

        private static datatable newtable()
        {
            //create a new table named commodity
            datatable commodity = new datatable("commodity");


            //instert new columns
            datacolumn auto = new datacolumn("autoid", typeof(int32));
            //setting the column auto increment
            auto.autoincrement = true;
            commodity.columns.add(auto);
            commodity.columns.add("comname", typeof(string));
            commodity.columns.add("cominprice", typeof(double));
            commodity.columns.add("comoutprice", typeof(double));
            commodity.columns.add("comcount", typeof(system.int32));
            commodity.primarykey = new datacolumn[] { commodity.columns[0] };
            return commodity;
        }