UltraGrid的使用(二):单元格输入内容的验证
原文:http://blog.csdn.net/szm220/archive/2007/07/28/1713727.aspx
UltraGrid提供很好的编辑功能,既然能编辑,当然就不能让他随便的输入,这就需要对输入单元格内容的验证了,以及验证结果的提示。举一个EMAIL输入的验证。下面是验证过程。
public bool CheckEmail(string strEmail)
{
string MailPattern = @"([w-.]+)@([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.]|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})";
Match m = Regex.Match(strEmail,@"^s*" + MailPattern + @"s*$");
if(m.Success)
{
return true;
}
return false;
}
{
string MailPattern = @"([w-.]+)@([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.]|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})";
Match m = Regex.Match(strEmail,@"^s*" + MailPattern + @"s*$");
if(m.Success)
{
return true;
}
return false;
}
别人说验证的事件在Validating里验证比较好,但是我用这个事件代码在里面根本就不运行。我感觉还是用AfterCellUpdate比较好。
private void ultraGrid1_AfterCellUpdate(object sender, Infragistics.Win.UltraWinGrid.CellEventArgs e)
{
int i = this.ultraGrid1.ActiveRow.Index;
string enterip = this.ultraGrid1.Rows[i].Cells[3].Value.ToString().Trim();
bool str = CheckEmail(enterip);
DataTable table = ds.Tables["tb_city_info"];
DataRow row = table.Rows[i];
DataColumn column = table.Columns["email"];
row.SetColumnError(column, "");
if (str == false)
{
row.SetColumnError(column, "sdfsdfssd");
}
}
{
int i = this.ultraGrid1.ActiveRow.Index;
string enterip = this.ultraGrid1.Rows[i].Cells[3].Value.ToString().Trim();
bool str = CheckEmail(enterip);
DataTable table = ds.Tables["tb_city_info"];
DataRow row = table.Rows[i];
DataColumn column = table.Columns["email"];
row.SetColumnError(column, "");
if (str == false)
{
row.SetColumnError(column, "sdfsdfssd");
}
}
不知道你能不能看懂,首先是获取输入内容enterip,然后验证,将结果返回给str,接下来找到你点击单元格所在的行和列,并对他进行清除错误样式(这样做是输入错误后再输入正确的话,可以将错误表记清除),然后对结果判断,并设置错误原因(“”内是原因)。
OK!不要以为这样就结束了。NO,这样根本就没达到要求的效果。
//验证错误显示
private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
{
e.Layout.Override.SupportDataErrorInfo = Infragistics.Win.UltraWinGrid.SupportDataErrorInfo.CellsOnly;
e.Layout.Override.DataErrorCellAppearance.ForeColor = Color.Red;
}
private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
{
e.Layout.Override.SupportDataErrorInfo = Infragistics.Win.UltraWinGrid.SupportDataErrorInfo.CellsOnly;
e.Layout.Override.DataErrorCellAppearance.ForeColor = Color.Red;
}