文/嶽永鹏

  本文主要在数据绑定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. php常用array函数

    http://www.w3school.com.cn/php/php_ref_array.asp 1.array_change_key_case() 把数组中所有键更改为小写或大写2.array_ch ...

  2. iOS开发直播需要的准备

    这里我们要研究直播技术首先需要对AVFoundation熟悉掌握 AVFoundation拍照和录制视频 AVFoundation中提供了很多现成的播放器和录音机,但是事实上它还有更加底层的内容可以供 ...

  3. c# winform DirectX播放器 可以任意设置宽高比 屏幕拉伸

    第一步:dll引用 Microsoft.DirectX.dll Microsoft.DirectX.AudioVideoPlayback.dll 如果没有的话,可能需要安装微软的DRECTX JDK ...

  4. three.js初涉略(一)

    <!-- 最近要研究一下webgl,发现了webgl中文网(http://www.hewebgl.com/article/articledir/1).边研究教程边做下记录 --> thre ...

  5. css基础样式四

    上次我们讲到了相对定位: 这次我们了解下绝对定位; 绝对定位: #box_relative { position: absolute; left: 30px; top: 20px; } 绝对定位会脱离 ...

  6. Hibernate之环境搭建及demo

    ORM概念 ORM即Object/Relation Mapping, 对象/关系数据库映射.ORM是一种规范,完成面向对象编程语言到关系数据库之间的映射.J2EE中的JPA就是一种ORM规范. ORM ...

  7. django之分页、cookie装饰器

    一.分页代码如下 from django.utils.safestring import mark_safe class Page: def __init__(self, current_page, ...

  8. 使用ajax分页

    前台页面: <table class="table table-hover"> <thead> <tr> <th class='hidde ...

  9. java使用sax解析xml

    目的:解析xml文件,并存入mysql,并且要解析的字段能一一对应.这里解析的是微博的文件,想要利用里面的article和person_id字段. 思路: 为了能得到person_id和article ...

  10. Maven安装配置使用

    Maven介绍 Maven是一个项目管理工具,它包含了一个项目对象模型 (Project Object Model),一组标准集合,一个项目生命周期(Project Lifecycle),一个依赖管理 ...