你好,我刚开始接触编程。上个月做了一个有用的程序,现在转而做一些稍微大一点的事情——数据网格视图让我头痛。我的Datagridview没有绑定到数据库。简单地说,我有两个不同窗体上的DataGridviews——一个本质上是一个字典(Dictionary),另一个是数据输入表格(有点像Excel)。数据输入 Datagrid 与字典 Datagrid 相互交叉参照。我已经解决了这个问题,但是我需要做的是——如果编辑后的数据单元格不在字典中,则不能移动到另一个单元格(即被卡在该单元格中,直到添加正确的字典值为止)。我目前可以让它显示消息框“不在字典中”,但是我的代码不允许移出该单元格的功能不起作用。
以下是代码:
Private Sub dataGridView1_CellEndEdit(ByVal sender As Object,
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
Handles DataGridView1.CellEndEdit
Dim Row As Integer = DataGridView1.CurrentRow.Index
Dim temp As Integer = 0
Try
For i As Integer = 0 To FormGeoDicLith.DataGridViewDicLith.RowCount - 1
'this code references a column in the datagrid dictionary to see if the correct value has been added to the data entry datagrid
If DataGridView1.Rows(Row).Cells(2).Value = FormGeoDicLith.DataGridViewDicLith.Rows(i).Cells(1).Value Then
MsgBox("Item found")
temp = 1
End If
Next
If temp = 0 Then
'this is the problem area
DataGridView1.Rows(Row).Cells(2).Selected = True
MsgBox("Code Not In Dictionary")
Exit Sub
End If
Catch ex As Exception
End Try
End Sub
问题是DataGridView1.Rows(Row).Cells(2).Selected = True,尽管它看起来选择了单元格,但它只是取消选择,我不能像想要的那样一直停留在单元格中,直到输入正确的字典项。非常感谢您的帮助。