vb.net 如何在vb.net的gridview中显示数据库中的记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19204853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:22:17  来源:igfitidea点击:

How to show records in database in a gridview in vb.net

vb.netgridview

提问by Divine Grace Blanza

    Private Sub frmSearchRecords_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load         
    Dim cmd As New MySqlCommand         
    Dim dr As MySqlDataReader         
    cmd.Connection = con          
    connect()         
    cmd.CommandText = "select * from tblconsumer order by ConsumerID"
    dr = cmd.ExecuteReader
    While dr.Read
        With grdView
            .Rows.Add()
            .Rows(.RowCount - 1).Cells(0).Value = dr(0).ToString
            .Rows(.RowCount - 1).Cells(1).Value = dr(1).ToString
            .Rows(.RowCount - 1).Cells(2).Value = dr(2).ToString
            .Rows(.RowCount - 1).Cells(3).Value = dr(3).ToString
            .Rows(.RowCount - 1).Cells(4).Value = dr(4).ToString
            .Rows(.RowCount - 1).Cells(5).Value = dr(5).ToString
            .Rows(.RowCount - 1).Cells(6).Value = dr(6).ToString
        End With
    End While
    disconnect()
    End Sub

can anyone help me fix this one, i have this gridview in database and i cant load the records in the database in this gridview

谁能帮我解决这个问题,我在数据库中有这个 gridview,但我无法在这个 gridview 中加载数据库中的记录

回答by sk2185

Your Code applicable form Windows Forms not applicable to Asp.net to follow below method or Yuriy Galanter Method

您的代码适用形式 Windows 窗体不适用于 Asp.net 遵循以下方法或 Yuriy Galanter 方法

Private Sub frmSearchRecords_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load         
    Dim cmd As New MySqlCommand         
    Dim dr As MySqlDataReader         
    Dim dt As New DataTable
    Dim da As SqlDataAdapter
    cmd.Connection = con          
    connect()         
    cmd.CommandText = "select * from tblconsumer order by ConsumerID"
    da = New SqlDataAdapter(cmd)
    da.Fill(dt)
    If Not dt IsNothing Andalso dt.Rows.Count >0 Then
    grdView.DataSource =dt
    grdView.DataBind()
    End If
    disconnect()
End Sub