2012-09-28 20 views

回答

53
  • 添加事件EditingControlShowing
  • 的在EditingControlShowing,檢查如果當前單元格位於所需的列。
  • 在EditingControlShowing中註冊KeyPress的新事件(如果上述條件爲真)。
  • 刪除先前在EditingControlShowing中添加的任何KeyPress事件。
  • 在KeyPress事件中,檢查如果鍵不是數字,則取消輸入。

例子:

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 
{ 
    e.Control.KeyPress -= new KeyPressEventHandler(Column1_KeyPress); 
    if (dataGridView1.CurrentCell.ColumnIndex == 0) //Desired Column 
    { 
     TextBox tb = e.Control as TextBox; 
     if (tb != null) 
     { 
      tb.KeyPress += new KeyPressEventHandler(Column1_KeyPress); 
     } 
    } 
} 

private void Column1_KeyPress(object sender, KeyPressEventArgs e) 
{ 
    if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar)) 
    { 
     e.Handled = true; 
    } 
} 
+3

我不是批評這個答案,但是提出了一個普遍的評論:能夠使用默認的格式化器如'AllowNumericOnly'等設置某種列的格式化器不是很好。就像一個財產。 –

+4

優秀的答案!這使他們遠離輸入無效數據。我想補充一點,使用硬編碼列索引不是一個好主意。我建議使用'if(dataGridView1.CurrentCell.ColumnIndex == dataGridView1.Columns [「name」]。Index)' –

+0

@druciferre更好:'dataGridView1.CurrentCell.ColumnIndex == dataGridView.Columns.IndexOf(dataGridViewColumn1);' –

25

您必須使用DataGridView.CellValidating Event這樣的:

private void dataGridView1_CellValidating(object sender, 
              DataGridViewCellValidatingEventArgs e) 
    { 
     if (e.ColumnIndex == 1) // 1 should be your column index 
     { 
      int i; 

      if (!int.TryParse(Convert.ToString(e.FormattedValue), out i)) 
      { 
       e.Cancel = true; 
       label1.Text ="please enter numeric"; 
      } 
      else 
      { 
       // the input is numeric 
      } 
     } 
    } 
3
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 
    { 
     e.Control.KeyPress -= new KeyPressEventHandler(Column1_KeyPress); 
     if (dataGridView1.CurrentCell.ColumnIndex == 4) //Desired Column 
     { 
      TextBox tb = e.Control as TextBox; 
      if (tb != null) 
      { 
       tb.KeyPress += new KeyPressEventHandler(Column1_KeyPress); 
      } 
     } 

    } 
    private void Column1_KeyPress(object sender, KeyPressEventArgs e) 
    { 
      // allowed only numeric value ex.10 
     //if (!char.IsControl(e.KeyChar) 
     // && !char.IsDigit(e.KeyChar)) 
     //{ 
     // e.Handled = true; 
     //} 

       // allowed numeric and one dot ex. 10.23 
     if (!char.IsControl(e.KeyChar)&& !char.IsDigit(e.KeyChar) 
      && e.KeyChar != '.') 
     { 
      e.Handled = true; 
     } 

     // only allow one decimal point 
     if (e.KeyChar == '.' 
      && (sender as TextBox).Text.IndexOf('.') > -1) 
     { 
      e.Handled = true; 
     } 
    } 
1
Private WithEvents txtNumeric As New DataGridViewTextBoxEditingControl 

Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing 

txtNumeric = CType(e.Control, DataGridViewTextBoxEditingControl) 
End Sub 

Private Sub txtNumeric_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtNumeric.KeyPress 
    If (DataGridView1.CurrentCell.ColumnIndex > 0) Then 
     If (Not Char.IsControl(e.KeyChar) And Not Char.IsDigit(e.KeyChar) And Not e.KeyChar = ".") Then 
      e.Handled = True 
     Else 
      'only allow one decimal point 
      If (e.KeyChar = "." And txtNumeric.Text.Contains(".")) Then 
       e.Handled = True 
      End If 
     End If 
    End If 
End Sub 
+0

正確設置您的答案 – HaveNoDisplayName

2

給出的答案是優秀的,除非你需要小數正如其他人指出。 在這種情況下,你需要延長驗證,使用添加和下面瓦爾得到一個文化變量值小數點分隔

using System.Globalization; 

NumberFormatInfo nfi = Thread.CurrentThread.CurrentCulture.NumberFormat; 
char decSeperator; 

decSeperator = nfi.CurrencyDecimalSeparator[0]; 

擴展驗證到:

if (!char.IsControl(e.KeyChar) && !(char.IsDigit(e.KeyChar) 
| e.KeyChar == decSeperator)) 
{ 
    e.Handled = true; 
} 
// only allow one decimal point 
if (e.KeyChar == decSeperator 
    && (sender as TextBox).Text.IndexOf(decSeperator) > -1) 
{ 
    e.Handled = true; 
} 
+0

我喜歡擴展驗證。我注意到,雖然一個十進制字符從來沒有被接受爲有效的KeyChar,因爲即使在輸入文本框中的第一個小數點後,KeyPressEventArgs仍然返回true。我加了'else if(e.KeyChar == decSeparator){e.Handled = false; }作爲IndexOf條件的一部分。謝謝 – voidmain

+0

對象引用未設置爲對象的實例。 –

相關問題