Create a table
Datatable DTP = new datatable ("man ");
Add Columns
Datacolumn DCP;
DCP =
New datacolumn ("name", type. GetType ("system. String "));
DCP. readonly =
False;
DCP. allowdbnull = false;
DCP. Unique =
True;
DTP. Columns. Add (DCP );
DCP = new datacolumn ("sexy ",
Type. GetType ("system. int32 "));
DCP. readonly = false;
DCP. allowdbnull =
False;
DCP. Unique = false;
DTP. Columns. Add (DCP );
DCP = new
Datacolumn ("briday", type. GetType ("system. datetime "));
DCP. readonly =
False;
DCP. allowdbnull = false;
DCP. Unique =
False;
DTP. Columns. Add (DCP );
Add primary key
Datacolumn [] PK = new
Datacolumn [1];
PK [0] = DTP. Columns ["name"];
DTP. primarykey =
PK;
New row
Datarow newrow;
Newrow = DTP. newrow ();
Newrow ["name"] =
Textbox1.text;
Newrow ["sexy"] = checkbox1.checked? 1:
0;
Newrow ["briday"] =
Convert. todatetime (datetimepicker2.text );
DTP. Rows. Add (newrow );
DTP. acceptchanges ();
Bangding dategridview
Datagridview1.datasource
=
DTP;
Delete row
DTP. Rows [maid. currentrow. Index]. Delete ();
DTP. acceptchanges ();
Update row
Int
Dvindex = maid. index;
String filterstr = "name = '" +
Datagridview1.rows [dvindex]. cells [0]. value. tostring () + "'";
String tmpname =
NULL;
String tmpsexy = NULL;
String tmpbriday = NULL;
Datarow [] selrows
= DTP. Select (filterstr );
For (INT I = 0; I <selrows. length;
I ++)
{
Datarow temp = selrows [I];
Tmpname + = temp ["name"] = textbox1.text;
Tmpsexy + =
Temp ["sexy"] = checkbox1.checked? 1: 0;
Tmpbriday + =
Temp ["briday"] = datetimepicker2.text;
Selrows [I] =
Temp;
}
C # datatable usage