从访问数据库获取数据到datagridview Vb.Net的错误 [英] error in get data from access database to datagridview Vb.Net

查看:107
本文介绍了从访问数据库获取数据到datagridview Vb.Net的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击datagridview中的项目以获取更多信息时,我有一个问题!好 ?
我的代码:

i have a problem when i click to item in datagridview to get more information ! 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

这是我的完整源代码 http://up.dev-point.com/download279606.html

推荐答案

您正在尝试访问DataGridView就像一个数组,当你需要指定你正在查找行和列。而不是

You are trying to access the DataGridView like an array, when you need to specify that you're looking in the rows and columns. Instead of

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

你可以写

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

然而,依靠SelectedRows不是最好的方法。如果用户意外地选择了几行并点击了底部的话呢?

However, relying on SelectedRows is not the best way to do it. What if the user accidentally selected a few rows and clicked the bottom one?

假设您的代码位于具有

(sender As Object, e As DataGridViewCellEventArgs)

你应该使用DataGridViewCellEventArgs来告诉你正在使用哪一行依赖SelectedRows:

you should use the DataGridViewCellEventArgs to tell what row you're on instead of relying on SelectedRows:

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

这篇关于从访问数据库获取数据到datagridview Vb.Net的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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