Question : Datagridview Combobox!

Hello All,

I have a datagridview with a combobox column.  What I'm doing is loading a dataset with a value from a table and displaying that value in the combobox for the frist row of the datagrid.  Each row should have a different value in the combobox column.  What's happening is the same value is being set for each combobox in each row.  So if I have 5 rows, all 5 rows have the same value.  I do not want this.  Here's an example of my code:

 Dim intRows As Integer = dgvRO.Rows.Count
            Dim intRowNo As Integer = 0

            Dim dt As DataTable = Nothing
            Dim dr As DataRow = Nothing
            Dim strItem As String = ""

            'loop through grid rows
            For intRowNo = 0 To intRows - 1

                strItem = dgvRO.Rows(intRowNo).Cells("ItemNo").Value

               With clsMP
                    .Name = cboName.Text
                    dsMP = .GetAuthorizedVendors(strItem)
                End With

                Dim intRecs As Integer = dsMP.Tables(0).Rows.Count
                Dim intRow As Integer = 0

                dt = dsMP.Tables(0)

                If intRecs > 0 Then

                    Dim dgvc As DataGridViewComboBoxColumn
                    dgvc = dgvRO.Columns("cboItemNo")

                    For Each dr In dt.Rows
                        dgvc.Items.Add(dr.Item("ItemNo").ToString)
                    Next

                End If

                intRowNo += 1

            Next

        Catch ex As Exception
        End Try


The dataset loads currently with 1 ItemNo for the first reocord.   Any help would be great!

Thanks.

Jim

Answer : Datagridview Combobox!

Hi,

You can asign the Display and Value Members of the combo and the datagrid will do the match for you as long the field asigned to the comboboxcolum have values of the datasource asigned, like this:

        Dim dgvc As DataGridViewComboBoxColumn
        dgvc = CType( dgvRO.Columns("cboItemNo"), DataGridViewComboBoxColumn)
        dgvc.DataSource = YOUDATASOURCE
        dgvc.DisplayMember = "THE FIELD YOU WANT TO DISPLAY"
        dgvc.ValueMember = "THE FIELD THAT MATCH IN BOTH TABLES"

This should be before you load you datagrid.

Hope it helps,
Random Solutions  
 
programming4us programming4us