0
    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

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

4

1 回答 1

2

您的代码适用形式 Windows Forms 不适用于 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
于 2013-10-06T03:07:52.340 回答