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. How can I set a value in a cell of the DataGridView that contains a DataGridViewComboBoxColumn?

How can I set a value in a cell of the DataGridView that contains a DataGridViewComboBoxColumn?

Scheduled Pinned Locked Moved C#
questionhelp
6 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.
  • E Offline
    E Offline
    Elvia Gonzalez
    wrote on last edited by
    #1

    Hi all! I have DataGridView whose sourse I set to a datatable with 4 columns containing data. I then remove a column, create a DataGridViewComboBoxColumn and insert this into the same location as the column that was removed. I want to set as the selected value in the combo box of every cell in the DataGridView a specific value I have stored in the column of the datable that I removed in the DataGridView. How can I set a value in a cell of the DataGridView that contains a DataGridViewComboBoxColumn? Thanks for your help, ~~Elvia

    E 1 Reply Last reply
    0
    • E Elvia Gonzalez

      Hi all! I have DataGridView whose sourse I set to a datatable with 4 columns containing data. I then remove a column, create a DataGridViewComboBoxColumn and insert this into the same location as the column that was removed. I want to set as the selected value in the combo box of every cell in the DataGridView a specific value I have stored in the column of the datable that I removed in the DataGridView. How can I set a value in a cell of the DataGridView that contains a DataGridViewComboBoxColumn? Thanks for your help, ~~Elvia

      E Offline
      E Offline
      Elvia Gonzalez
      wrote on last edited by
      #2

      I tried dataGridView1.Rows["RowName"].Cells["ColumnName"].Value = "1"; but it doesn’t work.

      G 1 Reply Last reply
      0
      • E Elvia Gonzalez

        I tried dataGridView1.Rows["RowName"].Cells["ColumnName"].Value = "1"; but it doesn’t work.

        G Offline
        G Offline
        goyal manish
        wrote on last edited by
        #3

        hi there first use findcontrol function for getting the control combobox then set its value. (controltype)dataGridView1.Rows["RowName"].Cells["ColumnName"].findcontrol("ControlID").value = "1"

        E 1 Reply Last reply
        0
        • G goyal manish

          hi there first use findcontrol function for getting the control combobox then set its value. (controltype)dataGridView1.Rows["RowName"].Cells["ColumnName"].findcontrol("ControlID").value = "1"

          E Offline
          E Offline
          Elvia Gonzalez
          wrote on last edited by
          #4

          Hi Goyal, Thanks a lot for the suggestion. I tried it, but it doesn't work in my case. I'm programming a Window application form. I'm using the namespace System.Windows.Forms and as far as I saw FindControl is for System.Web.UI . I think the solution is the one you gave me, but for web applications. So far I have been looking for something similar to your suggestion for a windows form, but I haven't succedded. Do you know another solution? Thank you, Elvia

          E 1 Reply Last reply
          0
          • E Elvia Gonzalez

            Hi Goyal, Thanks a lot for the suggestion. I tried it, but it doesn't work in my case. I'm programming a Window application form. I'm using the namespace System.Windows.Forms and as far as I saw FindControl is for System.Web.UI . I think the solution is the one you gave me, but for web applications. So far I have been looking for something similar to your suggestion for a windows form, but I haven't succedded. Do you know another solution? Thank you, Elvia

            E Offline
            E Offline
            Elvia Gonzalez
            wrote on last edited by
            #5

            Hi everybody! I have added items to the DataGridViewComboBoxColumn and the value that I want to set is one of the values in the items list. This is my code: DataGridViewComboBoxColumn cb = new DataGridViewComboBoxColumn(); cb = CreateComboBoxColumn("Category"); cb.Items.AddRange(new string[] { "Mr.", "Ms.", "Mrs.", "Dr." }); dataGridView.Columns.Insert(1, cb); dataGridView.Rows[0].Cells[1].Value = "Mr."; I want to see as the selected value in the combobox “Mr.”. Once I load the form the combobox doesn’t show a selected value. I have to drop the combobox to see the items. Any suggestion is very welcome, Elvia

            E 1 Reply Last reply
            0
            • E Elvia Gonzalez

              Hi everybody! I have added items to the DataGridViewComboBoxColumn and the value that I want to set is one of the values in the items list. This is my code: DataGridViewComboBoxColumn cb = new DataGridViewComboBoxColumn(); cb = CreateComboBoxColumn("Category"); cb.Items.AddRange(new string[] { "Mr.", "Ms.", "Mrs.", "Dr." }); dataGridView.Columns.Insert(1, cb); dataGridView.Rows[0].Cells[1].Value = "Mr."; I want to see as the selected value in the combobox “Mr.”. Once I load the form the combobox doesn’t show a selected value. I have to drop the combobox to see the items. Any suggestion is very welcome, Elvia

              E Offline
              E Offline
              Elvia Gonzalez
              wrote on last edited by
              #6

              Hi, I have been debugging my windows application. I have found that the code in the previous post is working as I expected, because I see the variable in the Watch window. I have the code of the previous post in the Load() method of the form class (FrmTestCases.cs) that has my DataGridView. But when the debugger leaves the file that has this code (FrmTestCases.cs) to return to the file that called this code (FrmExplorer.cs), the value that I set in that cell by the code is lost and set automatically to null. When the value was lost I set it in this watch window and the ComboxBox selected the value I set. I don't know whether the problem is because I removed and then added this column (see the first post). At the end of the Load() method I have this line: dataGridView.AutoGenerateColumns = false; if I don't use it, the column that I removed appears again. Any idea about why the value of the DataGridComboBoxColumn is changed to null automatically and how to solve it? Best Regards, Elvia

              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