Note: Forums will be making significant UX changes to address key usability improvements surrounding search, discoverability and navigation. To learn more about these changes please visit the announcement which can be found HERE.
How to reset a datagridview combobox column value in VB?

Answered How to reset a datagridview combobox column value in VB?

  • Saturday, August 18, 2012 8:54 PM
     
     

    There are two dataGridComboBox columns in my table. Each of them is with intial item value list when the table is just opened. What I want is that when the selected value in the first column comboBox changes, the whole list of values in the second column comboBox will also be changed. In other words, the comboBox list in second column will always be reset if you select a different item from the first column comboBox. Do you have any idea to handle the comboBox list reset?

    Regards!

    Lao Fu

All Replies

  • Monday, August 20, 2012 7:10 AM
    Moderator
     
      Has Code

    Hi Lao Fu,

    You may try to change the second comboboxcell's DataSource property to another bindingsource when the first comboboxcell's value changes.

            private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
            {
                // column0 is the first comboboxcolumn, column1 is the second comboboxxolumn
                if (e.ColumnIndex == 0 && e.RowIndex > -1)
                {
                    if (dataGridView1[0, e.RowIndex].Value.ToString() == "1")
                    {
                        dataGridView1[1, e.RowIndex].Value = null;
                        ((DataGridViewComboBoxCell)(dataGridView1[1, e.RowIndex])).DataSource = second1BindingSource;
                    }
                    if (dataGridView1[0, e.RowIndex].Value.ToString() == "2")
                    {
                        dataGridView1[1, e.RowIndex].Value = null;
                        ((DataGridViewComboBoxCell)(dataGridView1[1, e.RowIndex])).DataSource = second2BindingSource;
                    }
                }
            }
    

    If there are any questions, please feel free to let us know.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

  • Monday, August 20, 2012 11:39 AM
    Moderator
     
     Answered Has Code

    Sorry, this is the VB.NET version:

    Private Sub dataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs)
    	' column0 is the first comboboxcolumn, column1 is the second comboboxxolumn
    	If e.ColumnIndex = 0 AndAlso e.RowIndex > -1 Then
    		If dataGridView1(0, e.RowIndex).Value.ToString() = "1" Then
    			dataGridView1(1, e.RowIndex).Value = Nothing
    			DirectCast(dataGridView1(1, e.RowIndex), DataGridViewComboBoxCell).DataSource = second1BindingSource
    		End If
    		If dataGridView1(0, e.RowIndex).Value.ToString() = "2" Then
    			dataGridView1(1, e.RowIndex).Value = Nothing
    			DirectCast(dataGridView1(1, e.RowIndex), DataGridViewComboBoxCell).DataSource = second2BindingSource
    		End If
    	End If
    End Sub
    

    Best regards,

    Chester Hong
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked As Answer by Lao Fu Monday, August 20, 2012 4:06 PM
    •  
  • Monday, August 20, 2012 4:06 PM
     
     

    Hi Chester,

    My question got answered by you. Thanks a lot!

    I have another question about how to handling SelectionChangeCommitted event correctly. I have posted it under the topic "DataGridViewComboBoxColumn Selection Changed". Would you mind having a look of my question there and giving me your solution or suggestion?

    This question was discussed several years ago and actually got solved by Troy. But his solution is in C#. I don't know how to translate it into VB.

    Thanks again!

    Lao Fu


    /Chengde Fu/

  • Wednesday, August 22, 2012 6:57 AM
    Moderator
     
      Has Code

    Hi Lao Fu,

    Are you talking about this thread: http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/453d7584-d4d3-495b-b3ed-5998758d9614

    If so, please try the following code snippet to see whether you can use it:

    Public Class Form1 Private _dataGridView As DataGridView Private _comboBoxSelectDelegate As EventHandler

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            _dataGridView = New DataGridView()

            ' setup the datagridview here.
            ' hook up editing control showing event
            AddHandler _dataGridView.EditingControlShowing, AddressOf _dataGridView_EditingControlShowing

            ' create a delegate for the method that will handle the event
            _comboBoxSelectDelegate = New EventHandler(AddressOf combo_SelectionChangeCommitted)
        End Sub

    Private Sub _dataGridView_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) ' get the control from the event args. Dim combo As ComboBox = TryCast(e.Control, ComboBox) If combo IsNot Nothing Then ' remove the event subscription if it exists. RemoveHandler combo.SelectionChangeCommitted, AddressOf combo_SelectionChangeCommitted ' add a subscription to the event AddHandler combo.SelectionChangeCommitted, AddressOf combo_SelectionChangeCommitted End If End Sub Private Sub combo_SelectionChangeCommitted(sender As Object, e As EventArgs) ' handle the event. Dim combo As ComboBox = TryCast(sender, ComboBox) If combo IsNot Nothing Then MessageBox.Show(String.Format("The combobox selected index is: {0}", combo.SelectedIndex)) End If End Sub End Class

    Best regards,

    Chester Hong
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

  • Thursday, August 23, 2012 10:03 PM
     
     

    Hi Chester,

    Your professional translation of the code help me a lot. Actually, just by copying your code, my problem get solved. Thank you so much!

    However, I encountered another related problem: For some columnComboBox, you have to select the same item in the comboBox twice to be able to display the selected item, if you want to invoke some action after the selection commited. Below is part of my code. You can see that the statement for calling sub "Reset_ComboBox_Values" is commented. If you uncomment the statement, the problem that I described above will happen. Do you have any idea about this problem?

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

            ' setup the datagridview here.
            ' hook up editing control showing event       
            AddHandler DataGridView1.EditingControlShowing, AddressOf DataGridView1_EditingControlShowing

            ' create a delegate for the method that will handle the event        
            _comboBoxSelectDelegate = New EventHandler(AddressOf combo_SelectionChangeCommitted)
        End Sub

        ...

        Private Sub DataGridView1_EditingControlShowing(ByVal sender As System.Object, ByVal e As DataGridViewEditingControlShowingEventArgs)
            'Private Sub _dataGridView_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs)Handles DataGridView1.EditingControlShowing

            ' get the control from the event args.
            Dim combo As ComboBox = e.Control

            If combo IsNot Nothing Then
                ' remove the event subscription if it exists.
                RemoveHandler combo.SelectionChangeCommitted, AddressOf combo_SelectionChangeCommitted

                ' add a subscription to the event
                AddHandler combo.SelectionChangeCommitted, AddressOf combo_SelectionChangeCommitted
            End If

        End Sub

        Private Sub combo_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs)

            Dim combo As ComboBox = sender

            If Not (combo Is Nothing) Then
                If Me.DataGridView1.CurrentCell.ColumnIndex = 1 Then
                    Dim compName As String
                    compName = combo.SelectedItem.ToString
                    'Reset_ComboBox_Values(compName)
                End If

            End If
        End Sub

        Private Sub Reset_ComboBox_Values(ByVal compName As String)
        ...

    I greatly appreciate your helping. Thanks again!

    Lao Fu


    /Chengde Fu/

  • Thursday, August 23, 2012 10:14 PM
     
     

    One more thing that I have to mention here is that the sub "Reset_ComboBox_Values" is to reset values/items in another columnComboBox, not the comboBox that you just selected an item. Below is its code:

        Private Sub Reset_ComboBox_Values(ByVal compName As String)
            If (Not String.IsNullOrEmpty(compName)) And _
                (StrComp(compName, "<application end>", 1) <> 0 And StrComp(compName, "Generic_Composite_Caller", 1) <> 0) Then
                ' call a function to open the component script, parse it and grab the action keywords from it, and store them into an array
                Dim reader As StreamReader = New StreamReader(projectFolder & "\Components\" & compName & "\Action1\Script.mts")
                Dim index, index2 As Int32
                Dim tmp, sArr(), actionKeyword(2000) As String
                Try
                    index2 = 0
                    Do
                        ' parse the line and if the line contains an action keyword, grab it from the line and store it into an arry
                        tmp = reader.ReadLine
                        index = InStr(1, tmp, "' To Do: Action keyword", 1)
                        If index > 0 Then
                            sArr = tmp.Split("'")
                            actionKeyword(index2) = sArr(2)
                            index2 = index2 + 1
                        End If

                    Loop Until reader.Peek = -1

                Catch
                    Debug.Print("File is empty")

                Finally
                    reader.Close()
                End Try

                numOfCompo = index2 + 2
                Dim tmpArr(numOfCompo) As String
                tmpArr(0) = ""
                tmpArr(1) = "Generic_Composite_Caller"
                tmpArr(2) = "<application end>"
                For i As Int16 = 3 To numOfCompo Step 1
                    tmpArr(i) = actionKeyword(i - 3)
                Next
                DataGridView1.Columns.Remove("column3")
                DataGridView1.Columns.Add(column3)
                column3.DataSource = tmpArr
            End If
        End Sub


    /Chengde Fu/