1

I tried to add values to DataTable columns by vertically in loops.

But output doesn't looks good as expected.

DataTables dynamictable = new DataTable()
foreach (DataColumn cl in dataTable.Columns)
{
   dynamictable.Columns.Add(cl.ToString());
   List<string> plainList = dataTable.AsEnumerable().Select(x => x[cl].ToString()).ToList();

   for (int i=0; i< plainList.Count(); i++)
   {
       DataRow row = dynamictable.NewRow();
       row[cl.ToString()] = plainList[i];   
       enryptedTable.Rows.Add(row);
   }
}

Actual Output got from my code:

enter image description here

Expected Output:

enter image description here

1

1 Answer 1

2

change your code to :

DataTable dynamictable = new DataTable();
int col = 0;
foreach (DataColumn cl in dataTable.Columns)
{
    dynamictable.Columns.Add(cl.ToString());
    List<string> plainList = dataTable.AsEnumerable().Select(x => x[cl].ToString()).ToList();
    DataRow row = null;
    for (int i = 0; i < plainList.Count(); i++)
    {
       row = dynamictable.NewRow();
       row[col] = plainList[i];
       dynamictable.Rows.Add(row);
    }
    col++;
}
//now set dynamictable to datagridview or .....
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.