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. Adding datagridview columns

Adding datagridview columns

Scheduled Pinned Locked Moved C#
questiondata-structureshelptutorial
7 Posts 2 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.
  • M Offline
    M Offline
    michaelgr1
    wrote on last edited by
    #1

    Hello, I need to add datagridviewcolumns in a loop. For example:

    string[] default_columns { "VlFile", "EquipName", "JobName", "NickName", "Operator", "EquipmentStartTime", "JobComment", "EquipTypeDesc" };
    foreach (string s in default_columns)
    {
    //Here i need to add the a column with the name in S
    }

    The columns need to be bounded. I have the bound source and all this but the problem is adding the columns. It will loop me through the array (default_columns) and will add this columns. How can i do it? I mean there is a problem because i need to declare a variable

    M 1 Reply Last reply
    0
    • M michaelgr1

      Hello, I need to add datagridviewcolumns in a loop. For example:

      string[] default_columns { "VlFile", "EquipName", "JobName", "NickName", "Operator", "EquipmentStartTime", "JobComment", "EquipTypeDesc" };
      foreach (string s in default_columns)
      {
      //Here i need to add the a column with the name in S
      }

      The columns need to be bounded. I have the bound source and all this but the problem is adding the columns. It will loop me through the array (default_columns) and will add this columns. How can i do it? I mean there is a problem because i need to declare a variable

      M Offline
      M Offline
      MickCurley
      wrote on last edited by
      #2

      I would create a datatable and point the datagridview to it

      DataTable DT = new DataTable();

      foreach (string s in default_columns)
      {
      DataColumn NewCol = new DataColumn(s);
      DT.Columns.Add(NewCol);
      }

      DataGridView.DataSource = DT;

      Regards Mick Curley :)

      M 1 Reply Last reply
      0
      • M MickCurley

        I would create a datatable and point the datagridview to it

        DataTable DT = new DataTable();

        foreach (string s in default_columns)
        {
        DataColumn NewCol = new DataColumn(s);
        DT.Columns.Add(NewCol);
        }

        DataGridView.DataSource = DT;

        Regards Mick Curley :)

        M Offline
        M Offline
        michaelgr1
        wrote on last edited by
        #3

        OK but i load data to the datagridview from a database. And i need the columns to be bound to the database. I declared the database (datatable) as a bindingsource to the datagridview. But i need to add columns in the program (the user will choose which columns he wants to see)

        M 1 Reply Last reply
        0
        • M michaelgr1

          OK but i load data to the datagridview from a database. And i need the columns to be bound to the database. I declared the database (datatable) as a bindingsource to the datagridview. But i need to add columns in the program (the user will choose which columns he wants to see)

          M Offline
          M Offline
          MickCurley
          wrote on last edited by
          #4

          I dont understand what your trying to do. If you load the data from your Database table into a DataTable haven't you got a reference to the columns within that DataTable?

          Regards Mick Curley :)

          M 1 Reply Last reply
          0
          • M MickCurley

            I dont understand what your trying to do. If you load the data from your Database table into a DataTable haven't you got a reference to the columns within that DataTable?

            Regards Mick Curley :)

            M Offline
            M Offline
            michaelgr1
            wrote on last edited by
            #5

            I have an empty datagridview (without columns declared- in the wizard of the datagridview). The user choses which columns he wants to see from the database (using a listbox that i save the items in an array). I want to add columns to the datagridview from this array (that was created from the listbox). The problem is that i need them to be bound to that datatable. So i do in the foreach loop datagridview.Columns.Add(s, s); it creates me the columns right but they are not filled with data from the database. If i do: datagridviewcolumn col=new datagridviewtextboxcolumn(); col.headertext="abc"; col.datapropertyname="abc"; col.name="abc"; It does creates me the column and filles it with data. But i need to do it in a loop that will go through all the items in the array (from the listbox) and add this columns But i can use the same name to the datagridviewcolumn (datagridviewcolumn col=new datagridviewtextboxcolumn();), Because if i do it it says i already have this column (even if i change it's name in the loop)

            M 1 Reply Last reply
            0
            • M michaelgr1

              I have an empty datagridview (without columns declared- in the wizard of the datagridview). The user choses which columns he wants to see from the database (using a listbox that i save the items in an array). I want to add columns to the datagridview from this array (that was created from the listbox). The problem is that i need them to be bound to that datatable. So i do in the foreach loop datagridview.Columns.Add(s, s); it creates me the columns right but they are not filled with data from the database. If i do: datagridviewcolumn col=new datagridviewtextboxcolumn(); col.headertext="abc"; col.datapropertyname="abc"; col.name="abc"; It does creates me the column and filles it with data. But i need to do it in a loop that will go through all the items in the array (from the listbox) and add this columns But i can use the same name to the datagridviewcolumn (datagridviewcolumn col=new datagridviewtextboxcolumn();), Because if i do it it says i already have this column (even if i change it's name in the loop)

              M Offline
              M Offline
              michaelgr1
              wrote on last edited by
              #6

              I think i managed the problem. I had to declare the datagridviewcolumn (datagridviewcolumn col=new datagridviewtextboxcolumn();) inside the foreach loop (i declared it outside) and in this way it declares a ned datagridviewcolumn for each item in the loop.

              M 1 Reply Last reply
              0
              • M michaelgr1

                I think i managed the problem. I had to declare the datagridviewcolumn (datagridviewcolumn col=new datagridviewtextboxcolumn();) inside the foreach loop (i declared it outside) and in this way it declares a ned datagridviewcolumn for each item in the loop.

                M Offline
                M Offline
                MickCurley
                wrote on last edited by
                #7

                Good to hear..

                Regards Mick Curley :)

                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