I am working with a DataGridView
that has one column that is made of DataGridViewComboBoxCell
. The way my program populates these ComboBoxCells
is that it polls out local database for specific values and sets them up that way.
This I am having no problem with.
What I am having a problem with, is when I try to programmatically change which value in the ComboBoxCell is shown 'by Default'. I have 'by Default' in quotes because I am not really trying to change the Default value of the cell, just change the value that is displayed by the time the user sees the column added to the DataGridView
.
Currently I am getting the following error in my program:
System.ArgumentException: DataGridViewComboBoxCell value is not valid.
Now this is the code I am attempting to use to get this to work:
Dim dgvcc As New DataGridViewComboBoxCell
For Each row As DataRow In suppliers.Rows
dgvcc.Items.Add(row.Item("Supplier").ToString())
Next
dg.Item("Supplier", rowNum) = dgvcc
dg.Item("Supplier", rowNum).Value = dgvcc.Items.IndexOf(supplier)
A few notes about the above code:
suppliers
is a DataTable` object which holds the data retrieved from the databaserowNum
is just an Integer variable set up to be the row the data is being added todg
is myDataGridView
object
Now I have run this code with the debugger to confirm that yes, the value I am trying to get the index of in the last line of code does exist in the combo box list.
If I change the last line to:
dg.Item("Supplier", rowNum).Value = dgvcc.Items(0)
Then I don't get the error. So my issue seems to be with trying to set the value to a specific index, but I am not sure what about my code is wrong. Is there another way to set the current value of a DataGridViewComboBoxCell
other than what I am trying to do?
Note: While I am programming in VB.Net, I will accept C# answers as well :)