Originally, it was thought that DataTable editing was simple, but not simple.
1. Modify
This is required:
DataRow row = myTable. Rows [0];
Row. BeginEdit ();
Row. ["userName"] = "aa ";
Row ["pwd"] = "121 ";
Row. EndEdit ();
Instance:
Implement data binding of the DataGridViewer and change the data format of one of the columns.
// Dg bind data
Public void Binding (string sqlstr)
{
SqlCommand com2 = DB. sqlcom (sqlstr );
SqlDataAdapter ad = new SqlDataAdapter (com2 );
DataSet ds = new DataSet ();
Ds. Clear ();
Ad. Fill (ds, "temp1 ");
DataTable dt = (DataTable) ds. Tables ["temp1"];
For (int I = 0; I <dt. Rows. Count; I ++)
{
DataRow row = dt. Rows [I];
Row. BeginEdit ();
String old = row ["billing time"]. ToString ();
Row ["billing time"] = old. substring (0, 4) + "year" + old. substring (4, 2) + "month" + old. substring (6, 2) + "day ";
Row. EndEdit ();
}
Dg. DataSource = dt. DefaultView;
}
2. Add a row
The Code is as follows:
Object [] row = new object [3];
Row [0] = "yy ";
Row [1] = "123 ";
Row [2] = "2007-09-09 ";
MyTable. Rows. Add (row );
3. delete a row
The Code is as follows:
Delete the second row
MyTable. Rows. RemoveAt (1 );