C # Dynamic Operation DataTable (new row, column, query row, column, etc.)

Source: Internet
Author: User

Method One: Dynamically create a DataTable and add data to it

public void CreateTable ()
{
Create a table
DataTable dt = new DataTable ();

1. Add columns
Dt. Columns.Add ("Name", typeof (String)); Data type is text

2. Adding columns through the column schema
DataColumn age = New DataColumn ("Age", typeof (Int32)); Data type is shaping
DataColumn time = new DataColumn ("Time", typeof (DateTime)); Data type is time
Dt. Columns.Add (age);
Dt. Columns.Add (time);

1. Add Blank lines
DataRow DR1 = dt. NewRow ();
Dt. Rows.Add (DR1);

2. Add Blank Lines
Dt. Rows.Add ();

3. Adding data rows
DataRow DR2 = dt. NewRow ();
Dr2[0] = "Zhang San"; Assigning values by index
DR2[1] = 23;
dr2["Time"] = datetime.now;//Assignment by name
Dt. Rows.Add (DR2);

4. Add by line Frame
Dt. Rows.Add ("John Doe", 25,datetime.now);//add the data order of your parameters corresponds to the column in DT

}

Method Two: Add a new column for an existing datetable, its value can be set to the default value, or the column can be set to not be empty.

public void CreateTable (DataTable vTable)
{
Add a new column to an existing DataTable
DataColumn DC1 = new DataColumn ("Tol", typeof (String));
VTABLE.COLUMNS.ADD (DC1);

Add a new column whose value is the default value
DataColumn DC2 = new DataColumn ("Sex", typeof (String));
DC2. DefaultValue = "male";
DC2. AllowDBNull = false;//This is at the beginning of the table, its role, when added to the existing table column, does not work
VTABLE.COLUMNS.ADD (DC2);
}

Method Three: Filter data in the DataTable, using the Select () method, the results can be saved to datarow[] Drarr; Data, you can save it as a new DataTable

public void selectrowdatatable ()
{
DataTable dt = new DataTable ();//Assuming that DT is a result of "select C1,C2,C3 from T1" Query
for (int i = 0; i < dt. Rows.Count; i++)
{
if (dt. rows[i]["C1"]. ToString () = = "abc")//Query condition
{
To operate
}
}
But this practice with one or two times fortunately, with more tired. Is there a better way to do that? Is dt. Select (), the above operation can be changed to this:

datarow[] Drarr = dt. Select ("c1= ' abc");//query (if all data is queried unconditionally in select)

You can also do this:
datarow[] drArr1 = dt. Select ("C1 like ' abc% '");//Fuzzy query (if the multi-criteria filter, you can add and OR OR)
datarow[] drArr2 = dt. Select ("' abc ' like C1 + '% '", "C2 DESC");//Another method of fuzzy query
datarow[] drArr3 = dt. Select ("c1= ' abc '", "C2 DESC");//Sort

Again, if you want to assign a DataRow to a new DataTable, how do you assign a value? You might think:
DataTable dtNew1 = dt. Clone ();
for (int i = 0; i < drarr.length; i++)
{
DTNEW1.ROWS.ADD (Drarr[i]);
}

But this procedure will be wrong, said the DataRow belongs to other DataTable, how to do it? Very simple, so it can be solved:
DataTable dtNew2 = dt. Clone ();
for (int i = 0; i < drarr.length; i++)
{
Dtnew2.importrow (Drarr[i]);//importrow is a copy.
}
}

Method Four: Filter the specified field to the DataTable and save as a new table

public void selectcolumndatatable (DataTable dt)
{
Filter the specified field to a DataTable and save as a new table
DataTable dtnew = dt. Defaultview.totable (False, new string[] {"Column name", "Column Name", "Column Name"});//These column names, ensure that the DT exists, otherwise it will report an error
}

Method Five: Sort the datatable by setting (sort)

public void sortdatatable (DataTable dt)
{
Dt. Defaultview.sort = "id desc";//Reset Sort
DataTable dtnew = dt. Defaultview.totable (); Save in a new table
}

C # Dynamic Operation DataTable (new row, column, query row, column, etc.)

Related Article

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.