2012-05-31 42 views
2

防止用戶輸入逗號值到datagridveiw單元格的最簡單方法是什麼? c#winforms。c#winforms datagridview如何限制單元格輸入?

+0

的[http://stackoverflow.com/questions/5687670/let-only-some-chars-be-typed-in-a-datagridview-cell][1] 可能的複製 [1]:http://stackoverflow.com/questions/5687670/let-only-some-chars-be-typed-in-a-datagridview-cell – openshac

回答

2

當您嘗試在單元格中鍵入試試這個這將防止進入逗號

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 
{ 
     e.Control.KeyPress += new KeyPressEventHandler(Control_KeyPress); 
} 

void Control_KeyPress(object sender, KeyPressEventArgs e) 
{ 
     if (e.KeyChar == 44) 
     { 
      e.Handled = true; 
     } 
} 

不要忘了綁定到EditingControlShowing事件在DataGridView

注意

要精確檢查並限制它的控件類型,您可以檢查控件類型(例如對於TextBox,您可以這樣做)

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 
    { 
     if (e.Control is TextBox) 
     { 
      e.Control.KeyPress += new KeyPressEventHandler(Control_KeyPress); 
     } 
    } 
+1

在添加事件處理程序之前,我們可以檢查e.Control類型。 –

+0

@Romil正確。編輯後 –

+1

檢查是否(e.Control是TextBox),它會保存變量聲明。 –

相關問題