文/嶽永鹏

  本文主要在数据绑定1和2中新增了DataSet对象,练习了如何在DataSet中添加表、关系和约束,同时本文也简要的介绍了如何将数据转化为信息。

目标界面:

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#数据绑定(3)——数据转化为信息的更多相关文章

  1. c#教程之通过数据绑定修改数据

    通过数据绑定修改数据 "实体框架"提供了与数据库的双向通信通道.前面已经讲述了如何使用"实体框架"获 取数据,现在来看看如何修改获取的信息,并将改动发送回数据库 ...

  2. asp.net/wingtip/显示数据和详细信息

    前边我们的工作处于wingtip工程基础建设阶段,先是建立了“数据访问层”,然后设计建设了“UI和导航”的框架,接下来要充实工程的内容,显示“数据和详细信息”. 一. 添加数据控件(Data Cont ...

  3. Unix环境高级编程(四)数据系统文件和信息

    本章主要介绍了Unix系统的正常运行要使用的与系统有关的数据文件和信息.如:口令文件,阴影文件.组文件.附加组.系统标识.时间和日期历程. 口令文件,即Unix系统用户数据库,存储在/etc/pass ...

  4. OLEDB 静态绑定和数据转化接口

    OLEDB 提供了静态绑定和动态绑定两种方式,相比动态绑定来说,静态绑定在使用上更加简单,而在灵活性上不如动态绑定,动态绑定在前面已经介绍过了,本文主要介绍OLEDB中的静态,以及常用的数据类型转化接 ...

  5. xml格式的数据转化成数组

    将得到的xml格式的数据转化成数组 <?php //构造xml $url = "http://api.map.baidu.com/telematics/v3/weather?locat ...

  6. 将数据转化成字符串时:用字符串的链接 还是 StringBuilder

    /* 目的:将数据转化成字符串时:用字符串的链接 还是 StringBuilder呢? */ public class Test{ public static void main(String[] a ...

  7. (四) 一起学 Unix 环境高级编程(APUE) 之 系统数据文件和信息

    . . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...

  8. Yii框架AR对象数据转化为数组

    demo函数作用:将AR对象数据转化为数组 局限:仅用于findAll的多维数组,find一维数组可以先转化为多维数组的一个元素在使用 function actionIndex() { $data = ...

  9. sa命令从/var/account/pacct原始记账数据文件读取信息并汇总

    sa命令从/var/account/pacct原始记账数据文件读取信息并汇总

随机推荐

  1. 关于dvajs里effects的call和put

    call会把return 传回来 put把参数穿回来了 在effects里好像只有yield能触发put ,call暂时没定

  2. shell-脚本入门【转】

    转自:http://blog.csdn.net/gexiaobaohelloworld/article/details/7973846 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?) ...

  3. ORA-12537: TNS:connection closed

    http://www.vitalsofttech.com/ora-12537-tnsconnection-closed/ Question: When trying to establish a sq ...

  4. MVC Razor模板引擎 @RenderBody、@RenderPage、@RenderSection及Html.RenderPartial、Html.RenderAction

    一.Views文件夹 -> Shared文件夹下的 _Layout.cshtml 母版页 @RenderBody 当创建基于_Layout.cshtml布局页面的视图时,视图的内容会和布局页面合 ...

  5. Angular--页面间切换及传值的四种方法

    1. 基于ui-router的页面跳转传参(1) 在AngularJS的app.js中用ui-router定义路由,比如现在有两个页面,一个页面(producers.html)放置了多个produce ...

  6. 类传奇手游简单Demo

    这是一年多前自己闲时以Unity2D制作的很粗糙简单的传奇类手游Demo(单机),已很久未作继续开发. 此小Demo初步完成或实现了如下功能(有诸多考虑欠妥甚至不完善之处): 1).图片资源打包方式. ...

  7. easyUI的Dialog和Windows框的应用

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  8. docker私有库UI和添加私有库到本机能够push和pull

    $ docker run -p 8080:8080 -e REG1=http://104.236.246.10:5000/v1/ atcol/docker-registry-ui$ docker ru ...

  9. IBM X3850 Windows 无法安装到这个磁盘。选中的磁盘具有MBR分区表。在 EFI 系统上,Windows 只能安装到 GPT 磁盘

    以前安装的是window2003 32位, 改装为2012 64位的时候.出现 Windows 无法安装到这个磁盘.选中的磁盘具有MBR分区表.在 EFI 系统上,Windows 只能安装到 GPT ...

  10. CentOS 7 最小化安装的无线网络配置

    1.首先下载iw工具. yum -y install iw 2.获取无线网卡的名称 执行iw dev,假设获得名称为 wlp3s0(示例) 3.激活无线网络接口 执行ip link set wlp3s ...