user10753256
user10753256

Reputation: 141

Add same row and column to datable

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

enter image description here

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

Answers (2)

user10753256
user10753256

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

A.M. Patel
A.M. Patel

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

Related Questions