Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
            }
like image 987
MRu Avatar asked Dec 24 '22 04:12

MRu


1 Answers

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(); 
like image 165
Sami Avatar answered Dec 31 '22 14:12

Sami