从数据网格视图中选择项时出错 |Access database-Visual Basic

本文关键字:出错 Access Basic database-Visual 选择 数据网 数据 网格 视图 | 更新日期: 2023-09-27 17:57:09

当单击DatagridView中的项目以在某些文本框中加载该项目的所有信息时,我遇到错误,我的代码是:

Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick    
    otherdata()
    End Sub 
 Sub otherdata()
    Try
        If (DataGridView1.Rows.Count = 0) Then Return
        FlatTextBox1.Text = String.Empty
        FlatTextBox2.Text = String.Empty
        FlatTextBox3.Text = String.Empty
        FlatTextBox4.Text = String.Empty
        Dim id As Integer = DataGridView1(1, DataGridView1.SelectedRows(0).Index).Value.ToString
        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))
        FlatTextBox1.Text = dt.Rows(0)(0).ToString
        FlatTextBox2.Text = dt.Rows(0)(1).ToString
        FlatTextBox3.Text = dt.Rows(0)(2).ToString
        FlatTextBox4.Text = dt.Rows(0)(3).ToString
        dt.Dispose()
        dt = Nothing
    Catch ex As Exception
        MessageBox.Show(ex.Message)
End Try    
End Sub              

错误是消息框说;指数超出限制。它不能为负数,并且必须小于集合的大小。无参数:索引

从数据网格视图中选择项时出错 |Access database-Visual Basic

你应该用 if 语句将 FlatTextBoxX 的赋值括起来:

If dt.Rows.Count > 0 Then 
        FlatTextBox1.Text = dt.Rows(0)(0).ToString
        FlatTextBox2.Text = dt.Rows(0)(1).ToString
        FlatTextBox3.Text = dt.Rows(0)(2).ToString
        FlatTextBox4.Text = dt.Rows(0)(3).ToString
End If