从datagridview加载数据到文本框vb.net时出错

时间:2016-07-15 21:07:14

标签: vb.net datagridview

我将数据从Datagridview加载到TextBox时出错。请看下面的图片https://s32.postimg.org/m28ardoat/Sans_titre.png 我的代码:

Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick    
    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 ClientID, ClientName, ClientAddress, ClientPhone, ClientCredit, ClientLastPay FROM client where client.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

End Sub

通过调试此行中的错误:Dim id As String = DataGridView1(2, DataGridView1.SelectedRows(0).Index).Value 希望能帮到我!

0 个答案:

没有答案