【WinForm】杂记(6):C#之DataTable类(总结)
2021-01-21 01:12
标签:which 方法 aaa color ini tab 完整 osi col 内容列表 属性 方法 其它 属性 获取行列数 方法 添加列 添加行 删除行列 获取列名 获取列类型 更改列名 获取单元格数值 写入数值到单元格 完整代码 其它 转置 将一张表的某列添加到另一张表(行添加的同理) 【WinForm】杂记(6):C#之DataTable类(总结) 标签:which 方法 aaa color ini tab 完整 osi col 原文地址:https://www.cnblogs.com/RicardoIsLearning/p/12116037.html
int numOfRows = dt.Rows.Count;
int numOfCols = dt.Columns.Count;
dt.Columns.Add("Name", typeof(string)).SetOrdinal(0);//to the first column
DataColumn dc1 = new DataColumn("Tol", typeof(string));
dt.Columns.Add(dc1);
//method1
DataRow dr1 = dt.NewRow();
dt.Rows.Add(dr1);
//method2
dt.Rows.Add();
dt.Columns.RemoveAt(index);//index is location of col
dt.Rows.RemoveAt(index);dt.Columns[i].ColumnName.ToString();
Type dataType= dataDt.Columns["Name"].DataType
dt.Columns[0].ColumnName = "AAA";
var val = dt.Rows[i][j].toString();
dt.Rows[i][j]=val;//The type of val should be the same as the column, which the cell belongs to
private void tableoperations()
{
System.Data.DataTable dt = new System.Data.DataTable();
int index = 0;
int i = 0, j = 0;
//add new column
dt.Columns.Add("Name", typeof(string)).SetOrdinal(0);//to the first column
DataColumn dc1 = new DataColumn("Tol", typeof(string));
dt.Columns.Add(dc1);
//add new row
//method1
DataRow dr1 = dt.NewRow();
dt.Rows.Add(dr1);
//method2
dt.Rows.Add();
//delet columns
dt.Columns.RemoveAt(index);
//delet rows
dt.Rows.RemoveAt(index);
//get the column name
dt.Columns[i].ColumnName.ToString();
//change columns name:
dt.Columns[0].ColumnName = "AAA";
//read/write the cell value
dt.Rows[i][j] = 1;
//get column type
Type dataType = dt.Columns["Name"].DataType;
}
private System.Data.DataTable transpositioin(System.Data.DataTable tb_org, int col_start = 0)//the original one has names of rows and columns
{
System.Data.DataTable tb_trans = new System.Data.DataTable();
tb_trans.Columns.Add("item", typeof(string));
for (int i = 0; i
private System.Data.DataTable getcolumn(System.Data.DataTable tb_WithTarCol)
{
System.Data.DataTable tb = new System.Data.DataTable();
tb.Columns.Add(tb_WithTarCol.Columns[0].ColumnName.ToString(), typeof(string));//here, get the first column
foreach (DataRow dr in tb_WithTarCol.Rows) {
DataRow tmp = tb.NewRow();
tmp[0] = dr[0];//get the first cell, which belongs to the first column
tb.Rows.Add(tmp);
}
return tb;
}
文章标题:【WinForm】杂记(6):C#之DataTable类(总结)
文章链接:http://soscw.com/index.php/essay/44784.html