c#数据绑定之数据转化为信息的示例

这篇文章主要介绍了c#数据绑定中的数据转化为信息的示例,需要的朋友可以参考下

目标界面:

XAML代码:

复制代码 代码如下:


           
               
               
               
               
           

           
               
                   
                       
                       
                       
                   

                   
                       
                       
                   

                   
                   
                   
                   
                   

C# 代码:

复制代码 代码如下:

DataSet business = NewData();

        public MainWindow()
        {
            InitializeComponent();

        }

        private static DataSet NewData()
        {
            //-----build the parent table and add some data
            DataTable customer = new DataTable("Customer");
            customer.Columns.Add("CID", typeof(Int32));
            customer.Columns.Add("Name", typeof(string));
            //-------build the child table and add some data.
            DataTable orders = new DataTable("Order");
            orders.Columns.Add("OID", typeof(int));
            orders.Columns.Add("Customer", typeof(Int32));
            orders.Columns.Add("Subtotal", typeof(decimal));
            orders.Columns.Add("TaxRate", typeof(decimal));
            orders.Columns.Add("Total",typeof(decimal),"Subtotal*(1+TaxRate)");

            //-----Link the table within a Dataset.
            DataSet business = new DataSet();
            business.Tables.Add(customer);
            business.Tables.Add(orders);
            business.Relations.Add(customer.Columns["CID"],orders.Columns["Customer"]);
            customer.Columns.Add("OrderTotals" ,typeof(decimal),"Sum(Child.Total)");
            return business;
        }

        private void btnAddCustomer_Click(object sender, RoutedEventArgs e)
        {
           //Vist datatable customer.
            DataTable customer=business.Tables["Customer"];
            NewMember(customer);
            lstDisplayCustomer.DataContext = customer;
        }

        private DataTable NewMember(DataTable table)
        {
            DataRow newRow = table.NewRow();
            newRow["CID"] = tbxCustomerID.Text;
            newRow["Name"] = tbxCustomerName.Text;
            table.Rows.Add(newRow);
            return table;
        }

        private DataTable NewMemberOrder(DataTable table)
        {
            DataRow newRow = table.NewRow();
            newRow["OID"] = tbxOrderID.Text;
            newRow["Customer"] = tbxOrderName.Text;
            newRow["Subtotal"] = tbxSubtotal.Text;
            newRow["TaxRate"] = tbxTaxRate.Text;
            table.Rows.Add(newRow);
            return table;
        }

        private void btnAddOrder_Click(object sender, RoutedEventArgs e)
        {
            //Vist datatable order.
            DataTable order = business.Tables["Order"];
            NewMemberOrder(order);
            lstDisplayOrder.DataContext = order;
        }

以上就是c#数据绑定之数据转化为信息的示例的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 其他教程