【问题标题】:Force datagridview call row validating after finish input完成输入后强制datagridview调用行验证
【发布时间】:2013-07-11 08:29:07
【问题描述】:

我正在创建一个Form,里面有一个Bound Datagridview。在Form_LoadRow_Validating 上,我通过以下方式向Datagridview 添加了新行:

Private Sub PurchaseInRowAdd()
        'add new row to datagridview
        Dim dtRow As DataRow = CType(Me.dgvPurchaseIn.DataSource, DataTable).NewRow()
        dtRow.Item("InvoiceID") = 0
        dtRow.Item("KindID") = CType(CType(Me.dgvPurchaseIn.Columns("colPurchaseKind"), DataGridViewComboBoxColumn).DataSource, DataTable).Rows(0)("KindID")
        dtRow.Item("InvoiceSign") = ""
        dtRow.Item("InvoiceNo") = ""
        dtRow.Item("InvoiceDate") = New Date(objController.ProcessYear, objController.ProcessMonth, 1)
        dtRow.Item("ID") = CType(CType(Me.dgvPurchaseIn.Columns("colPurchaseCustomer"), DataGridViewComboBoxColumn).DataSource, DataTable).Rows(0)("ID")
        dtRow.Item("Product") = ""
        dtRow.Item("Price") = "0.00"
        dtRow.Item("Note") = ""
        dtRow.Item("Tax") = CType(CType(Me.dgvPurchaseIn.Columns("colPurchaseKind"), DataGridViewComboBoxColumn).DataSource, DataTable).Rows(0)("Tax")
        dtRow.Item("TaxCode") = CType(CType(Me.dgvPurchaseIn.Columns("colPurchaseCustomer"), DataGridViewComboBoxColumn).DataSource, DataTable).Rows(0)("TaxCode")
        dtRow.Item("VAT") = ""

        CType(Me.dgvPurchaseIn.DataSource, DataTable).Rows.Add(dtRow)
    End Sub

这里的问题是,当用户在新行中完成输入并按回车时,Row_Validating 没有被触发,因为它下面没有行。那么如何在用户完成输入并按回车时强制Row_Validating 触发?

我找到了this 解决方案,但它不适合我的情况,因为我不想将Enable Adding 设置为True。我想改为通过代码处理行添加。

【问题讨论】:

    标签: vb.net datagridview .net-2.0 datagridviewrow


    【解决方案1】:

    我找到了解决方案,我将DataGridView 子类化并覆盖ProcessDialogKey,如下所示:

    protected override bool ProcessDialogKey(Keys keyData)
            {
                if(keyData == Keys.Enter)
                {
                    KeyEnterPress(this, new EventArgs());
                }
                return base.ProcessDialogKey(keyData);
            }
    

    (我用 C# 写了这个子类)

    然后像这样在我的表单中处理 key enter press

    Private Sub PurchaseInCellKeyDown(ByVal sender As Object, ByVal e As EventArgs)
            If Me.dgvPurchaseIn.CurrentRow.Index = Me.dgvPurchaseIn.Rows.Count - 1 Then
                If PurchaseInRowValidate(Me.dgvPurchaseIn.CurrentRow.Index, True) Then
                    Me.PurchaseInRowAdd()
                    Me.deselectPurchaseCell(Me.dgvPurchaseIn.CurrentRow.Index)
                    Me.dgvPurchaseIn.Rows(Me.dgvPurchaseIn.Rows.Count - 1).Cells("colPurchaseSign").Selected = True
                End If
            End If
        End Sub
    

    这一行:

    Me.dgvPurchaseIn.Rows(Me.dgvPurchaseIn.Rows.Count - 1).Cells("colPurchaseSign").Selected = True
    

    将触发行验证

    【讨论】:

      猜你喜欢
      • 2016-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多