Mr Bim
Mr Bim

Reputation: 45

DataGridView value is not Valid when changing value in DataGridViewComboBox

I have DataGridView containing 2 cells like this:

enter image description here

For Size, ComboBox is populated based on what the user choose in Material Combo Box. If I select Material ComboBox and then choose the size is not show the error, but when I want to select other Material and it show the error like this:

enter image description here

for showing the size based on what user choose in the material type ComboBox, I use CellValueChanged . Here is the code

Private Sub dgMaterial_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles dgMaterial.CellValueChanged
        Dim dTableSize As New DataTable
        Dim lngID as Long
        If e.ColumnIndex = 0 AndAlso e.RowIndex >= 0 Then
            lngID = dgMaterial.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
            
            Dim AdSize As New SqlDataAdapter("SELECT ID, sizeY from ItemSC WHERE ParentID = " & lngID & "ORDER BY sizeY", con)
            Dim str As String
            AdSize.Fill(dTableSize)

            dgmSize.DataSource = dTableSize
            dgmSize.DisplayMember = "sizeY"
            dgmSize.ValueMember = "ID"
        End If



    End Sub

is it the correct way to show the size in the Size ComboBox?

Update:

I use the suggestion from jmcilhinney and still experience the same problem. In the CellValueChanged Event Handler, I remove the line for filling the DataTable and move it to the EditingControlShowing Event.

Private Sub dgMaterial_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles dgMaterial.EditingControlShowing
        Dim dTableSize As New DataTable

        If TypeOf e.Control Is ComboBox Then
            Dim AdSize As New SqlDataAdapter("SELECT ID, sizeY from ItemSC WHERE ParentID = " & lngIDMat & "ORDER BY sizeY", con)
            AdSize.Fill(dTableSize)

            dgmUkuran.DisplayMember = "sizeY"
            dgmUkuran.ValueMember = "ID"
            dgmUkuran.DataSource = dTableSize

        End If
    End Sub

is this the right way?

Upvotes: 0

Views: 66

Answers (0)

Related Questions