C# DataRow

來源:互聯網
上載者:User

標籤:link   cep   編程方式   move   cti   modified   刪除行   資料   object   

http://www.cnblogs.com/fengkuangshubiaodian/archive/2012/08/01/2609911.html

DataRow 類比的是資料庫中的一行。使用 HasVersion 和 IsNull 屬性確定特定行值的狀態。

1. 添加行

  建立新的 DataRow,要使用 DataTable 對象的 NewRow 方法。然後,使用 Add 方法將新的 DataRow 添加到 DataRowCollection 中。最後,調用 DataTable 對象的 AcceptChanges 方法以確認是否已添加。具體描述參考我另一篇文章 C# DataTable。

private void CreateNewDataRow(){    // Use the MakeTable function below to create a new table.    DataTable table;    table = MakeNamesTable();    // Once a table has been created, use the     // NewRow to create a DataRow.    DataRow row;    row = table.NewRow();    // Then add the new row to the collection.    row["fName"] = "John";    row["lName"] = "Smith";    table.Rows.Add(row);    foreach(DataColumn column in table.Columns)        Console.WriteLine(column.ColumnName);    dataGrid1.DataSource=table;}private DataTable MakeNamesTable(){    // Create a new DataTable titled ‘Names.‘    DataTable namesTable = new DataTable("Names");     // Add three column objects to the table.    DataColumn idColumn = new  DataColumn();    idColumn.DataType = System.Type.GetType("System.Int32");    idColumn.ColumnName = "id";    idColumn.AutoIncrement = true;    namesTable.Columns.Add(idColumn);    DataColumn fNameColumn = new DataColumn();    fNameColumn.DataType = System.Type.GetType("System.String");    fNameColumn.ColumnName = "Fname";    fNameColumn.DefaultValue = "Fname";    namesTable.Columns.Add(fNameColumn);    DataColumn lNameColumn = new DataColumn();    lNameColumn.DataType = System.Type.GetType("System.String");    lNameColumn.ColumnName = "LName";    namesTable.Columns.Add(lNameColumn);    // Create an array for DataColumn objects.    DataColumn [] keys = new DataColumn [1];    keys[0] = idColumn;    namesTable.PrimaryKey = keys;    // Return the new DataTable.    return namesTable;}

///////////////////////////////////////////////////////DataRow workRow;for (int i = 0; i <= 9; i++) { workRow = workTable.NewRow(); workRow[0] = i; workRow[1] = "CustName" + i.ToString(); workTable.Rows.Add(workRow);}
2. 刪除行

  您可通過調用 DataRowCollection 的 Remove 方法或調用 DataRow 對象的 Delete 方法,從 DataRowCollection 中刪除 DataRow。Remove 方法將行從集合中移除。與此相反,Delete 標記要移除的 DataRow。在調用AcceptChanges 方法時發生實際移除。通過調用 Delete,您可在實際刪除行之前以編程方式檢查哪些行被標記為移除。具體描述參考我另外一篇文章 C# DataTable。

private void DemonstrateAcceptChanges(){    //Run a function to create a DataTable with one column.    DataTable table = MakeTable();    DataRow row;    // Create a new DataRow.    row = table.NewRow();    // Detached row.    Console.WriteLine("New Row " + row.RowState);    table.Rows.Add(row);    // New row.    Console.WriteLine("AddRow " + row.RowState);    table.AcceptChanges();    // Unchanged row.    Console.WriteLine("AcceptChanges " + row.RowState);    row["FirstName"] = "Scott";    // Modified row.    Console.WriteLine("Modified " + row.RowState);    row.Delete();    // Deleted row.    Console.WriteLine("Deleted " + row.RowState);}private DataTable MakeTable(){    // Make a simple table with one column.    DataTable table = new DataTable("table");    DataColumn fnameColumn = new DataColumn(        "FirstName", Type.GetType("System.String"));    table.Columns.Add(fnameColumn);    return table;} 

 

C# DataRow

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.