Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Data Grid View Column Headers

Data Grid View Column Headers

Scheduled Pinned Locked Moved C#
helpcss
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jasper018
    wrote on last edited by
    #1

    Hello, I have a data grid view, and what I am trying to do is, grab all the column headers and put them into a combo box or list box. what I have so far is: foreach (DataColumn column in this.dataGridView1.Columns) { this.listBox1.Items.Add(column.ToString()); } I get an error with this, it is: "Unable to cast object of type 'System.Windows.Forms.DataGridViewTextBoxColumn' to type 'System.Data.DataColumn'" Any help would be apreciated. Thanks J

    F S 2 Replies Last reply
    0
    • J jasper018

      Hello, I have a data grid view, and what I am trying to do is, grab all the column headers and put them into a combo box or list box. what I have so far is: foreach (DataColumn column in this.dataGridView1.Columns) { this.listBox1.Items.Add(column.ToString()); } I get an error with this, it is: "Unable to cast object of type 'System.Windows.Forms.DataGridViewTextBoxColumn' to type 'System.Data.DataColumn'" Any help would be apreciated. Thanks J

      F Offline
      F Offline
      FernandoMartin
      wrote on last edited by
      #2

      you can try: foreach (DataColumn column in this.dataGridView1.Columns) { this.listBox1.Items.Add(column.HeaderText); }

      J 2 Replies Last reply
      0
      • J jasper018

        Hello, I have a data grid view, and what I am trying to do is, grab all the column headers and put them into a combo box or list box. what I have so far is: foreach (DataColumn column in this.dataGridView1.Columns) { this.listBox1.Items.Add(column.ToString()); } I get an error with this, it is: "Unable to cast object of type 'System.Windows.Forms.DataGridViewTextBoxColumn' to type 'System.Data.DataColumn'" Any help would be apreciated. Thanks J

        S Offline
        S Offline
        Sebastien Lachance
        wrote on last edited by
        #3

        this.dataGridView1.Columns returns a collection of GridViewColumn, not DataColumn. In this case, the column returned is a specialized version of GridViewColumn which contains a textbox. foreach (GridViewColumn in this.dataGridView1.Columns) { this.listBox1.Items.Add(column.HeaderText); }

        My Blog[^]

        J 1 Reply Last reply
        0
        • F FernandoMartin

          you can try: foreach (DataColumn column in this.dataGridView1.Columns) { this.listBox1.Items.Add(column.HeaderText); }

          J Offline
          J Offline
          jasper018
          wrote on last edited by
          #4

          Thank you for your help!

          1 Reply Last reply
          0
          • S Sebastien Lachance

            this.dataGridView1.Columns returns a collection of GridViewColumn, not DataColumn. In this case, the column returned is a specialized version of GridViewColumn which contains a textbox. foreach (GridViewColumn in this.dataGridView1.Columns) { this.listBox1.Items.Add(column.HeaderText); }

            My Blog[^]

            J Offline
            J Offline
            jasper018
            wrote on last edited by
            #5

            Thank you for your help, I added a identifyer, and it worked great! Thanks. Here is the end result. foreach (DataGridViewColumn column in this.dataGridView1.Columns) { this.listBox1.Items.Add(column.HeaderText); }

            S 1 Reply Last reply
            0
            • F FernandoMartin

              you can try: foreach (DataColumn column in this.dataGridView1.Columns) { this.listBox1.Items.Add(column.HeaderText); }

              J Offline
              J Offline
              jasper018
              wrote on last edited by
              #6

              Thanks... The end result that I ended up with is: foreach (DataGridViewColumn column in this.dataGridView1.Columns) { this.listBox1.Items.Add(column.HeaderText); }

              1 Reply Last reply
              0
              • J jasper018

                Thank you for your help, I added a identifyer, and it worked great! Thanks. Here is the end result. foreach (DataGridViewColumn column in this.dataGridView1.Columns) { this.listBox1.Items.Add(column.HeaderText); }

                S Offline
                S Offline
                Sebastien Lachance
                wrote on last edited by
                #7

                You're welcome :-D

                My Blog[^]

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups