C#
复制代码
// Put the next line into the Declarations section.
private System.Data.DataSet dataSet;
private void MakeDataTables()
{
// Run all of the functions.
MakeParentTable();
MakeChildTable();
MakeDataRelation();
BindToDataGrid();
}
private void MakeParentTable()
{
// Create a new DataTable.
System.Data.DataTable table = new DataTable("ParentTable");
// Declare variables for DataColumn and DataRow objects.
DataColumn column;
DataRow row;
// Create new DataColumn, set DataType,
// ColumnName and add to DataTable.
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "id";
column.ReadOnly = true;
column.Unique = true;
// Add the Column to the DataColumnCollection.
table.Columns.Add(column);
// Create second column.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "ParentItem";
column.AutoIncrement = false;
column.Caption = "ParentItem";
column.ReadOnly = false;
column.Unique = false;
// Add the column to the table.
table.Columns.Add(column);
// Make the ID column the primary key column.
DataColumn[] PrimaryKeyColumns = new DataColumn[1];
PrimaryKeyColumns[0] = table.Columns["id"];
table.PrimaryKey = PrimaryKeyColumns;
// Instantiate the DataSet variable.
dataSet = new DataSet();
// Add the new DataTable to the DataSet.
dataSet.Tables.Add(table);
// Create three new DataRow objects and add
// them to the DataTable
for (int i = 0; i<= 2; i++)
{
row = table.NewRow();
row["id"] = i;
row["ParentItem"] = "ParentItem " + i;
table.Rows.Add(row);
}
}
private void MakeChildTable()
{
// Create a new DataTable.
DataTable table = new DataTable("childTable");
DataColumn column;
DataRow row;
// Create first column and add to the DataTable.
column = new DataColumn();
column.DataType= System.Type.GetType("System.Int32");
column.ColumnName = "ChildID";
column.AutoIncrement = true;
column.Caption = "ID";
column.ReadOnly = true;
column.Unique = true;
// Add the column to the DataColumnCollection.
table.Columns.Add(column);
// Create second column.
column = new DataColumn();
column.DataType= System.Type.GetType("System.String");
column.ColumnName = "ChildItem";
column.AutoIncrement = false;
column.Caption = "ChildItem";
column.ReadOnly = false;
column.Unique = false;
table.Columns.Add(column);
// Create third column.
column = new DataColumn();
column.DataType= System.Type.GetType("System.Int32");
column.ColumnName = "ParentID";
column.AutoIncrement = false;
column.Caption = "ParentID";
column.ReadOnly = false;
column.Unique = false;
table.Columns.Add(column);
dataSet.Tables.Add(table);
// Create three sets of DataRow objects,
// five rows each, and add to DataTable.
for(int i = 0; i <= 4; i ++)
{
row = table.NewRow();
row["childID"] = i;
row["ChildItem"] = "Item " + i;
row["ParentID"] = 0 ;
table.Rows.Add(row);
}
for(int i = 0; i <= 4; i ++)
{
row = table.NewRow();
row["childID"] = i + 5;
row["ChildItem"] = "Item " + i;
row["ParentID"] = 1 ;
table.Rows.Add(row);
}
for(int i = 0; i <= 4; i ++)
{
row = table.NewRow();
row["childID"] = i + 10;
row["ChildItem"] = "Item " + i;
row["ParentID"] = 2 ;
table.Rows.Add(row);
}
}
private void MakeDataRelation()
{
// DataRelation requires two DataColumn
// (parent and child) and a name.
DataColumn parentColumn =
dataSet.Tables["ParentTable"].Columns["id"];
DataColumn childColumn =
dataSet.Tables["ChildTable"].Columns["ParentID"];
DataRelation relation = new
DataRelation("parent2Child", parentColumn, childColumn);
dataSet.Tables["ChildTable"].ParentRelations.Add(relation);
}
private void BindToDataGrid()
{
// Instruct the DataGrid to bind to the DataSet, with the
// ParentTable as the topmost DataTable.
dataGrid1.SetDataBinding(dataSet,"ParentTable");
}

转载于:https://www.cnblogs.com/shineqiujuan/archive/2008/05/30/1210976.html

