Reputation: 141
DataTable table = new DataTable();
foreach (DataColumn column in dt.Columns)
{
table.Columns.Add(column.ColumnName, typeof(string));
}
I have one datable dt which have data I am creating one new Datatable and adding a column into that Datatable then my table looks like shown in below image
Now show in image column name as a number, fullname, address, date, and so on I also want to add that same column as data row but I don't know how can I add it is like data row.
And every time column name changes so when I am adding it on the Datatable table as column name I also need to add it is a data row. Please help me to do that
Upvotes: 0
Views: 1056
Reputation: 141
I found it myself using the below code
List<string> names = new List<string>();
DataTable table = new DataTable();
DataRow firstRow = table.NewRow();
foreach (DataColumn column in dt.Columns)
{
names.Add(column.ColumnName);
table.Columns.Add(column.ColumnName, typeof(string));
}
firstRow.ItemArray = names.ToArray();
table.Rows.InsertAt(firstRow, 0);
And it works as I want and thanks for the reply.
Upvotes: 1
Reputation: 334
You can try this type of logic:- First Add row after that in that rows add a column
DataTable table = new DataTable();
DataRow row = table.NewRow();
table.Rows.Add(number, fullname, address, address);
Upvotes: 0