DataTable editing Method

Source: Internet
Author: User
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 );

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.