【问题标题】:VB Getting DataGridView to stay on same cell if validation (CellEndEdit fails如果验证(CellEndEdit 失败),VB 让 DataGridView 留在同一个单元格上
【发布时间】: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


【解决方案1】:

您可以使用DataGridView.CellValidating Event,当单元格失去输入焦点时会发生这种情况,从而启用内容验证。

例如:

Private Sub DataGridView1_CellValidating(sender As Object, e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) _
    Handles DataGridView1.CellValidating

    If e.FormattedValue < 0 Or e.FormattedValue > 20 Then
        MsgBox("Please specify a valid value between 0 and 20.")
        e.Cancel = True
    End If

End Sub

您只能通过e.ColumnIndex 验证特定的列过滤。

【讨论】:

  • Cell 验证事件对我的代码造成严重破坏 - 我在“endit”之前尝试过。
  • 结束编辑适用于一切,除了它不允许退出单元格的能力 - 我玩过按键字符和按键(我最初的想法是在旅途中验证并创建自动单元格在正确输入后移动——但这也产生了各种意想不到的后果。
  • 如果您在特定过程中遇到 CellValidating 问题(即:当您第一次填充 DataGridView 时)尝试使用 AddHandler 和 RemoveHandler 动态添加/删除此事件(链接:msdn.microsoft.com/en-us/library/6yyk8z93%28v=vs.90%29.aspx) .
  • 我最终选择了 Telerik Radgrid 并使用了单元格验证事件。它引起的戏剧性减少了很多。
【解决方案2】:

我已经取得了一些进展——

DataGridView1.CurrentCell = DataGridView1.Rows(Row).Cells(2)

            MsgBox("Code Not In Dictionary")

            DataGridView1.BeginEdit(True)

开始编辑功能确实将我拉回单元格,尽管选择了下一个单元格,然后将其拉回正确的单元格并进入编辑模式。在 Msgbox 之后需要按两次 Enter 键而不是按一次,如果我不断输入错误的值/由于某种原因最终没有选择单元格,它似乎会失败。

【讨论】:

  • 实际上-它回到单元格然后你认为你正在编辑单元格,但后来它奇怪地决定将值放在它旁边的单元格中(非常奇怪)
  • 我最终放弃了股票 DatagridView 并转向 Telerik Radgrid。通过这样做,我设法摆脱了上述所有问题(有些戏剧性,但比 Datagridview 少得多)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-07
  • 1970-01-01
  • 1970-01-01
  • 2015-07-07
  • 2012-02-03
  • 1970-01-01
相关资源
最近更新 更多