如何对我的 DataGridView 中的特定 DataGridViewTextBoxColumn 列执行验证,以便用户需要在其中输入值?
最佳答案
我认为您正在寻找数据 GridView 文本框列验证,对吗?如果是的话,请看一下这个链接
http://www.codeproject.com/Questions/93691/Validations-inside-DataGridView-TextboxColumn.aspx
编辑 1:
你可以使用这个解决方案,但它只验证数字,或者如果你想验证文本,你可以更改代码..
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
DataGridViewTextBoxCell cell = dataGridView1[2, e.RowIndex] as DataGridViewTextBoxCell;
if (cell != null)
{
if (e.ColumnIndex == 2)
{
char[] chars = e.FormattedValue.ToString().ToCharArray();
foreach (char c in chars)
{
if (char.IsDigit(c) == false)
{
MessageBox.Show("You have to enter digits only");
e.Cancel = true;
break;
}
}
}
}
}
注意:此代码未经测试..
关于c# - 对单个 DataGridView 列的验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7615050/