using System.Data;
(1) Create a DataTable
(1) Create a DataTable
DataTable dt = new DataTable("TableName"); (2) Add columns for DataTable
//Method 1
dt.Columns.Add("column0", typeof(string));
//Method 2
DataColumn dc = new DataColumn("column1", typeof(bool));
dt.Columns.Add(dc);(3) Add rows for DataTable
//Initialize the row
DataRow dr = dt.NewRow();
//Arrange data to dr(row1)
dr["column0"] = "AX";
dr["column1"] = true;
//Add row dr to GridTable
dt.Rows.Add(dr);
//Doesn't initialize the row
DataRow dr1 = dt.NewRow();
dt.Rows.Add(dr1);(4) Delete rows
dt.Rows[this.dataGrid1.CurrentRowIndex].Delete();
本文详细介绍了如何使用C#创建和操作DataTable,包括添加列、添加行、删除行等基本操作,适用于数据库和数据处理领域的开发者。
1万+

被折叠的 条评论
为什么被折叠?



