Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > datagridview winform tag only brings back the first row value
 

datagridview winform tag only brings back the first row value

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.
Shannon Ramirez
jvcoach23  Thursday, September 03, 2009 7:16 PM

Hello Shannon,

Thanks for your post onMSDN forum.

I am not sure how you setup the tag ofDataGridViewcontrol, but if we bindTagto the list,it will show the currentrow ID value. See the following for example.

<CODE>
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Os As New List(Of Camper)()
Dim camp As Camper
For i As Integer = 0 To 10
camp = New Camper()
camp.Id = i.ToString()
camp.Name = "Item" & i
Os.Add(camp)
Next

Me.DataGridView1.DataSource = Os
Me.DataGridView1.DataBindings.Add("Tag", Os, "Id")

AddHandler Me.DataGridView1.CellDoubleClick, AddressOf dataGridView1_CellDoubleClick
End Sub

Private Sub dataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
MessageBox.Show(Me.DataGridView1.Tag.ToString())
End Sub

End Class

Public Class Camper
Private m_id As String

Public Property Id() As String
Get
Return m_id
End Get
Set(ByVal value As String)
m_id = value
End Set
End Property
Private m_name As String

Public Property Name() As String
Get
Return m_name
End Get
Set(ByVal value As String)
m_name = value
End Set
End Property
End Class
</CODE>

If you have any additional question, welcome to post here.

Thanks,
Rong-Chun Zhang
MSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Marked As Answer byjvcoach23 Friday, September 04, 2009 11:57 AM
  •  
Rong-Chun Zhang  Friday, September 04, 2009 3:29 AM

Hello Shannon,

Thanks for your post onMSDN forum.

I am not sure how you setup the tag ofDataGridViewcontrol, but if we bindTagto the list,it will show the currentrow ID value. See the following for example.

<CODE>
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Os As New List(Of Camper)()
Dim camp As Camper
For i As Integer = 0 To 10
camp = New Camper()
camp.Id = i.ToString()
camp.Name = "Item" & i
Os.Add(camp)
Next

Me.DataGridView1.DataSource = Os
Me.DataGridView1.DataBindings.Add("Tag", Os, "Id")

AddHandler Me.DataGridView1.CellDoubleClick, AddressOf dataGridView1_CellDoubleClick
End Sub

Private Sub dataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
MessageBox.Show(Me.DataGridView1.Tag.ToString())
End Sub

End Class

Public Class Camper
Private m_id As String

Public Property Id() As String
Get
Return m_id
End Get
Set(ByVal value As String)
m_id = value
End Set
End Property
Private m_name As String

Public Property Name() As String
Get
Return m_name
End Get
Set(ByVal value As String)
m_name = value
End Set
End Property
End Class
</CODE>

If you have any additional question, welcome to post here.

Thanks,
Rong-Chun Zhang
MSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Marked As Answer byjvcoach23 Friday, September 04, 2009 11:57 AM
  •  
Rong-Chun Zhang  Friday, September 04, 2009 3:29 AM
many thanks.. that did the trick
Shannon
Shannon Ramirez
jvcoach23  Friday, September 04, 2009 12:01 PM

You can use google to search for other answers

Custom Search

More Threads

• error
• Creating a Table Adapter from Access DB wrong date format
• Related to Clicking a DataGridViewComboBoxColumn thrice to select an Item of DataGridView
• Sort glyph position in column header
• Changing Color in Datagrid
• GridView edit button, Catch button push
• SqlConnection and sp_reset_connection problem
• How to Display the child data in a combobox which is in a DataagridView
• Row comitted on datagridview
• Selecting a DataGridView row programatically