The Method of Adding Rows to DataTable by C Programming

  • 2021-08-28 20:52:51
  • OfStack

In this paper, an example is given to describe the method of adding rows to DataTable by C # programming. Share it for your reference, as follows:

Method 1:


DataTable tblDatas = new DataTable("Datas");
DataColumn dc = null;
dc = tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));
dc.AutoIncrement = true;// Automatic increment 
dc.AutoIncrementSeed = 1;// Start with 1
dc.AutoIncrementStep = 1;// Step length is 1
dc.AllowDBNull = false;//
dc = tblDatas.Columns.Add("Product", Type.GetType("System.String"));
dc = tblDatas.Columns.Add("Version", Type.GetType("System.String"));
dc = tblDatas.Columns.Add("Description", Type.GetType("System.String"));
DataRow newRow;
newRow = tblDatas.NewRow();
newRow["Product"] = " Fruit knife ";
newRow["Version"] = "2.0";
newRow["Description"] = " Special for fighting ";
tblDatas.Rows.Add(newRow);
newRow = tblDatas.NewRow();
newRow["Product"] = " Folding stool ";
newRow["Version"] = "3.0";
newRow["Description"] = " Walk the rivers and lakes 7 Weapons 1";
tblDatas.Rows.Add(newRow);

Method 2:


DataTable tblDatas = new DataTable("Datas");
tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));
tblDatas.Columns[0].AutoIncrement = true;
tblDatas.Columns[0].AutoIncrementSeed = 1;
tblDatas.Columns[0].AutoIncrementStep = 1;
tblDatas.Columns.Add("Product", Type.GetType("System.String"));
tblDatas.Columns.Add("Version", Type.GetType("System.String"));
tblDatas.Columns.Add("Description", Type.GetType("System.String"));
tblDatas.Rows.Add(new object[]{null,"a","b","c"});
tblDatas.Rows.Add(new object[] { null, "a", "b", "c" });
tblDatas.Rows.Add(new object[] { null, "a", "b", "c" });
tblDatas.Rows.Add(new object[] { null, "a", "b", "c" });
tblDatas.Rows.Add(new object[] { null, "a", "b", "c" });

I hope this article is helpful to everyone's C # programming.


Related articles: