C# DataTable 增加行与列
Posted 杨千羽
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# DataTable 增加行与列相关的知识,希望对你有一定的参考价值。
DataTable添加列和行的三种方法
#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
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
以上是关于C# DataTable 增加行与列的主要内容,如果未能解决你的问题,请参考以下文章