Results 1 to 8 of 8

Thread: Unbound DataGridView : Need ComboBox In cell of 2nd column only when user clicks cell

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2011
    Posts
    20

    Unbound DataGridView : Need ComboBox In cell of 2nd column only when user clicks cell

    Dear All,
    I have a unbound DataGridView with two columns. First column is just string values.
    Second column I want to display a combobox, only when user click the cell(not the whole column as DataGridViewColumn). I use the below code which is incorrect and gives me the error : Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function.
    The first column is popuated, and the second column is empty.


    The code is as below :

    Code:
    Private Sub DGVFieldsMap_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVFieldsMap.CellEnter
            If e.ColumnIndex = 1 Then
                If cboClmCell Is Nothing Then
                    Dim dgv As DataGridView = CType(sender, DataGridView)
                    cboClmCell = New DataGridViewComboBoxCell
                    cboClmCell.Items.Add("A")
                    cboClmCell.Items.Add("B")
                    cboClmCell.Items.Add("C")
                    cboClmCell.Items.Add("D")
                    cboClmCell.Items.Add("E")
                    cboClmCell.Items.Add("F")
                    dgv.Focus()
                    dgv(e.ColumnIndex, e.RowIndex) = cboClmCell '[Error Here]
                    isCombo = True
                    
                End If
            End If
        End Sub
     
    Private Sub DGVFieldsMap_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DGVFieldsMap.CellValidating
            If e.ColumnIndex = 1 Then
                Dim dgv As DataGridView = CType(sender, DataGridView)
                If isCombo Then
                    isCombo = False
                    cboClmCell = Nothing
                    dgv(e.ColumnIndex, e.RowIndex) = New DataGridViewTextBoxCell()
                End If
            End If
    End Sub
    Can anybody give me a complete working example with two columns, the second column being a ComboBoxCell, but only when user clicks. Also I need to get the selected values in the DataGridView cell. Thanks In Advance.
    Last edited by tonydsouza1987; Aug 12th, 2012 at 06:24 AM.

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Unbound DataGridView : Need ComboBox In cell of 2nd column only when user clicks

    No. Nobody can. That's not what DGVs are for and, to be honest, I can't really see what purpose would be served by having it any other way. What exactly are you trying to achieve (assuming it's not just being clever for being clever's sake).

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2011
    Posts
    20

    Re: Unbound DataGridView : Need ComboBox In cell of 2nd column only when user clicks

    Quote Originally Posted by dunfiddlin View Post
    No. Nobody can. That's not what DGVs are for and, to be honest, I can't really see what purpose would be served by having it any other way. What exactly are you trying to achieve (assuming it's not just being clever for being clever's sake).
    It just looks odd when I add a DataGridViewColumn as a second column. Because it displays Comboboxes in all cells in that particular column. So it is best to have a combobox displayed only when the user clicks on it. I just want to import data from excel to Database. So i provide fields mappings in the datagrid. So, the first column in the datagridview is DB field names, where as second column(as combobox) in DGV is loaded with excel first row names. So what do you suggest?. thanks for the reply.

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Unbound DataGridView : Need ComboBox In cell of 2nd column only when user clicks

    So why wouldn't you have a combo box for both?

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2011
    Posts
    20

    Re: Unbound DataGridView : Need ComboBox In cell of 2nd column only when user clicks

    Quote Originally Posted by dunfiddlin View Post
    So why wouldn't you have a combo box for both?
    You mean for both the column I need to display comboboxes? No. First column is fixed by DB field names.

  6. #6
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: Unbound DataGridView : Need ComboBox In cell of 2nd column only when user clicks

    Handle the CellPainting event, draw the text, and set e.handled to true for the cell that you want to hide the checkbox for..
    this should do it

    Kris

  7. #7
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Unbound DataGridView : Need ComboBox In cell of 2nd column only when user clicks

    Quote Originally Posted by dunfiddlin View Post
    So why wouldn't you have a combo box for both?
    And? If you only want only the users choice of fields to map then the double combobox is perfect. If you want all the fields mapped than you need to make it clear by showing all the comboboxes in the 2nd column. It's the user's needs not your aesthetics which should be primary here.

  8. #8
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Unbound DataGridView : Need ComboBox In cell of 2nd column only when user clicks

    Quote Originally Posted by i00 View Post
    Handle the CellPainting event, draw the text, and set e.handled to true for the cell that you want to hide the checkbox for..
    this should do it

    Kris
    Er .. first off it's comboboxes not checkboxes, second it's all of them the OP wants to hide until such time as the corresponding cell in the first column is clicked (and presumably thereafter), and third ..... well, I'm bound to think of something the second I submit this. Not saying it's impossible just a bit more complex than you suggest.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width