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.
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).
Re: Unbound DataGridView : Need ComboBox In cell of 2nd column only when user clicks
Quote:
Originally Posted by
dunfiddlin
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.
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?
Re: Unbound DataGridView : Need ComboBox In cell of 2nd column only when user clicks
Quote:
Originally Posted by
dunfiddlin
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.
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
Re: Unbound DataGridView : Need ComboBox In cell of 2nd column only when user clicks
Quote:
Originally Posted by
dunfiddlin
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.
Re: Unbound DataGridView : Need ComboBox In cell of 2nd column only when user clicks
Quote:
Originally Posted by
i00
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.