DataGridViewComboBoxCell Errors in Visual Studio 2005

Another fairly hard-core post on .Net today, so if you don’t program in C# .Net, you can probably ignore this! Recently, I have started playing with integrating databases into C# for the first time. I’ve done a little bit of data binding before, but I’ve never used the DataGridViewControl until this week.

I’ve been doing what I would consider to be very simple stuff, but had hit a brick wall recently that has sucked up hours of my time. It’s times like this that I really miss having colleagues and friends who are working on the same technologies as me, because I’m sure that anyone who’s used ADO and this control for more than a little while will probably know all about how to fix it and so on.

Anyway, I was developing a ‘lookup’ field from a grid; that is, the underlying data required a numeric id to identify another record; but no-one can remember what number identifies a supplier… so you really want an easier way to set that value. The result of the process is something like the image shows below; instead of showing a numeric ‘SupplierID’, you provide a drop-down-list of the various company names.

Lookup field with ComboBox on Supplier Id Column

Now, behind the scenes, the system knows the company name and the associated Supplier ID, and it uses the latter to populate the database. Lovely stuff.

But I was doing my own project with my own data, so looking at various resources, I simply emulated their approach to replicate this effect. I rapidly started experiencing the following error messages: “The following exception occurred in the DataGridView: System.ArgumentException: DataGridViewComboBoxCell value is not valid.” and the associated dialog is here:

DataGridViewComboBoxCell Error Message Dialog

I was getting these messages as I closed the form! I could change data at will and save it, so logically it seemed like my code must be ‘right’ because I could prove the data updates were happening ok; and that seemed to suggest that the value of the cell was valid, whatever the message said. The error messages would repeat apparently infinitely, and the only way to stop them was to kill the debugging session.

Searching the web for help on this error resulted in many hits of enquiries on developer forums, and yet none of them seemed to apply to my situation. Many did not refer to databases, but objects, and certaily nothing I could find rang true to suit my situation. But before I cover the whole wind-up that was finding the cause of this problem, let’s take a quick look at what that cause was.

If You’re in a Rush, the Problem Was…

After many hours, attempts and re-attempts at fixing the problem, I found out that the issue was related to the AutoSizeMode property of the DataGridView. I had changed it on some of my columns to expand the column width to fit the data; this was not a problem for any column except the one(s) that had the lookup on; it was these columns that were the source of the error.

I regret that I do not have a solution as such right now, other than to leave AutoSizeMode to it’s default value on each column that you have a lookup on, and also make sure not to change the setting on the grid itself, which will have similar impact.

Solution now found! Read more on the story in the rest of this page, or take a look at my next post, which documents cause and solution.

I Guess ‘Playing’ can be a Bad Thing

So, a substantial part of my problem here was caused by me ‘playing’ to learn. I’d see settings in the IDE and just have a go at changing it, if it looked interesting. And guess what, when you are dealing with lookup columns, having the ability to resize the column to match the width of the contents is very interesting; as the text you will want to display will invariably be much longer than the number column is replaces.

Nevertheless, my bad for not sticking to the main target of getting a lookup sorted out; but in context, adjusting the AutoSizeMode property was quite natural for me to want to do for several columns.

Resources Rants

Ah – typical! I’ve just found a support thread that documents the cause I found… and in fact notes a possible solution that seems intelligible to me! See this link.

Other resources I found were unanswered questions… the same message, but affecting the form when it opened (apparently due to an actual difference in data-types).

Somewhat by chance, I happen to have received two books in the post today, and one of them has been tantalising in hinting at an answer, but massively frustrating in a distinct lack of workable examples. Now, obviously I’ve not actually read the whole book in a day, but skipping through any number of sections, I finally found a section that hinted that setting the AutoSizeMode on a lookup cell could cause an infinite loop of some sort (no mention of an error though). The book in question then gave an example of a modification to some example code earlier in the chapter. So I went back to that earlier code, and tried to follow the instructions to implement it (for some reason, that example was not in the downloaded code solutions). Unfortunately, despite being a code example that did have step-by-step instructions right from the start of opening a new project, by the second or third line of code it was obvious that something was missing; probably just a reference or a prefix or something, but nevertheless an extreme frustration. Even reading and understanding the intention of that code example, I still could not quite see how it applied to my siuation (which was why I wanted to implement it in the first place).

Anyway, so then I went back to the ‘added’ code example later in the chapter… and re-reading the added code, realised that it can not have been for the example listing that they noted, because the addition referred to an object / variable that did not exist in that example. Worse still (perhaps) searching all the code samples for the book, it did not appear in any of it.

All in all then, this whole experience has been layer after layer of frustrations and hurdles. I hit what seems to be a bug, missed the solution in my searches online, and find numerous hints about a solution, and then errors, in the first few sections of the book that I bought almost specifically for the purpose of learning more about data binding.

It’s times like this that I think I should move into Quality Control or Proofreading. Somehow, I just seem to find these bugs – and whilst I never exclude the possibility that there has been some stupidity on my own part, I don’t think that’s the case this time (with the exception of not finding that solution post earlier, which really was a bit dumb on my part).

