填充数据源

时间:2015-05-27 09:49:16

标签: vb.net visual-studio-2010 data-binding datagridview

我有4列的datagridview [Nam],[ID],[Shuru],[Payan] 第二列([ID])被隐藏,用于收集有关数据的一些细节。 这是加载代码:

Private Sub SalMali_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.DGV_SalMaliTableAdapter.Fill(Me.FDBDataSet.DGV_SalMali)
End Sub

检索DGV_SalMali表中的所有内容。 我知道datagridview会在填充后自动选择第一行,所以我在Selection Change中放了一个函数来检索数据。这是代码:

    Private Sub DGV_SalMaliDataGridView_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DGV_SalMaliDataGridView.SelectionChanged
        refreshDT()
    End Sub
Public Sub refreshDT()
        Dim cnt As Integer = DGV_SalMaliDataGridView.Rows.Count
        If cnt = 0 Then
            GoTo line
        End If

        unlockAll()

        Dim sel As String = DGV_SalMaliDataGridView.SelectedRows(0).Cells(1).Value

        Dim dt As New DataTable
        dt = SalMaliTA.GetData_SalMali_B_ID(sel)
        Dim dtr As DataRow = dt.Rows(0)

        TextBox1.Text = dtr.Item(4)
        MaskedTextBox1.Text = mc.MtoS(dtr.Item(1))
        MaskedTextBox2.Text = mc.MtoS(dtr.Item(2))
        CheckBox1.Checked = dtr.Item(3)

        Exit Sub
line:
        lockAll()
    End Sub

但是当我致电refreshDT()时,选择不再有效。它在refreshDT()

的第8行引发错误

sub { exactly here : Dim sel As String = DGV_SalMaliDataGridView.SelectedRows(0).Cells(1).Value }

  

抛出:"指数超出范围。必须是非负的且小于   集合的大小。"   (System.ArgumentOutOfRangeException)异常消息="索引是   超出范围。必须是非负的且小于的大小   集合。",异常类型=" System.ArgumentOutOfRangeException"

datagridview有行,我不知道问题是什么。 谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

您目前正在检查是否有任何行,但没有选择任何行:

Public Sub refreshDT()
    Dim cnt As Integer = DGV_SalMaliDataGridView.Rows.Count
    If cnt = 0 Then
        lockAll()
        Exit Sub
    End If

    If DGV_SalMaliDataGridView.SelectedRows.Count = 0 Then
        DGV_SalMaliDataGridView.Rows(0).Selected = true
    End If

    unlockAll()

    Dim sel As String = DGV_SalMaliDataGridView.SelectedRows(0).Cells(1).Value

    Dim dt As New DataTable
    dt = SalMaliTA.GetData_SalMali_B_ID(sel)
    Dim dtr As DataRow = dt.Rows(0)

    TextBox1.Text = dtr.Item(4)
    MaskedTextBox1.Text = mc.MtoS(dtr.Item(1))
    MaskedTextBox2.Text = mc.MtoS(dtr.Item(2))
    CheckBox1.Checked = dtr.Item(3)
End Sub