DataTable添加列和行的三种方法
2017-08-16 14:11
447 查看
#region 方法一:
DataTable tblDatas =new DataTable("Datas");
DataColumn dc =null;
dc = tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));
dc.AutoIncrement =true;//自动增加
dc.AutoIncrementSeed =1;//起始为1
dc.AutoIncrementStep =1;//步长为1
dc.AllowDBNull =false;
dc = tblDatas.Columns.Add("Product", Type.GetType("System.String"));
dc = tblDatas.Columns.Add("Version", Type.GetType("System.String"));
dc = tblDatas.Columns.Add("Description", Type.GetType("System.String"));
DataRow newRow;
newRow = tblDatas.NewRow();
newRow["Product"]
="这个地方是单元格的值";
newRow["Version"]
="2.0";
newRow["Description"]
="这个地方是单元格的值";
tblDatas.Rows.Add(newRow);
newRow = tblDatas.NewRow();
newRow["Product"]
="这个地方是单元格的值";
newRow["Version"]
="3.0";
newRow["Description"]
="这个地方是单元格的值";
tblDatas.Rows.Add(newRow);
#endregion
#region 方法二:
DataTable tblDatas =new DataTable("Datas");
tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));
tblDatas.Columns[0].AutoIncrement
=true;
tblDatas.Columns[0].AutoIncrementSeed
=1;
tblDatas.Columns[0].AutoIncrementStep
=1;
tblDatas.Columns.Add("Product", Type.GetType("System.String"));
tblDatas.Columns.Add("Version", Type.GetType("System.String"));
tblDatas.Columns.Add("Description", Type.GetType("System.String"));
tblDatas.Rows.Add(newobject[] {
null,
"a",
"b",
"c" });
tblDatas.Rows.Add(newobject[] {
null,
"a",
"b",
"c" });
tblDatas.Rows.Add(newobject[] {
null,
"a",
"b",
"c" });
tblDatas.Rows.Add(newobject[] {
null,
"a",
"b",
"c" });
tblDatas.Rows.Add(newobject[] {
null,
"a",
"b",
"c" });
#endregion
#region 方法三:
DataTable table =new DataTable();
//创建table的第一列
DataColumn priceColumn =new DataColumn();
priceColumn.DataType = System.Type.GetType("System.Decimal");//该列的数据类型
priceColumn.ColumnName ="price";//该列得名称
priceColumn.DefaultValue =50;//该列得默认值
// 创建table的第二列
DataColumn taxColumn =new DataColumn();
taxColumn.DataType = System.Type.GetType("System.Decimal");
taxColumn.ColumnName ="tax";//列名
taxColumn.Expression ="price * 0.0862";//设置该列得表达式,用于计算列中的值或创建聚合列
// 创建table的第三列
DataColumn totalColumn =new DataColumn();
totalColumn.DataType = System.Type.GetType("System.Decimal");
totalColumn.ColumnName ="total";
totalColumn.Expression ="price + tax";//该列的表达式,是第一列和第二列值得和
// 将所有的列添加到table上
table.Columns.Add(priceColumn);
table.Columns.Add(taxColumn);
table.Columns.Add(totalColumn);
//创建一行
DataRow row = table.NewRow();
table.Rows.Add(row);//将此行添加到table中
//将table放在视图中
DataView view =new DataView(table);
//绑定到DataGrid
dg.DataSource = view;
dg.DataBind();
#endregion
DataTable tblDatas =new DataTable("Datas");
DataColumn dc =null;
dc = tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));
dc.AutoIncrement =true;//自动增加
dc.AutoIncrementSeed =1;//起始为1
dc.AutoIncrementStep =1;//步长为1
dc.AllowDBNull =false;
dc = tblDatas.Columns.Add("Product", Type.GetType("System.String"));
dc = tblDatas.Columns.Add("Version", Type.GetType("System.String"));
dc = tblDatas.Columns.Add("Description", Type.GetType("System.String"));
DataRow newRow;
newRow = tblDatas.NewRow();
newRow["Product"]
="这个地方是单元格的值";
newRow["Version"]
="2.0";
newRow["Description"]
="这个地方是单元格的值";
tblDatas.Rows.Add(newRow);
newRow = tblDatas.NewRow();
newRow["Product"]
="这个地方是单元格的值";
newRow["Version"]
="3.0";
newRow["Description"]
="这个地方是单元格的值";
tblDatas.Rows.Add(newRow);
#endregion
#region 方法二:
DataTable tblDatas =new DataTable("Datas");
tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));
tblDatas.Columns[0].AutoIncrement
=true;
tblDatas.Columns[0].AutoIncrementSeed
=1;
tblDatas.Columns[0].AutoIncrementStep
=1;
tblDatas.Columns.Add("Product", Type.GetType("System.String"));
tblDatas.Columns.Add("Version", Type.GetType("System.String"));
tblDatas.Columns.Add("Description", Type.GetType("System.String"));
tblDatas.Rows.Add(newobject[] {
null,
"a",
"b",
"c" });
tblDatas.Rows.Add(newobject[] {
null,
"a",
"b",
"c" });
tblDatas.Rows.Add(newobject[] {
null,
"a",
"b",
"c" });
tblDatas.Rows.Add(newobject[] {
null,
"a",
"b",
"c" });
tblDatas.Rows.Add(newobject[] {
null,
"a",
"b",
"c" });
#endregion
#region 方法三:
DataTable table =new DataTable();
//创建table的第一列
DataColumn priceColumn =new DataColumn();
priceColumn.DataType = System.Type.GetType("System.Decimal");//该列的数据类型
priceColumn.ColumnName ="price";//该列得名称
priceColumn.DefaultValue =50;//该列得默认值
// 创建table的第二列
DataColumn taxColumn =new DataColumn();
taxColumn.DataType = System.Type.GetType("System.Decimal");
taxColumn.ColumnName ="tax";//列名
taxColumn.Expression ="price * 0.0862";//设置该列得表达式,用于计算列中的值或创建聚合列
// 创建table的第三列
DataColumn totalColumn =new DataColumn();
totalColumn.DataType = System.Type.GetType("System.Decimal");
totalColumn.ColumnName ="total";
totalColumn.Expression ="price + tax";//该列的表达式,是第一列和第二列值得和
// 将所有的列添加到table上
table.Columns.Add(priceColumn);
table.Columns.Add(taxColumn);
table.Columns.Add(totalColumn);
//创建一行
DataRow row = table.NewRow();
table.Rows.Add(row);//将此行添加到table中
//将table放在视图中
DataView view =new DataView(table);
//绑定到DataGrid
dg.DataSource = view;
dg.DataBind();
#endregion
相关文章推荐
- DataTable添加列和行的三种方法
- DataTable添加列和行的三种方法
- DataTable添加列和行的三种方法
- DataTable添加列和行的三种方法
- C#中DataTable添加列和行的三种方法
- DataTable添加列和行的三种方法
- DataTable添加列和行的三种方法
- DataTable添加列和行的三种方法
- asp DataTable添加列和行的三种方法
- [.Net] DataTable添加列和行的三种方法
- C#给Winform的button等控件添加快捷键三种方法
- Android开发---为按钮添加事件的三种方法
- DataTable各种方法总结(给Datatable添加行列、DataTable选择排序等
- 在VS中添加lib库的三种方法
- 在VS中添加lib的三种方法
- 在VS中添加lib库的三种方法
- 对DataGrid/DataList/Repeater添加序号的三种方法
- Android Studio添加库的三种方法
- Mac添加环境变量的三种方法
- PHP中添加HTML代码的三种方法(print<<<END)