C# - How can i get header row from datatable and arrange it vertically down a column?

17,271

Try

string[] columnNames = dt.Columns.Cast<DataColumn>()
                                 .Select(x => x.ColumnName)
                                 .ToArray();  
dataGridView1.DataSource = columnNames;

Or

dataGridView1.DataSource = dt.Columns.Cast<DataColumn>()
                                 .Select(x => x.ColumnName)
                                 .ToArray(); 
Share:
17,271
MRu
Author by

MRu

i'm technically new in programming with limited knowledge on IT. But i'm willing to learn. So bare with me please

Updated on June 11, 2022

Comments

  • MRu
    MRu almost 2 years

    How can i turn this datagridview from datatable enter image description here

    into this enter image description here

    Here is my code now. Any suggestion, comments, or sample code are highly appreciated. Thank you.

                DataSet result = excelReader.AsDataSet();
    
                excelReader.Close();
    
                if (result != null)
                {
                    DataTable dataTable = result.Tables[0];
                    List<string> headers = new List<string>();
                    foreach (DataColumn col in dataTable.Columns)
                    {
                        headers.Add(col.ColumnName);
                    }
                    dataGridView1.DataSource = dataTable;
                }