c#数据绑定之数据转化为信息的示例
目标界面:
XAML代码:
<Grid Margin="2">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition />
</Grid.RowDefinitions>
<GroupBox Header="Customer" Grid.Row="0" Padding="5">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="CustomerID" Margin="2" HorizontalAlignment="Right" VerticalAlignment="Center"/>
<TextBox Grid.Row="0" Grid.Column="1" Margin="2" Name="tbxCustomerID" Text="{Binding Path=CID}"/>
<TextBlock Grid.Row="1" Grid.Column="0" Text="CustomerName" Margin="2" HorizontalAlignment="Right" VerticalAlignment="Center"/>
<TextBox Grid.Row="1" Grid.Column="1" Margin="2" Name="tbxCustomerName" Text="{Binding Path=Name}"/>
<Button Grid.Row="2" Grid.Column="1" Content="Add New Customer" Margin="2" Name="btnAddCustomer" Padding="2" Click="btnAddCustomer_Click" />
</Grid>
</GroupBox>
<GroupBox Header="Order" Grid.Row="1" Padding="5">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="OrderID" Margin="2" HorizontalAlignment="Right" VerticalAlignment="Center"/>
<TextBox Grid.Row="0" Grid.Column="1" Margin="2" Name="tbxOrderID" Text="{Binding Path=OID}"/>
<TextBlock Grid.Row="1" Grid.Column="0" Text="OrderName" Margin="2" HorizontalAlignment="Right" VerticalAlignment="Center"/>
<TextBox Grid.Row="1" Grid.Column="1" Margin="2" Name="tbxOrderName" Text="{Binding Path=Customer}"/>
<TextBlock Grid.Row="2" Grid.Column="0" Text="Subtotal" Margin="2" HorizontalAlignment="Right" VerticalAlignment="Center"/>
<TextBox Grid.Row="2" Grid.Column="1" Margin="2" Name="tbxSubtotal" Text="{Binding Path=Subtotal}"/>
<TextBlock Grid.Row="3" Grid.Column="0" Text="TaxRate" Margin="2" HorizontalAlignment="Right" VerticalAlignment="Center"/>
<TextBox Grid.Row="3" Grid.Column="1" Margin="2" Name="tbxTaxRate" Text="{Binding Path=TaxRate}"/>
<Button Grid.Row="4" Grid.Column="1" Content="Add New Order" Margin="2" Name="btnAddOrder" Padding="2" Click="btnAddOrder_Click" />
</Grid>
</GroupBox>
<ListView Name="lstDisplayCustomer" ItemsSource="{Binding}" Grid.Row="2" Margin="2" MinHeight="150">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="CustomerID" DisplayMemberBinding="{Binding CID}"/>
<GridViewColumn Header="CustomerName" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="Total" DisplayMemberBinding="{Binding OrderTotals}" />
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
<ListView Name="lstDisplayOrder" ItemsSource="{Binding}" Grid.Row="3" Margin="2" MinHeight="150">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="OrderID" DisplayMemberBinding="{Binding OID}"/>
<GridViewColumn Header="Customer" DisplayMemberBinding="{Binding Customer}"/>
<GridViewColumn Header="Subtotal" DisplayMemberBinding="{Binding Subtotal}"/>
<GridViewColumn Header="TaxRate" DisplayMemberBinding="{Binding TaxRate}" />
<GridViewColumn Header="Total" DisplayMemberBinding="{Binding Total}"/>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</Grid>
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# 如何向comboBox中绑定数据库中指定的数据信息;
- c#数据绑定(3)——数据转化为信息
- c#数据绑定之linq使用示例
- c#DataGridView数据绑定示例——格式化单元格的内容(转)
- C# 数据绑定 控件信息的获取
- c# .Net :Excel NPOI导入导出操作教程之数据库表信息数据导出到一个Excel文件并写到磁盘示例分享
- 【转】详解Data Binding 通过几个简单示例深入了解WinForm数据绑定特性
- C#GridView绑定datatable数据
- 在C#中使用组合框数据绑定的问题
- C# TreeView树视图控件 如何把树与数据库的信息进行绑定
- Asp.Net--DropDownList与DataTable数据绑定的方法(C#)[学习笔记]
- 将C#数据转化成Word文档
- c# 中combobox绑定数据
- C# aspx 数据绑定 Bind、Eval 、DataBinder.eval、Container.DataItem
- C# Winform Combobox手动绑定数据
- c# TreeView 数据递归绑定的算法
- asp.net customdatagrid 绑定数据用到的ItemDataBound 中的HyperLinkColumn 的跳转链接 通过C#后台 处理的方法
- C# datagridviewcomboxcolumn数据的绑定与值的显示
- 为用户定义的数据类型绑定规则示例.sql
- C#实现的多列数据绑定组合框控件MultiColumnComboBoxEx