How to create a DataTable in C# and how to add rows?

后端 未结 13 2294
星月不相逢
星月不相逢 2020-11-28 01:35

How do create a DataTable in C#?

I did like this:

 DataTable dt = new DataTable();
 dt.clear();
 dt.Columns.Add(\"Name\");
 dt.Columns.Add(\"Marks\")         


        
相关标签:
13条回答
  • 2020-11-28 01:59

    Create DataTable:

    DataTable MyTable = new DataTable(); // 1
    DataTable MyTableByName = new DataTable("MyTableName"); // 2
    

    Add column to table:

     MyTable.Columns.Add("Id", typeof(int));
     MyTable.Columns.Add("Name", typeof(string));
    

    Add row to DataTable method 1:

    DataRow row = MyTable.NewRow();
    row["Id"] = 1;
    row["Name"] = "John";
    MyTable.Rows.Add(row);
    

    Add row to DataTable method 2:

    MyTable.Rows.Add(2, "Ivan");
    

    Add row to DataTable method 3 (Add row from another table by same structure):

    MyTable.ImportRow(MyTableByName.Rows[0]);
    

    Add row to DataTable method 4 (Add row from another table):

    MyTable.Rows.Add(MyTable2.Rows[0]["Id"], MyTable2.Rows[0]["Name"]);
    

    Add row to DataTable method 5 (Insert row at an index):

    MyTable.Rows.InsertAt(row, 8);
    
    0 讨论(0)
  • 2020-11-28 02:02
    DataTable dt=new DataTable();
    Datacolumn Name = new DataColumn("Name");
    Name.DataType= typeoff(string);
    Name.AllowDBNull=false; //set as null or not the default is true i.e null
    Name.MaxLength=20; //sets the length the default is -1 which is max(no limit)
    dt.Columns.Add(Name);
    Datacolumn Age = new DataColumn("Age", typeoff(int));`
    
    dt.Columns.Add(Age);
    
    DataRow dr=dt.NewRow();
    
    dr["Name"]="Mohammad Adem"; // or dr[0]="Mohammad Adem";
    dr["Age"]=33; // or dr[1]=33;
    dt.add.rows(dr);
    dr=dt.NewRow();
    
    dr["Name"]="Zahara"; // or dr[0]="Zahara";
    dr["Age"]=22; // or dr[1]=22;
    dt.rows.add(dr);
    Gv.DataSource=dt;
    Gv.DataBind();
    
    0 讨论(0)
  • 2020-11-28 02:03

    The easiest way is to create a DtaTable as of now

    DataTable table = new DataTable
    {
        Columns = {
            "Name", // typeof(string) is implied
            {"Marks", typeof(int)}
        },
        TableName = "MarksTable" //optional
    };
    table.Rows.Add("ravi", 500);
    
    0 讨论(0)
  • 2020-11-28 02:03

    In addition to the other answers.

    If you control the structure of the DataTable there is a shortcut for adding rows:

    // Assume you have a data table defined as in your example named dt dt.Rows.Add("Name", "Marks");

    The DataRowCollection.Add() method has an overload that takes a param array of objects. This method lets you pass as many values as needed, but they must be in the same order as the columns are defined in the table.

    So while this is a convenient way to add row data, it can be risky to use. If the table structure changes your code will fail.

    0 讨论(0)
  • 2020-11-28 02:07
    // Create a DataTable and add two Columns to it
    DataTable dt=new DataTable();
    dt.Columns.Add("Name",typeof(string));
    dt.Columns.Add("Age",typeof(int));
    
    // Create a DataRow, add Name and Age data, and add to the DataTable
    DataRow dr=dt.NewRow();
    dr["Name"]="Mohammad"; // or dr[0]="Mohammad";
    dr["Age"]=24; // or dr[1]=24;
    dt.Rows.Add(dr);
    
    // Create another DataRow, add Name and Age data, and add to the DataTable
    dr=dt.NewRow();
    dr["Name"]="Shahnawaz"; // or dr[0]="Shahnawaz";
    dr["Age"]=24; // or dr[1]=24;
    dt.Rows.Add(dr);
    
    // DataBind to your UI control, if necessary (a GridView, in this example)
    GridView1.DataSource=dt;
    GridView1.DataBind();
    
    0 讨论(0)
  • 2020-11-28 02:07

    You have to add datarows to your datatable for this.

    // Creates a new DataRow with the same schema as the table.
    DataRow dr = dt.NewRow();
    
    // Fill the values
    dr["Name"] = "Name";
    dr["Marks"] = "Marks";
    
    // Add the row to the rows collection
    dt.Rows.Add ( dr );
    
    0 讨论(0)
提交回复
热议问题