Is there any way to create a datatable with static value let say
Column : col1,col2.....coln
Rows : for n number of rows
For first row values row11,row12......row1n
for second row values row21,row22.....row2n
.
.
.
for nth row values rown1,rown2......rownn
Any stylish way to do it using lambda/linq...may be in a single line in a expertise way ..
I know it is an un-cool question ("outdated technology" - John Saunders) but I can't resist:
const int cols = 6;
const int rows = 20;
DataTable nt = new DataTable("new table");
nt.Columns
.AddRange(
Enumerable
.Range(1,cols)
.Select(x => new DataColumn("col"+x.ToString())).ToArray());
Enumerable
.Range(1,rows).ToList()
.ForEach(x => nt.Rows
.Add(
Enumerable
.Range(1,cols)
.Select(y => "row"+x.ToString()+"col"+y.ToString()).ToArray()));
Sorry about the strange formatting but I did not want a horizontal scroll bar.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments