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

后端 未结 13 2395
星月不相逢
星月不相逢 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 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 );
    

提交回复
热议问题