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












1















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;
}









share|improve this question





























    1















    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;
    }









    share|improve this question



























      1












      1








      1








      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;
      }









      share|improve this question
















      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;
      }






      c# visual-studio visual-studio-2013 datagridview datatable






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jun 17 '16 at 4:23







      MRu

















      asked Jun 17 '16 at 4:14









      MRuMRu

      175319




      175319
























          2 Answers
          2






          active

          oldest

          votes


















          4














          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|improve this answer



















          • 1





            I think it returns the length of the column. But it did change to vertically down a column. Can i know. Is this what they call LINQ?

            – MRu
            Jun 17 '16 at 4:39











          • Yes, this is LINQ . will return ColumnName and if you bind with Grid, will be shown vertically as needed.

            – Sami
            Jun 17 '16 at 4:43











          • the columnNames did store the row values in array. But when i set it to dataGridView1.DataSource it shows the property of the array. Not the values in it. Can i know how i can solve this?

            – MRu
            Jun 17 '16 at 6:42



















          0














          Simply set the Header to be visible like this:



          DataGridView.ColumnHeaderVisible = true;



          This worked for me.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37873023%2fc-sharp-how-can-i-get-header-row-from-datatable-and-arrange-it-vertically-down%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            4














            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|improve this answer



















            • 1





              I think it returns the length of the column. But it did change to vertically down a column. Can i know. Is this what they call LINQ?

              – MRu
              Jun 17 '16 at 4:39











            • Yes, this is LINQ . will return ColumnName and if you bind with Grid, will be shown vertically as needed.

              – Sami
              Jun 17 '16 at 4:43











            • the columnNames did store the row values in array. But when i set it to dataGridView1.DataSource it shows the property of the array. Not the values in it. Can i know how i can solve this?

              – MRu
              Jun 17 '16 at 6:42
















            4














            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|improve this answer



















            • 1





              I think it returns the length of the column. But it did change to vertically down a column. Can i know. Is this what they call LINQ?

              – MRu
              Jun 17 '16 at 4:39











            • Yes, this is LINQ . will return ColumnName and if you bind with Grid, will be shown vertically as needed.

              – Sami
              Jun 17 '16 at 4:43











            • the columnNames did store the row values in array. But when i set it to dataGridView1.DataSource it shows the property of the array. Not the values in it. Can i know how i can solve this?

              – MRu
              Jun 17 '16 at 6:42














            4












            4








            4







            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|improve this answer













            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|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jun 17 '16 at 4:29









            SamiSami

            2,1412820




            2,1412820








            • 1





              I think it returns the length of the column. But it did change to vertically down a column. Can i know. Is this what they call LINQ?

              – MRu
              Jun 17 '16 at 4:39











            • Yes, this is LINQ . will return ColumnName and if you bind with Grid, will be shown vertically as needed.

              – Sami
              Jun 17 '16 at 4:43











            • the columnNames did store the row values in array. But when i set it to dataGridView1.DataSource it shows the property of the array. Not the values in it. Can i know how i can solve this?

              – MRu
              Jun 17 '16 at 6:42














            • 1





              I think it returns the length of the column. But it did change to vertically down a column. Can i know. Is this what they call LINQ?

              – MRu
              Jun 17 '16 at 4:39











            • Yes, this is LINQ . will return ColumnName and if you bind with Grid, will be shown vertically as needed.

              – Sami
              Jun 17 '16 at 4:43











            • the columnNames did store the row values in array. But when i set it to dataGridView1.DataSource it shows the property of the array. Not the values in it. Can i know how i can solve this?

              – MRu
              Jun 17 '16 at 6:42








            1




            1





            I think it returns the length of the column. But it did change to vertically down a column. Can i know. Is this what they call LINQ?

            – MRu
            Jun 17 '16 at 4:39





            I think it returns the length of the column. But it did change to vertically down a column. Can i know. Is this what they call LINQ?

            – MRu
            Jun 17 '16 at 4:39













            Yes, this is LINQ . will return ColumnName and if you bind with Grid, will be shown vertically as needed.

            – Sami
            Jun 17 '16 at 4:43





            Yes, this is LINQ . will return ColumnName and if you bind with Grid, will be shown vertically as needed.

            – Sami
            Jun 17 '16 at 4:43













            the columnNames did store the row values in array. But when i set it to dataGridView1.DataSource it shows the property of the array. Not the values in it. Can i know how i can solve this?

            – MRu
            Jun 17 '16 at 6:42





            the columnNames did store the row values in array. But when i set it to dataGridView1.DataSource it shows the property of the array. Not the values in it. Can i know how i can solve this?

            – MRu
            Jun 17 '16 at 6:42













            0














            Simply set the Header to be visible like this:



            DataGridView.ColumnHeaderVisible = true;



            This worked for me.






            share|improve this answer




























              0














              Simply set the Header to be visible like this:



              DataGridView.ColumnHeaderVisible = true;



              This worked for me.






              share|improve this answer


























                0












                0








                0







                Simply set the Header to be visible like this:



                DataGridView.ColumnHeaderVisible = true;



                This worked for me.






                share|improve this answer













                Simply set the Header to be visible like this:



                DataGridView.ColumnHeaderVisible = true;



                This worked for me.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 31 '18 at 18:56









                Kovacs LaszloKovacs Laszlo

                1




                1






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37873023%2fc-sharp-how-can-i-get-header-row-from-datatable-and-arrange-it-vertically-down%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas