【发布时间】:2015-09-22 14:31:57
【问题描述】:
您好,我对编程很陌生。上个月设法制作了一个有用的程序,现在正在做一些更大的事情——DataGridviews 让我头疼。我的 Datagridview 未绑定到数据库。简而言之,我在两种不同的表单上有两个 DataGridviews——一个本质上是字典——另一个是数据输入表(有点像 excel)。 DataEntry Datagrid 交叉引用 Dictionary Datagrid。我已经完成了这项工作,但是我需要做的是-如果编辑后数据输入表中的数据单元格不在字典中,那么它将不会转到另一个单元格(即卡在该单元格中,直到正确的字典值是已添加。我目前可以让它说 msgbox“不在字典中”但是我不允许移出单元格的代码不起作用-
这里是代码
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 虽然它看起来像是选择了单元格 - 然后它只是取消选择并且我没有像我想要的那样卡在单元格中直到输入了正确的字典项。非常感谢您的帮助。
【问题讨论】:
-
我会调查在 CellEndEdit 之后调用了哪些事件。其中之一可能会阻止您想要的行为。
-
同意,但我没有调用这些事件-它们是由 DatagridView 默认行为调用的
-
确实如此,但您始终可以输入他们的事件处理方法并输入 println,然后查看在 dgv 运行时引发了哪些事件处理方法。
标签: vb.net datagridview