0

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?

Mr Bim
  • 45
  • 6
  • You should bind the column to a list of all possible values, so any value in any cell will be valid. When the user selects a value in a cell in the first column, you can clear the value in the second column if that's appropriate. When the user edits a cell in the second column, you then bind the editing control - NOT the column or the cell - to the filtered list so they can only select a value that is relevant to the selection in the other column. – jmcilhinney Jun 28 '23 at 04:09
  • 1
    BTW, whenever you bind data, ALWAYS set the `DataSource` last, AFTER `DisplayMember`, `ValueMember` and/or `DataMember` properties. – jmcilhinney Jun 28 '23 at 04:09
  • To bind the editing control to a different list to the column and cells, handle the `EditingControlShowing` event. – jmcilhinney Jun 28 '23 at 04:15
  • Thank you for the advice, I will change the DataSource position to the last.So I should change HandleEvent from CellValueChanged to EditingControlShowing? – Mr Bim Jun 28 '23 at 04:31
  • Bind both columns to full lists at the start. When the value changes in the first column, clear the cell in the second column of the same row, assuming the previous value in the second column will now be invalid. When the editing control is displayed, bind it to a filtered list based on the value in the first column in the same row. The user can then only select from the filtered list when editing but any and every selection will be valid in every cell because every cell is bound to the full list. – jmcilhinney Jun 28 '23 at 06:45
  • ok i will try it.. – Mr Bim Jun 28 '23 at 09:47
  • Hello @jmcilhinney, I give an update on my problem. is it the way I do is missing something? – Mr Bim Jul 04 '23 at 04:45

0 Answers0