How to add multiple controller to one column of DataGridView?
-
vendredi 27 juillet 2012 16:45
I initialize a DataGridView as three Column, three controllers are text, text and DataGridViewComboBoxCell.
There are some cells in third column should be use DataGridViewComboCellButton. My code is as following:
Me.diffGV.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {Me.nameCol, Me.targetCol, Me.depotCol})
ToCellButton = New DataGridViewButtonCell()
'Initialize ToCellButton in other function.
'depotCol as DataGridViewComboCellButton
Public sub initialize()
For Each row As DataGridViewRow In Me.diffGV.Rows
If row.Cells(0).Value.Equals("TO") Then
row.Cells(2) = ToCellButton
Else
Dim cBox As DataGridViewComboBoxCell = DirectCast(row.Cells(2), DataGridViewComboBoxCell)
If cBox.Items.Count > 0 Then
cBox.Value = cBox.Items(0)
End If
End If
NextEnd Sub
The initialize function works well when I call it first time. But when I refresh it, I will call the function again, the program stop at:
row.Cells(2) = ToCellButton
Pop up the exception:
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll
Additional information: Cell provided already belongs to a row. This operation is not valid.
How to fix it?
Thanks
Toutes les réponses
-
lundi 30 juillet 2012 06:35Modérateur
Hi Janet,
Welcome to the MSDN forum.
I think you need to follow this suggestion by DeborahK:
You only created on new DataGridViewComboBoxCell with your using Statement. I think you need to remove that and create a new DataGridViewComboBoxCell in the Do loop so you are not trying to reuse the cell.
Reference link at:http://social.msdn.microsoft.com/Forums/en-AU/vblanguage/thread/7d430b36-340f-43da-b0ce-f8b18e1d838e
I hope this will be helpful.
Shanks Zen
MSDN Community Support | Feedback to us
- Marqué comme réponse Shanks ZenMicrosoft Contingent Staff, Moderator mardi 7 août 2012 09:33
-
mardi 31 juillet 2012 14:46
Thanks for your reply. Shanks.
Public sub initialize()
Dim i as integer = 0
For Each row As DataGridViewRow In Me.diffGV.Rows
If row.Cells(0).Value.Equals("TO") Then
Me.diffGV(2,i) = ToCellButton
Else
Dim cBox As DataGridViewComboBoxCell = DirectCast(row.Cells(2), DataGridViewComboBoxCell)
If cBox.Items.Count > 0 Then
cBox.Value = cBox.Items(0)
End If
End Ifi = i+1
Next
End Sub
It still pops up exception when I call the function second time.
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll
Additional information: Cell provided already belongs to a row. This operation is not valid.

