KTN-IT
asked on
Populating custom DataGridViewRowCollection property of subclassed DataGridView
I have a subclassed DataGridView in my application to which I add a column of checkboxes alongside the cells of data (so that users can select multiple rows of data from the table). I am trying to add a "CheckedRows" property to my class that will return a collection of DataGridView rows that have been selected (checked) by the user.
But I get an error when I try to add rows to the collection. See screenshot and code below.
I am coding in VB, but solutions are welcome in C#, since I can just convert the code to VB online.
But I get an error when I try to add rows to the collection. See screenshot and code below.
I am coding in VB, but solutions are welcome in C#, since I can just convert the code to VB online.
ReadOnly Property CheckedRows As DataGridViewRowCollection
Get
Dim myrow As DataGridViewRow
For Each myrow In Me.Rows
If CType(myrow.Cells("chek").Value, Boolean) = True Then
CheckedRows.Add(myrow) '<<Errors here
End If
Next
End Get
End Property
Error.gif
ASKER
I tried creating a "new" instance of the row and it didn't work. Same error at the same place.
Dim myrow2 As New DataGridViewRow
For Each myrow In Me.Rows
If CType(myrow.Cells("chek").Value, Boolean) = True Then
myrow2 = myrow
CheckedRows.Add(myrow2) '<< still doesn't work
End If
Next
ASKER
Here's something interesting.
I thought that maybe what's not getting instanced correctly is not the DataGridViewRow but the DataGridViewRowCollection. So I tried creating a new instance of a DataGridViewRowCollection to temporarily add rows to until I was ready to return it to the property, and look what error I got (see screenshot).
It's referencing the standard Windows Forms' DataGridView, and not my subclassed version. Does this matter?
Clipboard01.gif
I thought that maybe what's not getting instanced correctly is not the DataGridViewRow but the DataGridViewRowCollection.
It's referencing the standard Windows Forms' DataGridView, and not my subclassed version. Does this matter?
Clipboard01.gif
http://stackoverflow.com/questions/489425/adding-columns-to-a-datatable-bound-to-a-datagridview-does-not-update-the-view
take a look at above link and see whether it is any helpful for you. I will try to come up with a sample code.
take a look at above link and see whether it is any helpful for you. I will try to come up with a sample code.
ASKER
From the above observation, I believe that I cannot have a new DataGridViewRowCollection without creating a new DataGridView. And I cannot reference my current instance of the row collection, because I need that to stay in tact. And it doesn't seem like I can add an additional RowCollection to the current instance of the DataGridView (it looks like there can only be one RowCollection per DataGridView).
So, not knowing any other solution, I am changing my property to be just a "Collection," rather than a specific DataGridViewRowCollection. I can create a new instance of a Collection in the property just fine and add rows to it.
I'll keep this question open in case anyone else has some helpful comments, but this solves me problem for now.
So, not knowing any other solution, I am changing my property to be just a "Collection," rather than a specific DataGridViewRowCollection.
I'll keep this question open in case anyone else has some helpful comments, but this solves me problem for now.
Ya in the mean time I will also try to create some example and post it for you.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks!
Like dim myrow as new datagridviewrow and see. I think it should fix the issue.
Good Luck