Http://www.cnblogs.com/fengkuangshubiaodian/archive/2012/08/01/2609911.html
A DataRow simulates a row in the database. Use the HasVersion and IsNull properties to determine the state of a particular row value.
1. Add Rows
Create a new DataRow to use the NewRow method of the DataTable object. Then, use the Add method to add the new DataRow to the DataRowCollection. Finally, call the AcceptChanges method of the DataTable object to confirm that it has been added. Refer to my other article in the C # DataTable for a specific description.
private void Createnewdatarow () {//Use the Maketable function below to create a new table. DataTable table; Table = Makenamestable (); Once A table has been created, with 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 = Workta ble. NewRow (); Workrow[0] = i; WORKROW[1] = "CustName" + i.tostring (); WORKTABLE.ROWS.ADD (Workrow);}
2. Delete Rows
You can remove a DataRow from DataRowCollection by calling the Remove method of DataRowCollection or by calling the Delete method of the DataRow object . The Remove method removes the row from the collection. In contrast, Delete marks the DataRow to be removed . An actual removal occurs when the AcceptChanges method is called. By calling Delete, you can programmatically check which rows are marked for removal before actually deleting the rows. For a specific description refer to my other article 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);
C # DataRow