简体   繁体   English

从访问数据库到datagridview的数据获取错误Vb.Net

[英]error in get data from access database to datagridview Vb.Net

i have a problem when i click to item in datagridview to get more information ! 我在datagridview中单击以获取更多信息时遇到问题! ok ? 好 ? my code : 我的代码:

Try
        If (DataGridView1.Rows.Count = 0) Then Return
        TextBox1.Text = String.Empty
        TextBox2.Text = String.Empty
        TextBox3.Text = String.Empty
        TextBox4.Text = String.Empty
        Dim id As String = DataGridView1(2, DataGridView1.SelectedRows(0).Index).Value
        Dim dt As DataTable = New DBConnect().selectdata(String.Format("SELECT items.ClientName, items.ClientAddress, items.ClientPhone, items.ClientCredit, items.ClientLastPay FROM items where items.ClientID = {0}", id))
        TextBox1.Text = dt.Rows(0)(0).ToString
        TextBox2.Text = dt.Rows(0)(1).ToString
        TextBox3.Text = dt.Rows(0)(2).ToString
        TextBox4.Text = dt.Rows(0)(3).ToString
        dt.Dispose()
        dt = Nothing
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

by debugging the error is in this line : 通过调试错误在这一行:

Dim id As String = DataGridView1(2, DataGridView1.SelectedRows(0).Index).Value.ToString

thats my full source code http://up.dev-point.com/download279606.html 多数民众赞成在我完整的源代码http://up.dev-point.com/download279606.html

You are trying to access the DataGridView like an array, when you need to specify that you're looking in the rows and columns. 当您需要指定要在行和列中查找时,您试图像访问数组一样访问DataGridView。 Instead of 代替

DataGridView1(2, DataGridView1.SelectedRows(0).Index).Value

you can write 你可以写

DataGridView1.Columns(2).Cells(DataGridView1.SelectedRows(0).Index).Value

However, relying on SelectedRows is not the best way to do it. 但是,依靠SelectedRows并不是最好的方法。 What if the user accidentally selected a few rows and clicked the bottom one? 如果用户不小心选择了几行并单击了底部的行怎么办?

Assuming your code is in a CellClick handler with 假设您的代码在CellClick处理程序中,

(sender As Object, e As DataGridViewCellEventArgs)

you should use the DataGridViewCellEventArgs to tell what row you're on instead of relying on SelectedRows: 您应该使用DataGridViewCellEventArgs来告诉您所在的行,而不是依赖SelectedRows:

Dim id As String = DataGridView1(e.RowIndex).Cells(2).Value 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM