没有想PB那样在数据窗口设计器那样直接输入验证条件,要自己写。可以通过DataGrid控件对象的CurrentCellChanged事件来实现对Cell数据的验证。
其中IsValidValue()函数用来判断Cell中的数据是否符合要求,根据实际需要进行编写相应的代码。
private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
{
newCurrentRow = dataGrid1.CurrentCell.RowNumber;
newCurrentCol = dataGrid1.CurrentCell.ColumnNumber;
string newText = dataGrid1[oldCurrentRow, oldCurrentCol].ToString();
if( okToValidate && !IsValidValue(oldCurrentRow, oldCurrentCol, newText))
{
MessageBox.Show("Entry Error");
okToValidate = false;
dataGrid1.CurrentCell = new DataGridCell(oldCurrentRow, oldCurrentCol);
okToValidate = true;
}
oldCurrentRow = newCurrentRow;
oldCurrentCol = newCurrentCol;
}
其中IsValidValue()函数用来判断Cell中的数据是否符合要求,根据实际需要进行编写相应的代码。
private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
{
newCurrentRow = dataGrid1.CurrentCell.RowNumber;
newCurrentCol = dataGrid1.CurrentCell.ColumnNumber;
string newText = dataGrid1[oldCurrentRow, oldCurrentCol].ToString();
if( okToValidate && !IsValidValue(oldCurrentRow, oldCurrentCol, newText))
{
MessageBox.Show("Entry Error");
okToValidate = false;
dataGrid1.CurrentCell = new DataGridCell(oldCurrentRow, oldCurrentCol);
okToValidate = true;
}
oldCurrentRow = newCurrentRow;
oldCurrentCol = newCurrentCol;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货