In order to fill-in a WinGrid I do the following
source = (UltraDataSource) myGrid.DataSourcemyObjCollection = GetMyObjCollection()Dim row(source.Band.Columns.Count - 1) As Object
Foreach myObj in myObjCollection i = 0 row(i++) = myObj.myProperty1 row(i++) = myObj.myProperty2 ... Dim uRow = source.Rows.Add(False, row, False) uRow.Tag = myObj Next myObj
Now we have a lot of rows in the grid filled with data of myObjCollection. So, once some of my rows are selected by user, I want to recuperate the BO from the rows tags,
I use:
Foreach row in myGrid.Selected.Rows row.Tag = Nothing ' WHY? How to recuperate the DataSource Row Tag value?
PS. Actually I do it like this:
For Each row In grdResult.Selected.Rows Dim mySourceRow = TryCast(row.ListObject, UltraDataRow) If mySourceRow IsNot Nothing Then Dim myObj = TryCast(mySourceRow.Tag, MyObjClass) If myObj IsNot Nothing Then myObj.Selected = True End If End If Next row
But perhaps there is a more elegant way to do it...
Hi,
I'm having a hard time understanding what this code is doing. It doesn't make a lot of sense. You seem to be creating an array of rows, but you are using the number of Columns as the count of the array instead of the number of rows in the data source. I'm also not really sure what the point of the array is, since you are filling it with properties of your data source, but then you are not doing anything else with it.
You have a counter here (i), which is incremented, but not defined or used anywhere else. Your 'next' statement doesn't match up to your 'Foreach'. And I don't know what 'v' is so I don't understand what you are assigning to the row's Tag.
It looks like maybe you are assigning 'v', whatever it is, so the Tag of the row in the UltraDataSource, and then you are reading the tag of the UltraDataSource row, so it seems like that should work.
But the code you posted here is so incomplete and jumbled, that I really can't make much out of it.
I updated the initial post.
The situation is classical. I initialize the grid's columns (say I have 10 columns).Then I have my collection of objects, and I fill-in the grid's DataSource ( i is the current column index)Finally, in order do not have just distinct properties of MyObject in a row, but to have a reference to the myObject itself, I use the row's Tag to set the object itself.Finally, if I have, say, 12 objects, I will have 12 rows and 12 tags with that objects. Each Object will contain displayed 10 of its properties in every of that 12 rows.Finally, the user selects only 3 of 12 objects, so I need to identify what object of 12 was selected.Grid -> objects collectionRow -> objectCell -> object's property.
Like I said above, if you are setting the Tag property of the UltraDataRow to something and then you try to read it out later, it should still be there. There's no reason why it would be removed by the grid or the UltraDataSource. So I don't have any idea what the problem could be.
But... it seems to me that you might be jumping through some unnecessary hoops here. If you have a custom business object and you want to bind the grid to a list of those objects, why are you using UltraDataSource? Why not just bind the grid directly to your business objects in a BindingList<T>?
the Tag is Nothing, because first time we deal with datasource (UltraDataRow), and the second with the grid rows (UltraGridRow).
As I fill-in the datasource row, the tag remains there, because I don't see any other way to get the corresponding to that row business object.
Mike, you understand the things right. My solution with Tag works, there is no problem.
I just tought that there should be a more elegant way to obtain the dataSource object, that manually setting it in the DataGridRow's tag, and then obtaining it from the ListObjects' ultraDataRow's tag...
I don't understand what you are referring to, now.
In your example, you bound the grid to an UltraDataSource and you are setting the Tag property of the UltraDataRow in the UltraDataSource.
When you get the ListObject of the UltraGridRow, it will return the UltraDataRow and the UltraDataRow's Tag property will return whatever you set it to.
If something is not working there, then something in your code must be stripping out the Tags, or else you are re-binding the grid to some other data source that doesn't have Tags.