DataTable类(MSDN)相关推荐

  1. ASP.NET基础教程-DataTable类对象-属性方法和事件

    DataTable类对象可以表示表格,也可以在DataSet中存储多个DataTable对象. 该对象的属性方法和事件列表如下: 转载于:https://blog.51cto.com/chenxing ...

  2. C#操作DataTable类

    一.DataTable简介 (1)构造函数 名称 说明 DataTable()  不带参数初始化DataTable 类的新实例 DataTable(string tableName) 用指定的表名初始 ...

  3. C#学习 数据模块--DataTable类

    DataTable类使用:(在内存中像实际表格的方式作业) 1.构造函数: public DataTable(); public DataTable(string tableName); public ...

  4. DataTable 类(一)表结果操作

    DataTable 是 ADO.NET 库中的核心类.其他使用 DataTable 的对象包括 DataSet 和 DataView. 1.DataTable特点 (1)DataTable 对象是按条 ...

  5. WPF 提供了以下关键帧动画类[msdn]

    属性类型 对应的 From/To/By 动画类 支持的内插方法 Boolean BooleanAnimationUsingKeyFrames 离散 Byte ByteAnimationUsingKey ...

  6. BindingNavigator 类 - MSDN

    .NET Framework 2.0 其他版本 5(共 9)对本文的评价是有帮助 - 评价此主题 注意:此类在 .NET Framework 2.0 版中是新增的. 表示窗体上绑定到数据的控件的导航和 ...

  7. java datatable用法_Java中实现DataTable工具类,并利用其实现简单分页控件。

    具体工具类代码,请见我上一个博客. 一.工具类的使用 1.1 DataTable工具类的使用 1.1.1DataTable简单解析 顾名思义,DataTable其实就是一张虚拟数据表,用于存储由数据库 ...

  8. 深入.NET DataTable

    1.ADO.NET相关对象一句话介绍 1)DataAdapter: DataAdapter实际是一个SQL语句集合,因为对Database的操作最终需要归结到SQL语句. 2)Dataset: Dat ...

  9. 浅谈 System.Data.DataRowCollection 类

    我们来看看以下程序吧: 01: using System; 02: using System.Data; 03: using System.Linq; 04: 05: namespace Skyiv. ...

最新文章

  1. animation动画不生效_关于CSS3的animation使用的一些坑,需要注意下!
  2. 在小公司、中等规模公司和大公司工作有什么不同
  3. mysql无法存储文字_mysql存储不了中文字符串怎么办
  4. BZOJ.4180.字符串计数(后缀自动机 二分 矩阵快速幂/倍增Floyd)
  5. sql server一个查询语句引发的死锁
  6. ROS的学习(十五)验证publisher和subscriber
  7. 【LeetCode】【字符串】题号:*434. 字符串中的单词数
  8. ppt生成器_#PPT素材神器#在线卡通头像生成器: avataaars generator
  9. 联想服务器安装2019系统,联想支持的Win10 2019年10月更新(1909版本)的机型
  10. mysql中utf8和utf8mb4区别,MySQL中utf8和utf8mb4的区别
  11. 自己动手制作RPM包
  12. 【计算机操作系统】银行家算法的模拟实现
  13. 计算机运行快捷方式,电脑运行快捷键有哪些 电脑运行快捷键介绍
  14. 物联网专业属于计算机的哪个门类,物联网工程专业属于什么门类
  15. c语言月份判断季节 switch,C语言编程基础(7页)-原创力文档
  16. 【牛客】快手2020校园招聘秋招笔试--算法A试卷
  17. matlab如何形成节点导纳矩阵,关于利用矩阵稀疏技术求解节点导纳矩阵的MATLAB编程...
  18. SNS游戏开发的感想
  19. arcgis server 授权许可失败问题
  20. Transformer发展历史

热门文章

  1. 透明色的rgb值是多少_一文掌握PPT主题色原理及使用技巧
  2. 【Python数据分析】数据挖掘建模——分类与预测——回归分析
  3. 幽默度识别第一名解决方案代码及说明
  4. 开源中文关系抽取框架,来自浙大知识引擎实验室
  5. python画羊_羊车门作业 Python版
  6. python str转int_Python入门丨数据类型与转换
  7. 修辞结构理论论文集合
  8. 如何制作一款HTML5 RPG游戏引擎——第三篇,利用幕布切换场景
  9. [RHEL5企业级Linux服务攻略]--第5季 Sendmail服务全攻略之高级配置
  10. 基于盐+Sha算法的安全密码保护机制