-
Aug 12th, 2012, 05:45 AM
#1
Thread Starter
Junior Member
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.
-
Aug 12th, 2012, 03:48 PM
#2
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).
-
Aug 12th, 2012, 04:10 PM
#3
Thread Starter
Junior Member
Re: Unbound DataGridView : Need ComboBox In cell of 2nd column only when user clicks
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.
-
Aug 12th, 2012, 04:44 PM
#4
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?
-
Aug 13th, 2012, 02:00 AM
#5
Thread Starter
Junior Member
Re: Unbound DataGridView : Need ComboBox In cell of 2nd column only when user clicks
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.
-
Aug 13th, 2012, 05:46 AM
#6
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
-
Aug 13th, 2012, 09:22 AM
#7
Re: Unbound DataGridView : Need ComboBox In cell of 2nd column only when user clicks
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.
-
Aug 13th, 2012, 09:30 AM
#8
Re: Unbound DataGridView : Need ComboBox In cell of 2nd column only when user clicks
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|