Custom combo keeps repeating itself in DataGridView

Answered Custom combo keeps repeating itself in DataGridView

  • Tuesday, July 03, 2012 1:58 PM
     
     

    I've developed a VB app which has a datagridview with a custom combobox.  The app displays patient data one patient at a time.  There is a datagridview that shows patients' family info:  name of relative, age, and ethnicity.  "Ethnicity" is a combobox.  As subsequent patients are displayed, this combo erroneously repeats itself.  For patient #1, there is one combobox.  For patient #2, there are 2 comboboxes, patient #3 - 3 combos and so on.  There should only be one combobox per patient.

    The DataGridView is unbound.  With each patient, I fill the datagridview and create a combobox for the Ethnicity.

    It appears that I have to clear either the datagridview or the combobox  from memory as another patient is displayed but I'm not quite sure how to do that.  I tried:

         dgvHouseHold = Nothing

    But I got a "Null Reference Error".

    Do I need to clear the combobox somehow and if so, how?


    Kris Hood

All Replies

  • Friday, July 06, 2012 7:05 AM
    Moderator
     
     

    Hi Kris,

    I'm not sure what is going on without any source code. However, if you want to use combo box in a unbound DataGridView, you can add DataGridViewComboBoxColumn .

    You can add the ComboBoxColumn in the designer, right click the DataGridView, select add columns..., select DataGridViewComboBoxColumn.

    If there is anything unclear, please let me know. If I misunderstand you, please kindly clarify the question.

    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

  • Friday, July 06, 2012 2:44 PM
     
      Has Code

    Thanks for your reply.  If I understand you correctly, you're suggesting this approach rather adding the combobox with code.  Below is the coding I use now to add the combobox to the DataGridView.  I found it online and it seems to work fine except for the repeating issue.

                strSQL = "SELECT ClientID, PersonName as [Name of Person], Relationship, Age, HIVPositive as [HIV Positive], tblClientHousehold.RaceID " & _
                    "FROM tblClientHousehold " & _
                    "LEFT JOIN tlkpRace ON tblClientHousehold.RaceID = tlkpRace.RaceID WHERE ClientID = " & intClientID
                daHousehold = New SqlDataAdapter(strSQL, conn)
                daHousehold.Fill(dsHousehold, strSQL)
                MyBindingSource.DataSource = dsHousehold
                MyBindingSource.DataMember = dsHousehold.Tables(0).TableName
                ' create combobox for Race
                Dim col As New DataGridViewComboBoxColumn
                col.HeaderText = "Race"
                col.DataSource = dsRace.Tables("tlkpRace")
                col.DisplayMember = "Race"
                col.ValueMember = "RaceID"
                col.DataPropertyName = "RaceID" 'this is tblClientHousehold.RaceID
                col.FlatStyle = FlatStyle.Flat
                With dgvHousehold_Fill
                    .DataSource = MyBindingSource
                    .Columns.Add(col)
                    .Columns(0).Visible = False
                    .Columns(5).Visible = False
                    .Columns(6).Width = 125
                    .ColumnHeadersVisible = True
                    .RowsDefaultCellStyle.BackColor = Color.WhiteSmoke
                    .RowsDefaultCellStyle.ForeColor = Color.Black
                    .AlternatingRowsDefaultCellStyle.BackColor = Color.Gainsboro
                    .AlternatingRowsDefaultCellStyle.ForeColor = Color.Black
                    .ColumnHeadersDefaultCellStyle.Font = New Font(.Font, FontStyle.Bold)
                    .AllowUserToAddRows = True
                    .AllowUserToDeleteRows = True
                    .ReadOnly = False
                End With

    This code handles the selection of items in the combobox.  Again I found this online. 

        Private Sub dgvHousehold_EditingControlShowing(sender As Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgvHousehold.EditingControlShowing
            If dgvHousehold.CurrentCell.ColumnIndex = 6 Then
                Dim cb_Race As ComboBox = e.Control
                RemoveHandler cb_Race.SelectedIndexChanged, New EventHandler(AddressOf cb_Race_SelectedIndexChanged)
                AddHandler cb_Race.SelectedIndexChanged, AddressOf Me.cb_Race_SelectedIndexChanged
            End If
        End Sub
        Private Sub cb_Race_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
            cmbBox = DirectCast(sender, ComboBox)
            intRowSave = dgvHousehold.CurrentCell.RowIndex
        End Sub
    Using the method you suggested, it sounds like I could remove the code where the combobox is added to the DataGridView.  Of course I'd have to keep the code to bind data to the combobox.  Would I need the "EditingControlShowing" and "SelectedIndexChanged" events?

    Kris Hood

  • Monday, July 09, 2012 9:37 AM
    Moderator
     
     

    Hi Kris,

    I got confused. In your initial post, you mentioned this is an unbound DataGridView while it is a bound DatGridView according to your code.

    The code seems fine. I'm not sure what do you mean by "repainting" now?

    Could you please give me a screenshot to clarify what do you mean by "repeating"?

    If you can share a demo with us, it would help us understand your question more clearly. You can upload demo to SkyDrive, set it share with everyone and post the link here.

    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

  • Monday, July 09, 2012 3:20 PM
     
     

    I apologize.  I'm a newbie to vb.net so I might use the wrong terminology.  Hopefully the following screenshots will explain my problem.

    The first image shows the datagridview for the first patient (this patient just happens to have no data yet).  You'll notice that there is only one Race combobox (and there should only be one):

    This next image shows the datagridview for the next patient.  Notice there are now 2 Race comboboxes when there should be one:

    If I got to a third patient, there are 4 Race comboboxes.  With a fourth patient, there are 6 comboboxes, and so on.

    It appears to me that I have to wipe out the custom combobox somehow when navigating to a new patient.  I'm not sure how to do that.

    Let me know if there's any other info you need. 

    Thanks....Kris


    Kris Hood

  • Thursday, July 12, 2012 8:23 AM
    Moderator
     
     Answered

    Hi Kris,

    I'm can't detect the issue without the code but I'm sure that this caused by code logic.

    I assume that you call the posted code more than one time. If so, I suggest you clear all the column each time you before you add the column. You can clean the column by call DataGridViewColumnCollection.Clear Method.

    If the issue still not solved, could you please share a demo with us?

    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

    • Marked As Answer by kris_hood Thursday, July 12, 2012 1:17 PM
    •  
  • Thursday, July 12, 2012 1:19 PM
     
     
    Thanks, Bob.  I think that's all it needed.  The combobox does not repeat any more. 

    Kris Hood

  • Friday, July 13, 2012 5:29 AM
    Moderator
     
     

    I'm glad the issue have been solved.

    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us