18 thoughts on “DataGridViewComboBoxCell Errors in Visual Studio 2005

  1. i have problem with System.ArgumentException: DataGridViewComboBoxCell value is not valid

  2. Thanks very much…

    I could not find what the problem was while I never had made the relation between Autosizecolumnsmode and this errormessage “System.Argument.Exception: DataGridViewComboBoxCell value is not valid”

  3. Thank you very much for this clear and concise solution.

    You have saved me hours of work also. I searched the web and also found a lot of unanswered questions, or errors about data types not matching.

    But I found your solution fairly quick. So thank you for putting it out there.

    I also found it frustrating that the error did not mention what event was causing this so could not tell where to put the error handling code.

    p.s This was in Visual Studio 2008, so the problem has not gotten addressed by MS yet.

  4. Private Sub DataGridView1_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
    If e.Context = DataGridViewDataErrorContexts.Formatting Or e.Context = DataGridViewDataErrorContexts.PreferredSize Then
    e.ThrowException = False
    End If
    End Sub

  5. Excellent. Trust me to be messing with these properties. Thanks for posting the solution!

  6. i have problem with datagridview in comboboxcell
    i want to display corrosponding values in another comboboxcell in datagridview

    plz give me solution

  7. Hi,

    I have gone through all the settings whichever possible but I am unable to resolve same issue in application.

    In my DataGridView there are 3 ComboBoxColumns. Out of which two are filled by Enum list and one from DataAdapter.
    I pass Generic Class object as DataSource to DataGridView. Based on the values in this object I want those ComboBoxColumns values need to be selected and relevant DisplayMember has to get displayed.

    I works only for 2 ComboBoxColumns which are filled with Enum List. But third combo shows ValueMember as DisplayMember and gives same error :

    “System.Argument.Exception: DataGridViewComboBoxCell value is not valid”

    What could be the reason for it?

    Plz help me to solve this.

  8. I don’t know if this would help – I had the same problem but this is what worked for me. On my form I have 4 datagridviews for inserting and updating. Inside these datagridviews I have comboboxes and to populate the comboboxes I used the same datasources as my datagridviews. When updating the datagridviews I received a “System.Argument.Exception: DataGridViewComboBoxCell value is not valid” for each combobox inside the gridviews. I then created separate typed datasets as lookupdatasets for the comboboxes to populate them and my problem was solved.

  9. I had the EXACT same problem. I never would have in a million years thought it had anything to do with the autosize property and therefore would have never found it (and probably would have just eaten the exception). Thank you very much!

  10. Excellent. Trust me to be messing with these properties. Thanks for posting the solution

  11. To Solve this problem, just add “DataError” for DataGridView. That’s all,
    steps: Double click the datagridview and select the “dataerror” event from the event list.

  12. Try also a CreateControl() call after the InitializeComponent() call of the form/control that contains the datagridview. Fill the DataGridViewComboBoxCell only after you do this. I guess something in the control creation is messing with the combo values causing the error.

  13. First set autosizemode property of column of gridview in which u having combobox set from notset to none

    private void dbgrid_DataError(object sender, DataGridViewDataErrorEventArgs e)
    {
    if ((e.Context == (DataGridViewDataErrorContexts.Formatting | DataGridViewDataErrorContexts.PreferredSize)))
    {
    e.ThrowException = false;
    }
    }

    try this code … it will help you…through this code i solved my problem… !!!!

  14. My code is different than above but I believe the concept is the same. I added the values manually from a lookup table on the form load. Then I added a handler in the EditingControlShowing to trigger the event LastFxTypeColumnValidating. The function modMain.CheckLookup adds the value to the lookup table (if user wants to). I added the value to the item lists to both the dgvcmbfx_type and the cbo and selected it using the findstringexact function to return the index.

    'Load lookup tables
    'dgvcmbfx_type name of datagridview combobox
    Private Sub frmAsset_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Try
    Dim oRow As dsMusicProMgmt.lookupRow
    For Each oRow In Me.DsMusicProMgmt.lookup.Rows
    If oRow.lookup_table = DsMusicProMgmt.fx.TableName And
    oRow.lookup_field = DsMusicProMgmt.fx.fx_typeColumn.ColumnName Then
    Me.dgvcmbfx_type.Items.Add(oRow.lookup_value)
    End If
    Next
    Catch ex As Exception
    MsgBox(ex.Message, vbExclamation)
    End Try
    End Sub

    Private Sub FxDataGridView_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles FxDataGridView.EditingControlShowing
    If Me.FxDataGridView.CurrentCell.ColumnIndex = Me.FxDataGridView.Columns("dgvcmbfx_type").Index Then

    If TypeOf e.Control Is DataGridViewComboBoxEditingControl Then
    CType(e.Control, ComboBox).DropDownStyle = ComboBoxStyle.DropDown
    CType(e.Control, ComboBox).AutoCompleteSource = AutoCompleteSource.ListItems
    CType(e.Control, ComboBox).AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest

    RemoveHandler CType(e.Control, ComboBox).Validating, AddressOf LastFxTypeColumnValidating
    AddHandler CType(e.Control, ComboBox).Validating, AddressOf LastFxTypeColumnValidating
    End If
    End If
    End Sub

    Private Sub LastFxTypeColumnValidating(sender As Object, e As EventArgs)
    Dim cbo As ComboBox = TryCast(sender, ComboBox)
    If cbo.SelectedIndex = -1 Then
    If modMain.CheckLookup("fx", "fx_type", cbo.Text) = True Then
    Me.dgvcmbfx_type.Items.Add(cbo.Text)
    cbo.Items.Add(cbo.Text)
    cbo.SelectedIndex = cbo.FindStringExact(cbo.Text)
    End If
    End If
    End Sub

  15. Hi Bob,
    Thank you for your comment. I’m afraid the site engine does not seem to process code blocks in comments as we might hope, but hopefully your approach may still be useful to someone even if your carefully formatted comment has lost its indentation!
    Nij (kebabShopBlues)

Leave a Reply

Your email address will not be published. Required fields are marked *