1.11M Members

How to change textboxcell to comboboxcell in datagridview at runtime


I have datagridview populated with several columns,out of which some columns I want to change from textbox cell to comboboxcell at runtime. I have written a piece of code which works perfectly fine but it takes time to load grid.Below is the code:

For Each row As DataGridViewRow In dgvUserDetails.Rows
            If row.Index < dgvUserDetails.Rows.Count - 1 Then
                Dim cmbprof As New DataGridViewComboBoxCell()
                dgvUserDetails.Rows(row.Index).Cells(8) = cmbprof
                sql = "Select Description from Category where Catgry = 5"
                If rs.State = 1 Then rs.Close()
                rs.Open(sql, MainCon, 1, 3)
                Do While Not rs.EOF

                sql = "Select Prof from datafile where Part_No = " & dgvUserDetails.Item("Baaga", row.Index).Value & " and SLNOINPART= " & dgvUserDetails.Item("emaaMk", row.Index).Value & ""
                If rs.State = 1 Then rs.Close()
                rs.Open(sql, MainCon, 1, 3)
                If Not rs.EOF Then
                    If Not IsDBNull(rs.Fields(0).Value) Then
                        gProf = rs.Fields(0).Value
                        cmbprof.DisplayMember = "Prof"
                        cmbprof.Value = gProf
                    End If
                End If
            End If

Now this code fills cmbprof for each row even though it has same values.If I declare cmbprof out of For loop,it gives me error "Cell provided already belongs to grid."Can any1 please help me to rectify this..My grid takes long time to load due to this.

Start New Discussion
Tags Related to this Article