2013-10-16 44 views
0

全部,我想在完成輸入DataGridView單元格的值後捕捉事件,以便驗證該值。發生焦點丟失時是否有任何事件DataGridView?什麼是最好的方式來做到這一點?謝謝。失去焦點時驗證單元格值

回答

1

您應該爲此使用CellValidating事件,請參閱此處MSDN

發生在單元失去輸入焦點時啓用內容驗證。

和示例(從MSDN以及)

private void dataGridView1_CellValidating(object sender, 
    DataGridViewCellValidatingEventArgs e) 
{ 
    dataGridView1.Rows[e.RowIndex].ErrorText = ""; 
    int newInteger; 

    // Don't try to validate the 'new row' until finished 
    // editing since there 
    // is not any point in validating its initial value. 
    if (dataGridView1.Rows[e.RowIndex].IsNewRow) { return; } 
    if (!int.TryParse(e.FormattedValue.ToString(), 
     out newInteger) || newInteger < 0) 
    { 
     e.Cancel = true; 
     dataGridView1.Rows[e.RowIndex].ErrorText = "the value must be a non-negative integer"; 
    } 
} 
+0

如果我想恢復原來的值時,驗證未通過。如何做到這一點。謝謝。 –

+0

您必須自己實現這樣的功能。 – gzaxx

+0

我使用的是'cellvaluechanged'事件。謝謝。 –