Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to write datagrid in VB.NET. I don't see the table. I have the calling statement in the main program and the calling function as:

What I have tried:

 If results.Rows.Count > 1 Then
                Call GridView1_SelectedIndexChanged(_DogID, _Name, _OwnerID)
            End If



Protected Sub GridView1_SelectedIndexChanged(Id As Integer, Name As String, Owner As Integer)
        
        Dim dogTags As New DataTable()       
        dogTags.Rows.Add(_DogID, _Name, _OwnerID)
        GridView1.DataSource = dogTags       
        GridView1.DataBind()

    End Sub
Posted
Updated 28-Feb-18 22:20pm

1 solution

The reason is obvious: you are destroing previous data every time when you call procedure GridView1_SelectedIndexChanged.

Move this line Dim dogTags As New DataTable() outside the procedure (for example: after InitializeComponent method).
Then change your procedure this way:
VB.NET
'Dim dogTags As New DataTable()       

Protected Sub GridView1_SelectedIndexChanged(Id As Integer, Name As String, Owner As Integer)
        
        Dim dt As DataTable = DirectCast(GridView1.DataSource, DataTable)
        Dim row As DataRow = dt.NewRow()
        'do not forget to change column names respectively to their names in your project!
        row("DogId") = Id
        row("Name") = Name
        row("Owner") = Owner
        dogTags.Rows.Add(row)
        GridView1.DataSource = dogTags       
End Sub


In a meanwhile. please read this:
Scope in Visual Basic | Microsoft Docs[^]
Variable and Method Scope in Microsoft .NET[^]
How to: Control the Scope of a Variable (Visual Basic) | Microsoft Docs[^]
DirectCast Operator (Visual Basic) | Microsoft Docs[^]
How to: Add Rows to a DataTable[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900