有沒有辦法讓只有某些字符被添加到datagridview單元格? like'1234567890'?讓只有一些字符被鍵入在datagridview單元格
回答
我知道有兩種方法可以用於此目的。第一個(我認爲最好的)是使用DataGridView
上的CellValidating事件並檢查輸入的文本是否是數字。
下面是一個設置行錯誤值的例子(使用額外的CellEndEdit事件處理程序來防止用戶取消編輯)。
private void dataGridView1_CellValidating(object sender,
DataGridViewCellValidatingEventArgs e)
{
string headerText =
dataGridView1.Columns[e.ColumnIndex].HeaderText;
// Abort validation if cell is not in the Age column.
if (!headerText.Equals("Age")) return;
int output;
// Confirm that the cell is an integer.
if (!int.TryParse(e.FormattedValue.ToString(), out output))
{
dataGridView1.Rows[e.RowIndex].ErrorText =
"Age must be numeric";
e.Cancel = true;
}
}
void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
// Clear the row error in case the user presses ESC.
dataGridView1.Rows[e.RowIndex].ErrorText = String.Empty;
}
第二種方法是使用EditingControlShowing事件和事件附着到細胞的按鍵響應 - 我不是這種方法的這樣的粉絲,因爲非數字鍵,它靜靜地阻擋輸入 - 雖然我猜想你可以給出一些反饋(比如響亮的鈴聲),與其他方式相比,它只是感覺更多的工作。
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
e.Control.KeyPress -= TextboxNumeric_KeyPress;
if ((int)(((System.Windows.Forms.DataGridView)(sender)).CurrentCell.ColumnIndex) == 1)
{
e.Control.KeyPress += TextboxNumeric_KeyPress;
}
}
private void TextboxNumeric_KeyPress(object sender, KeyPressEventArgs e)
{
bool nonNumberEntered = true;
if ((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == 8)
{
nonNumberEntered = false;
}
if (nonNumberEntered)
{
// Stop the character from being entered into the control since it is non-numerical.
e.Handled = true;
}
else
{
e.Handled = false;
}
}
與此相關的一個重要注意事項是小心刪除編輯控件中顯示方法的控件上的事件處理程序。這是很重要的,因爲DataGridView
爲同一類型的每個單元重複使用同一個對象,包括跨不同的列。如果您將事件處理程序附加到一個文本框列中的控件,則網格中的所有其他文本框單元格將具有相同的處理程序!此外,還會附加多個處理程序,每次顯示控件時都會有一個處理程序。
第一個解決方案來自this MSDN article。第二個來自this blog。
如果您希望datagridview只是爲用戶刪除無效字符,而不是發出錯誤消息,請使用DataGridView.CellParsing()。此事件僅在您進行單元格編輯後觸發,並允許您覆蓋輸入的內容。
例如:
private void dataGridView1_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
{
// If this is column 1
if (e.ColumnIndex == 1)
{
// Remove special chars from cell value
e.Value = RemoveSpecialCharacters(e.Value.ToString());
e.ParsingApplied = true;
}
}
對於RemoveSpecialCharacters()方法,見this SO question用於從字符串中除去特殊字符的一些優秀的方法。
使用** EditingControlShowing **事件而不是** CellParsing **請參閱[link](https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.editingcontrolshowing(v = vs。 110)的.aspx) – 2017-08-03 03:29:21
- 1. 提交DataGridView單元格編輯只有當某個鍵被按下
- 2. 如何讓一個contextmenu出現在右鍵單擊datagridview單元格
- 3. 如何確定在datagridview中的哪些單元格被檢查?
- 4. 如何在datagridview的最後一行獲取一些單元格?
- 5. 我該如何讓datagridview一次只能選擇同一列中的單元格?
- 6. 將字符插入現有單元格
- 7. DataGridView單元格
- 8. 當按TAB鍵跳過只讀DataGridView中的單元格
- 9. 在DataGridView單元格中插入路徑
- 10. UITableView正在重繪只有一些自定義單元格
- 11. 在DataGridView中顯示單元格內的圖標和字符串
- 12. 將DataGridView中的單個單元格讀取爲字符串
- 13. DataGridView中的換行符單元格vb.net
- 14. 只有最後一個單元格被添加到行
- 15. Datagridview,只顯示唯一值重複單元格值C#2005
- 16. c#DataGridView,總是隻獲得第一個單元格值
- 17. Excel:使用一個單元格中的數字在另一個單元格中寫入一個字符串
- 18. 如果DataGridView的單元格值大於另一個單元格
- 19. 讓鍵盤阻止單元格
- 20. 在Datagridview中格式化單元格
- 21. jquery動畫只有一個單元格
- 22. DataGridView單元格沒有繪製
- 23. WinForms - DataGridView - 沒有選擇單元格
- 24. 只有在滿足某些條件時才添加單元格
- 25. DataGridView在更新後顯示第一行中的一些空單元格
- 26. Excel求和函數,其中一些單元格是字符串
- 27. COUNTIF單元格包含這些字符串之一
- 28. DataGridView怪異字符'q'問題自定義DataGridViewColumn單元格
- 29. datetime毫秒datagridview單元格值到字符串
- 30. datagridview單元格與RTF字符串樣式
乾杯,隊友!!!! – Bahman 2011-04-17 09:32:29
第二種解決方案有兩個問題。首先是一旦Keypress事件在第一列中被替換,它對所有列都有效。第二個是它鏈接到自身,這樣每次在第一列中按下一個鍵時,處理程序會再次被添加。所以每個按鍵事件都會發生多次。 – 2012-12-08 03:43:13
@RichShealer謝謝,通過首先刪除事件來解決這個問題。正如我所說的,在這種情況下,這並不是我的首選方法。 – 2012-12-08 11:47:40