-1

I'm using VB.NET to make a stand-alone executable containing an unbounded DataGridView.

One of its columns is a combobox. I add rows to the combobox with some simple code like this:

 For r As Int16 = 0 To eachLine.GetUpperBound(0)
      Dim dgvcbc As DataGridViewComboBoxColumn = grd.Columns(col)
      ' Errors: dgvcbc.Items.Clear()
      dgvcbc.Items.Add(eachLine(r))
 Next r

Works great the first time, but when I try to clear it, in order to add some different items in the combobox, I get this error 100s of times:

> DataGridViewComboBoxCell value is not valid

Any ideas on how to fix this and why it occurs?

Bakchod Guru
  • 51
  • 1
  • 6
  • 1
    If it's a cell, you have to cast it as a datagridviewcomboboxcell. .. – Trevor Jul 06 '14 at 00:28
  • How would I "cast it" and "what" would I cast? I have no idea if I should be working with a "cell" or a "column". How you YOU clear a combobox... and then add new rows to the combobox? – Bakchod Guru Jul 06 '14 at 00:39
  • 1
    That's the point, I can't figure it out from here. Are you talking about a cell or column, if it's a cell do what I said above. Also from the looks of your other questions you have asked and got answers, you haven't accepted any of them. It is a good idea to mark it and vote if it's helped you, otherwise you won't get any help... – Trevor Jul 06 '14 at 01:30
  • *HOW* do I do what you said? In my example... it's a datagridview. It contains rows and columns. Cells. And the cell has a ComboBox in it. How do I clear it... and put 10 values into the dropdown? – Bakchod Guru Jul 06 '14 at 02:10
  • 2
    You need to look at other questions you have asked and Mark them if they helped you, then maybe I can help you as they did you. – Trevor Jul 06 '14 at 02:20

1 Answers1

0

The issue is that, if you clear the Items in that column, every cell in that column becomes invalid. Every value in that column has to match one of the items in the drop-down list and, if there's no items, no value can be valid. It doesn't make sense to clear the drop-down list. You must leave at least the items that match the values in the cells of that column.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • This isn't a well constructed answer, but more of statement/comment. – Trevor Jul 06 '14 at 07:04
  • 1
    @MrCoDeXeR, the question was "Any ideas on how to fix this and why it occurs". I think that my answer addresses those two points specifically. – jmcilhinney Jul 06 '14 at 09:06
  • What your answer was is common sense. Also have you tried your solution or I should say your proposal... what are you talking about drop down list, if you clear the items there won't be a list. You said, "you must leave at least the items that match the values in the cells of that column", in his case it doesn't matter what they are as he's not doing any comparison, so it doesn't matter. Would you mind showing an example to clarify your solution? – Trevor Jul 06 '14 at 23:30
  • Also it would help the OP to understand by showing an example as well as anyone else comes across it. – Trevor Jul 06 '14 at 23:33
  • The real issue here, is he's not casting the cells to the correct type and not setting each cell's data source... – Trevor Jul 06 '14 at 23:47
  • @MrCoDeXeR, that is not the real issue at all. Obviously casting is a good idea but not required with `Option Strict Off`, which it must be if the code compiles without a cast. Also, while binding data to the column would be a good idea, it's certainly not required either, as populating the `Items` collection can be done manually, just as for a `ComboBox`. The real issue is that the error message "DataGridViewComboBoxCell value is not valid" is being caused by the fact that the `Value` of a cell is not represented in the drop-down list for that cell. That's the issue the question is about. – jmcilhinney Jul 07 '14 at 00:48
  • @MrCoDeXeR, as for an example, you really want an example of NOT removing items from a drop-down list? – jmcilhinney Jul 07 '14 at 00:49
  • It's a good practice to have option strict on and I would say many programmers would agree. Also he's clearly doing it wrong, not using a datagridviewcomboboxcell. Also there's a property in the datagridview for each column for default value. If he sets that I bet his error goes away, unless he is I need trying to set a value for a specific cell. – Trevor Jul 07 '14 at 01:02
  • And yes I would like to see an example of adding comboboxes to the grid... and clear them out and instantiate them again with different values. – Trevor Jul 07 '14 at 01:04
  • I work with this stuff on a daily basis, nothing new to me. I have an answer, but it's not worth my time if the OP doesn't recognize other answers given to him. – Trevor Jul 07 '14 at 01:06