Results 1 to 3 of 3

Thread: Handle combobox items inside the datagridview column

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2021
    Posts
    34

    Handle combobox items inside the datagridview column

    Hello Everyone,
    I am having some problems working with the datagridview columns and the Combobox items.

    I have one combobox column in a datagridview with two options to select:
    1. Option1
    2. Option2



    Now the Combobox items in another datagridview column should be different according to an option selected in the previous column.
    For eg: When Option1 is selected in the combobox item of first column, the combobox of second column should have the following items:
    1. A
    2. B
    3. C
    4. D
    5. E

    And when Option2 is selected the item list should be:
    1. F
    2. G
    3. H
    4. I
    5. J
    6. K
    7. L


    Everything is working fine for a normal case. But when I select Option1 and A(For eg.) in the first row and later change Option1 to Option2 then item A to the next column is not the valid value and immediately I get the comboboxitem not valid error continuously.

    So how can I solve this problem? How to remove that selected item in the second column combobox item after the change in the first column combobox item and ask them to choose one of the valid items.

    It might be a tiny problem for experts but I could not solve the issue as VB.NET is new for me. That's why I posted it here.
    Any help would be highly appreciated.

    What I tried was:
    Code:
    Private Sub dataGridViewofflinedata_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles dataGridViewofflinedata.EditingControlShowing
        If TypeOf e.Control Is ComboBox Then
            If dataGridViewofflinedata.CurrentCell.ColumnIndex = 1 Then '(For Second column of my datagridview )
                Dim cb As ComboBox = TryCast(e.Control, ComboBox)
                AddHandler cb.SelectedIndexChanged, AddressOf ColumnCombo1SelectionChanged
            End If
        End If
    End Sub
    
    Private Sub ColumnCombo1SelectionChanged(sender As Object, e As EventArgs)
    	
    	Try
    		Dim sendingComboEdit = TryCast(sender, DataGridViewComboBoxEditingControl)
    		Dim selectedValue As String= sendingComboEdit.EditingControlFormattedValue.ToString   'Get the selected item of second column
    		Dim comboitem As New DataGridViewComboBoxColumn
    	        comboitem = dataGridViewofflinedata.Columns.Item(2)     'Third column of datagridview
    	        comboitem.Items.Clear()
    		
    		
    		
    	    If selectedValue = "Option1" Then
    		    	'Here I want to have the line of code which removes the selected item in combobox of third column if previously selected
    	                 Dim FirstOptions() As String =New String() {"A","B","C","D","E"}
    	    		comboitem.Items.AddRange(FirstOptions)
    
    	    ElseIf selectedValue = "Option2" Then
                           'Here I want to have the line of code which removes the selected item in combobox of third column if previously selected 
    	    		Dim SecondOptions() As String =New String() {"F","G","H","I","J","K","L"}
    			comboitem.Items.AddRange(econdOptions)	    	
    	    End If
    	Catch ex As System.ArgumentException
    	End Try
        
        
    End Sub

    Best regards
    Krishna

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Handle combobox items inside the datagridview column

    First of all, why create a new column and then immediately discard it and use an existing one?
    Code:
    Dim comboitem As New DataGridViewComboBoxColumn
    comboitem = dataGridViewofflinedata.Columns.Item(2)
    Don't use the New keyword unless you actually want a new object.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Handle combobox items inside the datagridview column

    As for the issue, you can't keep changing the items in the column. You need to change the items in the relevant cell. If an item is selected in a cell then that item has to actually exist in that cell. If you select item A in one cell and then you change the items in the whole column then item A will be removed from every cell, so obviously it cannot be selected in a cell, so an error is generated if it is.

    There are a number of ways you could go about this. Probably the best way is to simply bind the whole list to the column and thus each cell and then leave it that way. You can then handle the EditingControlShowing event and populate the editing ComboBox with the appropriate filtered list. That way, any selection will be valid for any cell but the user will only ever be able to select from the appropriate options. I've never actually done that before myself but I'll throw a demo together.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width