![](/img/trans.png)
[英]How to allow the fileuploader to accept multiple files while insertion and allow to accept only single file while editing row in gridview
[英]How to allow only numbers in specific cell while editing using Winform
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
e.Control.KeyPress -= new KeyPressEventHandler(polNumDataGridViewTextBoxColumn_KeyPress);
if (dataGridView1.CurrentCell.ColumnIndex == 3) //Desired Column
{
TextBox tb = e.Control as TextBox;
if (tb != null)
{
tb.KeyPress += new KeyPressEventHandler(polNumDataGridViewTextBoxColumn_KeyPress);
}
}
}
private void polNumDataGridViewTextBoxColumn_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
gridview 中的控件不是TextBox
。 它是一個DataGridViewTextBoxEditingControl
我做了這個工作測試(我的控件名稱和列索引不同):
private void DataGridView2_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is DataGridViewTextBoxEditingControl tb) {
tb.KeyPress -= Tb_KeyPress;
tb.KeyPress += Tb_KeyPress;
}
}
private void Tb_KeyPress(object sender, KeyPressEventArgs e)
{
if (dataGridView2.CurrentCell.ColumnIndex == 1 &&
!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
但是使用數據綁定通常是更好的方法,它會自動解決問題。 即,當單元格綁定到數字屬性或 DateTime 屬性時,相應的行為。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.