C# 在按键事件中,使特定列仅接受datagridview中的数值
我需要使datagridview仅在keypress事件中接受特定列的数值。有什么最好的方法可以做到这一点吗?您必须这样使用:C# 在按键事件中,使特定列仅接受datagridview中的数值,c#,datagridview,keypress,C#,Datagridview,Keypress,我需要使datagridview仅在keypress事件中接受特定列的数值。有什么最好的方法可以做到这一点吗?您必须这样使用: private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { if (e.ColumnIndex == 1) // 1
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
}
}
}
- 添加EditingControlShowing事件
- 在EditingControlShowing中,检查当前单元格是否位于所需列中
- 在EditingControlShowing中注册一个新的按键事件(如果上述条件为真)
- 删除先前在EditingControlShowing中添加的任何按键事件
- 在按键事件中,检查键是否不是数字,然后取消输入
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;
}
}
给出的答案非常好,除非你像其他人指出的那样需要小数点。 在这种情况下,您需要扩展验证,在下面添加using和vars以获取十进制分隔符的区域性变量值
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;
}
您也可以尝试这种方式,使用接受小数字符
private void Column1_KeyPress(object sender, KeyPressEventArgs e)
{
//allow number, backspace and dot
if (!(char.IsDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == '.'))
{
e.Handled = true;
}
//allow only one dot
if (e.KeyChar == '.' && (sender as TextBox).Text.Contains("."))
{
e.Handled = true;
}
}
我不是在批评这个答案,而是说了一句笼统的话:如果能够在一个列上设置某种格式,并使用默认的格式设置程序,比如
AllowNumericOnly
等等,那不是很好吗。就像一处房产。回答得很好!这样可以防止他们输入无效数据。不过,我想补充一点,使用硬编码的列索引不是一个好主意。我建议使用if(dataGridView1.CurrentCell.ColumnIndex==dataGridView1.Columns[“name”].Index)
@druciferre更好:dataGridView1.CurrentCell.ColumnIndex==dataGridView.Columns.IndexOf(dataGridViewColumn1)
@PontusMagnusson,如果您已经为列定义了一个控件,您就不能使用dataGridViewColumn1.ColumnIndex
?很好的答案,这里要添加的一点是,这不会阻止用户从上下文菜单粘贴。正确设置答案的格式与扩展验证类似。但我注意到,十进制字符从未被接受为有效的KeyChar,因为即使在文本框中输入第一个十进制,KeyPressEventArgs也返回true。我添加了else if(e.KeyChar==decselector){e.Handled=false;}
作为IndexOf条件的一部分。ThanksObject引用未设置为对象的实例。是否可以在此事件中添加允许一个小数的代码?如果有人需要允许小数,请将“int.TryParse”更改为“decimal.TryParse”
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;
}
private void Column1_KeyPress(object sender, KeyPressEventArgs e)
{
//allow number, backspace and dot
if (!(char.IsDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == '.'))
{
e.Handled = true;
}
//allow only one dot
if (e.KeyChar == '.' && (sender as TextBox).Text.Contains("."))
{
e.Handled = true;
}
}