我试图在VB.NET中编写datagrid。我没看到桌子。我在主程序和调用函数中有调用语句: [英] 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:

查看:54
本文介绍了我试图在VB.NET中编写datagrid。我没看到桌子。我在主程序和调用函数中有调用语句:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在VB.NET中编写datagrid。我没看到桌子。我在主程序和调用函数中有调用语句:



我尝试过:



如果results.Rows.Count> 1然后
调用GridView1_SelectedIndexChanged(_DogID,_Name,_OwnerID)
结束如果



受保护的子GridView1_SelectedIndexChanged(Id为整数,名称为字符串,所有者作为整数)

Dim dogTags As New DataTable()
dogTags.Rows.Add(_DogID,_Name,_OwnerID)
GridView1.DataSource = dogTags
GridView1.DataBind ()

结束子

解决方案

原因很明显:每当你摧毁以前的数据时调用程序 GridView1_SelectedIndexChanged



移动此行 Dim dogTags As New DataTable()在程序之外(例如:在 InitializeComponent 方法之后)。

然后用这种方式改变你的程序:

 '  Dim dogTags As New DataTable() 

受保护的 Sub GridView1_SelectedIndexChanged(Id As Integer ,Name 作为 字符串,所有者作为 整数

Dim dt As DataTable = DirectCast (GridView1.DataSource,DataTable)
Dim row As DataRow = dt.NewRow()
' 不要忘记分别更改列名他们在你的项目中的名字!
row( DogId)= Id
行( 名称)=名称
行( 所有者)=所有者
dogTags.Rows.Add(row)
GridView1.DataSource = dogTags
结束 Sub





与此同时。请阅读:

Visual Basic中的范围| Microsoft Docs [ ^ ]

变量和Microsoft .NET中的方法范围 [ ^ ]

如何:控制变量的范围(Visual Basic)| Microsoft Docs [ ^ ]

DirectCast Operator(Visual Basic)| Microsoft Docs [ ^ ]

如何:添加行到一个DataTable [ ^ ]


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

解决方案

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:

'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[^]


这篇关于我试图在VB.NET中编写datagrid。我没看到桌子。我在主程序和调用函数中有调用语句:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