2016-08-11 56 views
1

我有3列的DataGridView CellValidating工作不完美

  • 產品名稱

  • 在手

    數量
  • 要轉移的數量

的項目名稱和手頭數量將顯示給用戶 。如果他試圖輸入一個數量超過他手邊的數量;我需要一個消息框彈出...


這是我曾嘗試

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

    With DataGridView1.Rows(e.RowIndex) 
       If e.ColumnIndex = 2 Then '' this is the col index for qty to be transfered 
        If .Cells(2).Value > .Cells(1).Value Then 
         MessageBox.Show("You don't Have sufficient quantity ", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error) 
         e.Cancel = True 
        End If 
       End If 
      End With 
end sub 

但是該事件不觸發的確切時間我想要的。它允許我離開該行而不彈出消息。

回答

0

的值不會寫入到單元還,所以使用正在傳遞給你從事件的價值:因爲價值和FormattedValue的SET選項嚴明就在你的代碼:

If Convert.ToInt32(e.FormattedValue) > Convert.ToInt32(.Cells(1).Value) Then 

旁註返回對象

+0

沒有錯誤,它的工作。感謝您的提示,將會銘記未來。謝謝。 –

相關問題