views:

450

answers:

2

I'm using visual studio 2008.

I have a datagridview in a winform. I've bound to it using an object

 Private Sub LoadAllCampers()
    Dim Os As List(Of LE.Camper) = Nothing
    Dim Oc As New LE.Camper_Controller
    Os = Oc.GetCamperData(0)

    With Me.dgResults
        .DataSource = Os
    End With
    CamperBindingSource.DataSource = Os
End Sub

I have a tag setup on the ID property within the Camper class. when i double click on a row in teh datagridview, i do a me.dgResults.tag and it always shows the first rows ID value. If i change the ordering within the class, it will display a different value, but it's always the first value in the datagridview.

I'm missing something simple to get this working.. just don't know what it is. Hopefully someone can spare a minute.

A: 

The line:

Os = Oc.GetCamperData(0)

looks a little suspect to me.

It's a while since I've done any VB.NET, but that looks like it's getting the first index of an array/list object.

The rest of the code where you're binding the grid view's DataSource to your data looks OK.

ChrisF
I found the answer..dgResults.DataBindings.Add("Tag", Os, "CamperId")... this adds the binding that i needed to be able to use the "Tag" when calling for data.. otherwise could have used the rows.(rowindex) as suggested.Thanks for the responses
jvcoach23
A: 

"i do a me.dgResults.tag"

You mean you do something like this?

Dim value As Object = Me.dgResults.Tag

The tag property of a DataGridView is just a single information attached to the datagridview in general, not to a specific row.

There is also a Tag property in each row of the DataGridView. Let's say you want to get the Tag for the row at index rowIndex, you could do it like this:

Dim value As Object = Me.dgResults.Rows(rowIndex).Tag

...but I have no idea why you would use tags. Since you use databinding, you can get the object bound to a DataGridView's row, and access it's properties like this:

Dim row as DataGridViewRow = Me.dgResults.Rows(rowIndex)
Dim camper as LE.Camper = CType(row.DataBoundItem, LE.Camper)
Dim camperId = camper.Id
Meta-Knight