How can I set a value in a cell of the DataGridView that contains a DataGridViewComboBoxColumn?
-
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
-
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
I tried dataGridView1.Rows["RowName"].Cells["ColumnName"].Value = "1"; but it doesn’t work.
-
I tried dataGridView1.Rows["RowName"].Cells["ColumnName"].Value = "1"; but it doesn’t work.
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"
-
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"
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
-
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
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
-
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
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