In the DataTable, get the cell contents of row i J columns:
String str = dataset.tables[0]. ROWS[I][J]. ToString ();
DataGridView, gets the cell contents of row i J columns:
String str = Datagridview.rows[i]. CELLS[J]. Value.tostring ();
After the SelectionMode property of DataGridView is set to Fullrowselect, gets the contents of the specified cell:
String str = Datagridview.currentrow.cells[j]. Value.tostring ();
DataGridView Click once to enter the cell editing state:
This.dataGridView1.EditMode = System.Windows.Forms.DataGridViewEditMode.EditOnEnter;
DataGridView A column automatically adapts to the column width:
THIS.DATAGRIDVIEW1.COLUMNS[J]. AutoSizeMode = Datagridviewautosizecolumnmode.allcells;
DataGridView Press the ENTER key to jump to the next row in the same column
Add rows to the DataTable:
Method One:
datatable Tbldatas = new DataTable ("Datas");
DataColumn DC = null;
DC = TBLDATAS.COLUMNS.ADD ("ID", Type.GetType ("System.Int32"));
dc. AutoIncrement = true;//Auto-increment
dc. AutoIncrementSeed = 1;//starting at 1
dc. AutoIncrementStep = 1;//step is 1
dc. AllowDBNull = false;//
DC = TBLDATAS.COLUMNS.ADD ("Product", Type.GetType ("System.String"));
DC = TBLDATAS.COLUMNS.ADD ("Version", Type.GetType ("System.String"));
DC = TBLDATAS.COLUMNS.ADD ("Description", Type.GetType ("System.String"));
DataRow NewRow;
NewRow = Tbldatas.newrow ();
newrow["Product" = "fruit knife";
newrow["Version"] = "2.0";
newrow["Description"] = "fight Dedicated";
TBLDATAS.ROWS.ADD (NewRow);
NewRow = Tbldatas.newrow ();
newrow["Product" = "folding stool";
newrow["Version"] = "3.0";
newrow["Description"] = "one of the seven arms of a walking lake";
TBLDATAS.ROWS.ADD (NewRow);
Method Two:
DataTable Tbldatas = new DataTable ("Datas");
TBLDATAS.COLUMNS.ADD ("ID", Type.GetType ("System.Int32"));
Tbldatas.columns[0]. AutoIncrement = true;
Tbldatas.columns[0]. AutoIncrementSeed = 1;
Tbldatas.columns[0]. AutoIncrementStep = 1;
TBLDATAS.COLUMNS.ADD ("Product", Type.GetType ("System.String"));
TBLDATAS.COLUMNS.ADD ("Version", Type.GetType ("System.String"));
TBLDATAS.COLUMNS.ADD ("Description", Type.GetType ("System.String"));
TBLDATAS.ROWS.ADD (New Object[]{null, "A", "B", "C"});
TBLDATAS.ROWS.ADD (new object[] {null, "a", "B", "C"});
TBLDATAS.ROWS.ADD (new object[] {null, "a", "B", "C"});
TBLDATAS.ROWS.ADD (new object[] {null, "a", "B", "C"});
TBLDATAS.ROWS.ADD (new object[] {null, "a", "B", "C"});
Convert List<t> to DataTable
public static DataTable todatatable<t> (ilist<t> data)
{
PropertyDescriptorCollection properties = Typedescriptor.getproperties (typeof (T));
DataTable dt = new DataTable ();
for (int i = 0; i < properties. Count; i++)
{
PropertyDescriptor property = Properties[i];
Dt. Columns.Add (property. Name, property. PropertyType);
}
Object[] values = new object[properties. Count];
foreach (T item in data)
{
for (int i = 0; i < values. Length; i++)
{
Values[i] = properties[i]. GetValue (item);
}
Dt. Rows.Add (values);
}
return DT;
}
Datasets, DataTable, and DataGridView Knowledge memos