c#数据绑定之数据转化为信息的示例
本文导语: 目标界面: XAML代码: 代码如下: ...
目标界面:
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;
}