如何对 DataGridView 中的特定 DataGridViewTextBoxColumn 列执行验证,以便用户需要在其中输入值?
问问题
5984 次
1 回答
6
我认为您正在寻找数据网格视图文本框列验证对吗?如果是这样,请你看看这个链接
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;
}
}
}
}
}
注意:此代码未经测试..
于 2011-09-30T19:31:54.383 回答