C#datatable operation

Source: Internet
Author: User

C # Operations DataTable

1. Create a table

DataSet ds=new DataSet ();

DataTable dt=new DataTable ("User");

Ds. ADD (DT);

Dt. Columns.Add ("user_name", typeof (String));

Dt. Columns.Add ("User_password", typeof (String));

Dt. Columns.Add ("User_page", typeof (int));

2. Add data to the table

DataRow Dr=dt. NewRow ();

Dr[0]= "Zhangsan";

Dr[1]= "123456";

dr[2]=25;

Dt. Rows.Add (DR);


3. Modify the data

Datagridview1.datasource = ds. Tables[0];
DataTable dt = ds. tables["User"];
int i = DataGridView1.CurrentRow.Index;
DataRow dr = dt. Rows[i];
Dr. BeginEdit ();
Dr[0] = This.textBox1.Text;
DR[1] = This.textBox2.Text;
DR[2] = This.textBox3.Text;
Dr. EndEdit ();

4. Delete data

Datagridview1.datasource = ds. Tables[0];
DataTable dt = ds. tables["User"];
int i = datagridview1.currentrow.index;//get when forward
DataRow dr = dt. Rows[i];
Dt. Rows.remove (DR);//second Delete operation usage
Dr. Delete ();

C # Operations DataTable 2
2008-05-26 16:43:18

C # Operations DataTable

Create a DataTable
DataTable dt = new DataTable ("King");

       //Add Column Method 1
       //Add a column column named ID , the type is string
        dt. Columns.Add ("id", System.Type.GetType ("System.String"));//create a column directly for the table
        //Add Column Method
       //Add a column column named FolderName, type string
         DataColumn dc = new DataColumn ("foldername", System.Type.GetType ("System.String"));// Defines a column schema
        dt. Columns.Add (DC);//Insert column schema into table

       //Add Line Method
       //Add rows with data
        DataRow dr = dt. NewRow ();//Create new column
        dr["id"] = Guid.NewGuid (). ToString ();//Set column values by column name
        dr[1] = "Default business card Folder";//Set column value, through column index, starting with 0
         dt. Rows.Add (DR);//Add data to the table
       //Add blank lines
         DataRow dr1 = dt. NewRow ();//Create new column
        dt. Rows.Add (DR1);//To add data to the table

Find rows
datarow[] DRS = dt. Select ("ID is null");
Response.Write (Drs. Length.tostring () + "<br/>");
datarow[] drs1 = dt. Select ("ID is not NULL");
Response.Write (drs1. Length.tostring () + "<br/>");
Response.Write (Drs1[0]. Itemarray[0] + "_" + drs1[0]. ITEMARRAY[1] + "<br/>");//Display data

Action Line
DataRow cdr = dt. ROWS[1];
Cdr[0] = Guid.NewGuid (). ToString ();
CDR[1] = "Network 04g1";
Response.Write (Cdr. Itemarray[0] + "_" + cdr. ITEMARRAY[1] + "<br/>");//Display data

Sort
Response.Write (dt. Rows[0][0] + "|" + dt. Rows[0][1] + "_" + dt. Rows[1][0] + "|" + dt. rows[1][1]+ "<br>");
Dt. Defaultview.sort = "id desc,foldername";
DT = dt. Defaultview.totable ();
Response.Write (dt. Rows[0][0] + "|" + dt. Rows[0][1] + "_" + dt. Rows[1][0] + "|" + dt. ROWS[1][1]);

Asp. NET Basic Tutorial-datatable, DataRow, DataColumn objects use 3

Create and declare a DataTable object DataTable total=new DataTable ();
Add the DataColumn object Total.Columns.Add (New DataColumn ("Team", typeof (String)) to the DataTable object;
Total. Columns.Add (New DataColumn ("name", typeof (String)));
Total. Columns.Add (New DataColumn ("Subject", typeof (String)));
Total. Columns.Add (New DataColumn ("Score", typeof (int)));//Create and Declare DataRow object DataRow dr=total. NewRow ();//Assign values to the columns in the DataRow object dr["team"]= "one Shift";
dr["name"]= "Li Hong";
dr["Subject"]= "language";
dr["Achievement"]=98;
Add a DataRow object to the DataTable Total.Rows.Add (DR);d R=total. NewRow ();
dr["Team"]= "one Class";
dr["name"]= "Liu Jingsheng";
dr["Subject"]= "language";
dr["Achievement"]=96;
Total. Rows.Add (DR);//output data from a DataTable object on a page
for (int i=0;i<total. rows.count;i++)
{
Response.Write (total. Rows[i][0]. ToString () + "<br>");
Response.Write (total. ROWS[I][1]. ToString () + "<br>");
Response.Write (total. ROWS[I][2]. ToString () + "<br>");
Response.Write (total. ROWS[I][3]. ToString () + "<br>");
}c# Operation Datatbale 3:

DataTable memtable = new DataTable ("TableName");
MEMTABLE.COLUMNS.ADD (New DataColumn ("ID", typeof (int)));
MEMTABLE.COLUMNS.ADD (New DataColumn ("Username", typeof (String));
MEMTABLE.COLUMNS.ADD (New DataColumn ("Password", typeof (Guid));

DataRow row = Memtable.newrow ();
row["ID"] = 1;
row["Username"] = "badbug";
row["Password"] = Guid.NewGuid ();

MEMTABLE.ROWS.ADD (row);

C#datatable operation

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.