数据网格视图显示列但没有数据

时间:2013-12-27 19:59:16

标签: mysql database vb.net datagridview

我可以看到列而不是数据,可能与我的代码创建新数据集有关,但我不知道,这是我的代码:

  Private Sub frmClientDetails_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    DGVClient.Columns.Clear()
    objdataadapter.SelectCommand = New MySqlCommand()
    objdataadapter.SelectCommand.Connection = objconnection

    objdataadapter.SelectCommand.CommandType = CommandType.Text
    objdataadapter.SelectCommand.CommandText = "SELECT * FROM Client_Details"
    objdataadapter.Fill(New DataSet)

    DGVClient.ColumnCount = 9
    Call bind_dataset_DGVClient()
    Call count_records()
    rowposition = 0

    DGVClient.DataSource = objdataset
    DGVClient.DataMember = "Client_Details"
End Sub

感谢任何帮助,谢谢。

3 个答案:

答案 0 :(得分:1)

DataSource应设置为DataTable而不是DataSet

Private Sub frmClientDetails_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 Dim ds As New DataSet
 objdataadapter.SelectCommand = New MySqlCommand()
 objdataadapter.SelectCommand.Connection = objconnection
 objdataadapter.SelectCommand.CommandType = CommandType.Text
 objdataadapter.SelectCommand.CommandText = "SELECT * FROM Client_Details"
 objdataadapter.Fill(ds, "Client_Details")
 bind_dataset_DGVClient()
 count_records()
 rowposition = 0

 DGVClient.DataSource = ds.Tables("Client_Details")

End Sub

答案 1 :(得分:0)

objdataadapter.Fill(New DataSet)没有返回您引用的任何内容。试试这个

objdataset = New DataSet
objdataadapter.Fill(objdataset)

如果您在此之后立即设置断点,则可以将鼠标悬停在objdataset上并单击Hour glass图标以查看DataSet中的内容。

答案 2 :(得分:0)

我会将所有内容放在初始化子中并在显示帧之前调用它,以便在数据准备好显示时显示帧。我不确定,如果MySqlDataAdapter的fill()方法返回填充的记录数,我找不到快速答案。这样可以更轻松地计算行数。

     Private Sub initialise()
        DGVClient.Columns.Clear() 
        Dim sqlCmd As New MySqlCommand()
        sqlCmd.Connection = objconnection
        sqlCmd.CommandText = "SELECT * FROM Client_Details"
        objdataadapter.SelectCommand = sqlCmd
        objdataadapter.Fill(objdataset)
        rowposition = 0
        DGVClient.DataSource = objdataset.Table(0)
     End Sub

     Private Sub frmClientDetails_Load(ByVal sender As System.Object, ByVal e As   System.EventArgs)  Handles MyBase.Load

     End Sub