一、增加列
在datatable中添加列有兩種方式,一種是在創建datatable時指定列的名稱和數據類型,二是在已有的datatable中增加列。在創建datatable時需要指定的列名和數據類型,如下所示:
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("age", typeof(int));
如果是在已有的datatable中增加列,可以使用Add方法:
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("age", typeof(int));
dt.Columns.Add("gender", typeof(string));
上述代碼中,我們在已有的datatable中增加一個名為”gender”的列。
二、往datatable數據中間增加一列
我們可以通過循環每行記錄,插入新的值。
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("age", typeof(int));
dt.Rows.Add(1, "Alice", 25);
dt.Rows.Add(2, "Bob", 29);
dt.Rows.Add(3, "Charlie", 32);
DataColumn myNewCol = new DataColumn("gender", typeof(string));
dt.Columns.Add(myNewCol);
int dataIndex = 0;
foreach (DataRow row in dt.Rows)
{
row["gender"] = "未知";
dataIndex++;
}
三、獲取某一列的數據
使用DataTable的Select方法可以直接獲取數據表中的數據。
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("age", typeof(int));
dt.Rows.Add(1, "Alice", 25);
dt.Rows.Add(2, "Bob", 29);
dt.Rows.Add(3, "Charlie", 32);
DataColumn ageColumn = dt.Columns[2];
foreach (DataRow row in dt.Select())
{
Console.WriteLine(row[ageColumn]);
}
四、複製一行到另一行
我們可以使用DataRow的Clone方法來複制行,並將其添加到新的datatable中。
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("age", typeof(int));
dt.Rows.Add(1, "Alice", 25);
dt.Rows.Add(2, "Bob", 29);
dt.Rows.Add(3, "Charlie", 32);
DataRow rowToCopy = dt.Rows[0];
DataRow newRow = dt.NewRow();
newRow.ItemArray = rowToCopy.ItemArray;
dt.Rows.Add(newRow);
五、隱藏列名
要隱藏datatable中的某一列的名稱,可以使用ColumnMapping屬性設置。
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("age", typeof(int));
dt.Columns[2].ColumnMapping = MappingType.Hidden;
六、數據類型
在datatable中定義列時,我們需要指定列的數據類型。常見的數據類型包括整型、字元串、日期等。
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("birthday", typeof(DateTime));
七、Select方法用法
使用Select方法可以從datatable中篩選出符合條件的數據。Select方法傳遞一個字元串參數,該參數表示需要查詢的表達式。
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("age", typeof(int));
dt.Rows.Add(1, "Alice", 25);
dt.Rows.Add(2, "Bob", 29);
dt.Rows.Add(3, "Charlie", 32);
DataRow[] result = dt.Select("age > 30");
foreach (DataRow row in result)
{
Console.WriteLine(row["name"]);
}
八、Datatable使用場景
除了在後台資料庫操作過程中使用datatable,它還可以作為集合中的等效替代品。對於小型應用程序,它是一種簡單而快速的存儲和處理數據的方法。在需要將數據存儲在內存中的情況下,它非常有用。
九、排序
Datatable提供了對列的排序方法。下面的例子演示了如何對年齡列升序排序。
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("age", typeof(int));
dt.Rows.Add(1, "Alice", 25);
dt.Rows.Add(2, "Bob", 29);
dt.Rows.Add(3, "Charlie", 32);
dt.DefaultView.Sort = "age ASC";
foreach (DataRow row in dt.DefaultView.ToTable().Rows)
{
Console.WriteLine(row["name"]);
}
以上就是datatable的詳細介紹,它是一個強大的工具,可以幫助我們高效的管理和處理數據。
原創文章,作者:KLMF,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/146533.html
微信掃一掃
支付寶掃一掃