winform下dataGridView中限制某列只能输入数字
来源:互联网 发布:淘宝抽检神秘买家地址 编辑:程序博客网 时间:2024/04/28 21:38
方法一
定义一个方法
private void TextBoxDec_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar) && e.KeyChar != '.')
{
e.Handled = true;
}
}
{
if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar) && e.KeyChar != '.')
{
e.Handled = true;
}
}
然后在dataGridView的EditingControlShowing事件中
private void dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (this.dgvFenl.CurrentCell.ColumnIndex == 列索引)
{
e.Control.KeyPress += new KeyPressEventHandler(TextBox_KeyPress);
}
}
{
if (this.dgvFenl.CurrentCell.ColumnIndex == 列索引)
{
e.Control.KeyPress += new KeyPressEventHandler(TextBox_KeyPress);
}
}
方法二
在dataGridView的CellValidating事件中
private void dgvTix_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
dgvTix.Rows[e.RowIndex].ErrorText = "";
if (e.ColumnIndex == 3)
{
int newInteger = 0;
if (!int.TryParse(e.FormattedValue.ToString(), out newInteger) || newInteger < 0)
{
e.Cancel = true;
dgvTix.Rows[e.RowIndex].ErrorText = "题数格式错误,请重新输入。";
MessageBox.Show("题数格式错误,请重新输入。");
return;
}
}
else if (e.ColumnIndex == 4)
{
float newFloat = 0.00f;
if (!float.TryParse(e.FormattedValue.ToString(), out newFloat) || newFloat < 0.00)
{
e.Cancel = true;
dgvTix.Rows[e.RowIndex].ErrorText = "分数格式错误,请重新输入";
MessageBox.Show("分数格式错误,请重新输入。");
return;
}
}
}
{
dgvTix.Rows[e.RowIndex].ErrorText = "";
if (e.ColumnIndex == 3)
{
int newInteger = 0;
if (!int.TryParse(e.FormattedValue.ToString(), out newInteger) || newInteger < 0)
{
e.Cancel = true;
dgvTix.Rows[e.RowIndex].ErrorText = "题数格式错误,请重新输入。";
MessageBox.Show("题数格式错误,请重新输入。");
return;
}
}
else if (e.ColumnIndex == 4)
{
float newFloat = 0.00f;
if (!float.TryParse(e.FormattedValue.ToString(), out newFloat) || newFloat < 0.00)
{
e.Cancel = true;
dgvTix.Rows[e.RowIndex].ErrorText = "分数格式错误,请重新输入";
MessageBox.Show("分数格式错误,请重新输入。");
return;
}
}
}
- winform下dataGridView中限制某列只能输入数字
- datagridview 限制某列只能输入数字
- datagridview 限制某列只能输入数字
- datagridview 限制某列只能输入数字
- dataGridView中限制某列/单元格只能输入数字
- DataGridView中限制某列只能输入数字
- datagridview 限制某列只能输入数字(方法一)
- C# Winform 中验证DataGridView单元格内容--限制单元格中只能输入数字
- DataGridView 列大写、列只能输入数字
- C#中DataGridView实现某一列只能输入数字
- C#中DataGridView实现某一列只能输入数字
- dataGridView中只能输入数字
- C# datagridview 某列只能输入英文或数字
- C#Winform限制Textbox只能输入数字
- C# winform中限制只能输入小数
- datagridview 只能输入数字
- dataGridView中只能输入数字的判断
- C#Winform下用正则表达式限制TextBox只能输入数字
- 关于websphere的配置和管理监控问题
- 4.4.4 特殊字符验证
- 第一周--星期一(2008年3月24)--重新开始规划自己的生活
- IE地址栏可输入的最大字符数
- SQL注入的实现原理和防范
- winform下dataGridView中限制某列只能输入数字
- 企业信息化建设
- 平台架构——体系结构
- 阿里巴巴马云反串白雪公主照曝光
- 数据窗口中的字段如何设置自动换行
- [职业经历]实用问答——如何用电子邮件发送简历?(转载)
- 4.4.3 中文文本验证
- oracle unable to load oraMTS , unable to enlist in a distributed transaction .
- 百度将任命李昕晢为新首席财务官 31日正式生效