2013-07-11 52 views
0

我正在創建一個Form,其中包含Bound Datagridview在完成輸入後強制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,我通過添加新行Datagridview。那麼當用戶輸入完成並按下回車鍵後,如何強制Row_Validating觸發?

我找到了this解決方案,但它不適合我的情況,因爲我不想將Enable Adding設置爲True。我想通過代碼來處理行添加。

回答

0

我找到了解決辦法,我子類DataGridView並覆蓋ProcessDialogKey這樣的:

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

(我寫在C#這個子類)

然後在我的形式,這樣的

手柄按鍵輸入按
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 

會觸發行驗證

相關問題