How to change the cell value of datagridview when Datagridview combobox index is changed

Posted on 2010-01-06
Last Modified: 2012-05-08
II need to display the data in datagridview for my program in has three column and first one is combobox column and second one is textcolumn and third one also combobox.
Depends on the selection of  first combobox ,value of second text column has to be changed and same time third combobox text collection also has to be changed.

How to make changes when first combobox selected index changed in

Question by:Nitharsan
    LVL 4

    Expert Comment

    You need to handle the DataGridView's CellValueChanged event and do something like the following:

        Private Sub myDataGridView_CellValueChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
        Handles myDataGridView.CellValueChanged

            ' check if column 0 cell has changed
            If e.RowIndex <> -1 AndAlso e.ColumnIndex = 0 Then

                ' change column 1 cell value
                curPartiesRow.Cells(1).Value = someNewValue

            End If

        End Sub
    LVL 4

    Expert Comment

    I copied some code incorrectly.  The line

    curPartiesRow.Cells(1).Value = someNewValue

    should actually be:

    myDataGridView.Rows(e.RowIndex).Cells(1).Value = someNewValue


    Accepted Solution

    I found a solution...

    Private Sub dgBrake_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgBrake.EditingControlShowing

                dgComboBox = TryCast(e.Control, ComboBox)
                If (TypeOf e.Control Is DataGridViewComboBoxEditingControl) Then
                    Dim cmb As DataGridViewComboBoxEditingControl = CType(e.Control, DataGridViewComboBoxEditingControl)

                    If Not cmb Is Nothing Then
                        RemoveHandler dgComboBox.SelectedIndexChanged, AddressOf Me.dgComboBox_SelectedIndexChanged

                        AddHandler dgComboBox.SelectedIndexChanged, AddressOf Me.dgComboBox_SelectedIndexChanged
                        Exit Sub

                    End If
                End If

        End Sub

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

            If rowindex1 = 1 Then
                Dim cmbBox As ComboBox = DirectCast(sender, ComboBox)
                If cmbBox.SelectedIndex = 0 Then
                    'Add code here
                ElseIf cmbBox.SelectedIndex = 1 Then
                     'Add code here
                End If
            End If

        End Sub

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now