搜索DataGridView for Integer然后选择Row

时间:2014-01-25 20:09:56

标签: vb.net datagridview datagridviewcolumn

以下代码应搜索DataGridView1表单上的LeaderAccessTable,以获取用户输入SendFromID的整数,如果DataGridView1的第一列包含的内容用户输入的整数SendFromID然后应选择整行。但是它根本没有选择任何行......任何人都可以看到原因吗?此代码从单独的表单运行。

Dim intcount As Integer
For Each Row As DataGridViewRow In LeadersAccessTable.DataGridView1.Rows
  If LeadersAccessTable.DataGridView1.Rows(intcount).Cells(0).Value.ToString = SendFromID.Text Then
    LeadersAccessTable.DataGridView1.Rows(intcount).Selected = True
  End If
Next
MsgBox("Done.")

1 个答案:

答案 0 :(得分:0)

最后这段代码有效。

Dim v_SelectRow As Integer
        For counter = 0 To (LeadersAccessTable.DataGridView1.Rows.Count - 1)
            For counter2 = 0 To (LeadersAccessTable.DataGridView1.Columns.Count - 1)
                If (LeadersAccessTable.DataGridView1.Rows(counter).Cells(0).Value.ToString.Contains(SendFromID.Text)) Then
                    LeadersAccessTable.DataGridView1.Rows(counter).Cells(0).Selected = True
                    v_SelectRow = LeadersAccessTable.DataGridView1.CurrentRow.Index
                    CurrentPoints.Text = LeadersAccessTable.DataGridView1.Item(8, v_SelectRow).Value
                    'Do Something
                    Else
                    'Do Something
                    End If
                Next
            Next