Shortest way syntaxically to add row datas to a datatable in .NET 2 and .NET 3+
I know I can do this
string id = "123";
string name = "John";
DataRow dataRow = dataTable.NewRow();
dataRow["Id"] = id;
dataRow["Name"] = name;
I succeeded also with
dataTable.Rows.Add("123");开发者_JAVA百科
only when there is one single column I can't see syntax for multiple columns:
dataTable.Rows.Add("123", "John");
What's the syntax I need to be compatible with .NET 2 but also interested to know for 3+
Yes, the third example you listed works for .NET 2 and above:
dataTable.Rows.Add("123","John");
Since .NET 2, the Add method that takes an object[]
has had the argument marked with the params
keyword, so there is no need to create an array; just pass in the values as separate arguments.
DataTable dtStudent = new DataTable();
//Add new column
dtStudent.Columns.AddRange(
new DataColumn[] { new DataColumn("SlNo", typeof(int)),
new DataColumn("RollNumber", typeof(string)),
new DataColumn("DateOfJoin", typeof(DateTime)),
new DataColumn("Place", typeof(string)),
new DataColumn("Course", typeof(string)),
new DataColumn("Remark", typeof(string))
});
// Add value to the related column
dtStudent.Rows.Add(1, "10001", DateTime.Now, "Bhubaneswar", "MCA", "Good");
精彩评论