這篇文章主要介紹了C#編程實現(xiàn)DataTable添加行的方法,結(jié)合兩個實例形式分析了C#操作DataTable實現(xiàn)動態(tài)添加行的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了C#編程實現(xiàn)DataTable添加行的方法。分享給大家供大家參考,具體如下:
方法一:
- DataTable tblDatas = new DataTable("Datas");
- DataColumn dc = null;
- dc = tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));
- dc.AutoIncrement = true;//自動增加
- dc.AutoIncrementSeed = 1;//起始為1
- dc.AutoIncrementStep = 1;//步長為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"] = "水果刀";
- newRow["Version"] = "2.0";
- newRow["Description"] = "打架專用";
- tblDatas.Rows.Add(newRow);
- newRow = tblDatas.NewRow();
- newRow["Product"] = "折疊凳";
- newRow["Version"] = "3.0";
- newRow["Description"] = "行走江湖七武器之一";
- tblDatas.Rows.Add(newRow);
方法二:
- 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" });
希望本文所述對大家C#程序設(shè)計有所幫助。
新聞熱點
疑難解答