Datasets, DataTable, and DataGridView Knowledge memos

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.