.NET 2.0 - WinForm Control - DataGridView 编程36计(二)

本文介绍了 .NET2.0 中 DataGridView 控件的错误图标显示方法、单元格输入验证及错误处理技巧。包括如何设置 ErrorText 属性显示错误图标、使用 CellErrorTextNeeded 和 RowErrorTextNeeded 事件进行批量错误标记,以及 CellValidating 事件防止非法输入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

.NET 2.0 - WinForm Control - DataGridView 编程36计(二)

目录:


① DataGridView  Error图标表示的设定:

GO TO TOP
为了提醒用户注意,DataGridView可以使用Error图标来突出显示。如下图:


Error图标可以在单元格和行头内表示,但不能在列头上显示。 

1) ErrorText属性
当设定单元格/行的ErrorText属性的内容后,单元格/行的Error图标就会被表示出来。另外,只有在DataGridView.ShowCellErrors = True时,Error图标才能显示。(默认即时True)
[VB.NET]
'  设定 (0, 0) 的单元格表示 Error图标
DataGridView1(0, 0).ErrorText = "请确认单元格的值。"

'  设定第4行(Index=3)的行头显示Error图标
DataGridView1.Rows(3).ErrorText = "不能输入负值。"

2)  CellErrorTextNeeded、RowErrorTextNeeded 事件
即时输入时的Error图标的表示,可以使用CellErrorTextNeeded事件
同时,在大量的数据处理时,需要进行多处的内容检查并显示Error图标的应用中。遍历单元格设定ErrorText的方法是效率低下的,应该使用CellErrorTextNeeded事件。行的Error图标的设定则应该用RowErrorTextNeeded事件。
但是,需要注意的是当DataSource属性设定了VirtualMode=True时,上述事件则不会被引发。

[VB.NET]
' CellErrorTextNeeded 事件处理方法
Private  Sub DataGridView1_CellErrorTextNeeded( ByVal sender  As  Object, _
         ByVal e  As DataGridViewCellErrorTextNeededEventArgs) _
         Handles DataGridView1.CellErrorTextNeeded
     Dim dgv  As DataGridView =  CType(sender, DataGridView)
     '  单元格值为负整数时,Error图标被表示。
     Dim cellVal  As  Object = dgv(e.ColumnIndex, e.RowIndex).Value
     If  TypeOf cellVal  Is  Integer  AndAlso  CInt(cellVal) < 0  Then
        e.ErrorText = "不能输入负整数。"
     End  If
End Sub

' RowErrorTextNeeded 事件处理方法
Private  Sub DataGridView1_RowErrorTextNeeded( ByVal sender  As  Object, _
         ByVal e  As DataGridViewRowErrorTextNeededEventArgs) _
         Handles DataGridView1.RowErrorTextNeeded
     Dim dgv  As DataGridView =  CType(sender, DataGridView)
     If dgv("Column1", e.RowIndex).Value  Is DBNull.Value  AndAlso _
        dgv("Column2", e.RowIndex).Value  Is DBNull.Value  Then
        e.ErrorText = _
            "Column1和Column2必须输入一个值。"
     End  If
End Sub

 

[C#]
//  CellErrorTextNeeded 事件处理方法
private  void DataGridView1_CellErrorTextNeeded( object sender,
    DataGridViewCellErrorTextNeededEventArgs e)
{
    DataGridView dgv = (DataGridView)sender;
     //  单元格值为负整数时,Error图标被表示。
     object cellVal = dgv[e.ColumnIndex, e.RowIndex].Value;
     if (cellVal  is  int && (( int)cellVal) < 0)
    {
        e.ErrorText = "不能输入负整数。";
    }
}

//  RowErrorTextNeeded 事件处理方法
private  void DataGridView1_RowErrorTextNeeded( object sender,
    DataGridViewRowErrorTextNeededEventArgs e)
{
    DataGridView dgv = (DataGridView)sender;
     if (dgv["Column1", e.RowIndex].Value == DBNull.Value &&
        dgv["Column2", e.RowIndex].Value == DBNull.Value)
    {
        e.ErrorText = 
            "Column1和Column2必须输入一个值。";
    }
}


 DataGridView  单元格入力值的验证:

如果想在单元格入力之后验证其入力值并在不正确的场合取消之后的动作,那么请使用事件:CellValidating。如下代码所示:当“Column1”为空的时候,则在该行设定为错误图标,并且取消之后的动作。(焦点无法离开该单元格)

[VB.NET]
' CellValidating事件处理方法
Private  Sub DataGridView1_CellValidating( ByVal sender  As  Object, _
         ByVal e  As DataGridViewCellValidatingEventArgs) _
         Handles DataGridView1.CellValidating
     Dim dgv  As DataGridView =  CType(sender, DataGridView)

     If dgv.Columns(e.ColumnIndex).Name = "Column1"  AndAlso _
            e.FormattedValue.ToString() = ""  Then
         ' 行的错误提示的设定
        dgv.Rows(e.RowIndex).ErrorText = "值未输入。"
         '取消已经输入的内容,还原成上次的输入内容。
         ' dgv.CancelEdit()
         '取消之后的动作
        e.Cancel =  True
     End  If
End Sub

' CellValidated事件处理方法
Private  Sub DataGridView1_CellValidated( ByVal sender  As  Object, _
         ByVal e  As DataGridViewCellEventArgs) _
         Handles DataGridView1.CellValidated
     Dim dgv  As DataGridView =  CType(sender, DataGridView)
     '验证通过的话,则清空行的错误提示
    dgv.Rows(e.RowIndex).ErrorText =  Nothing
End Sub

 

 DataGridView  用户输入不正确的时候的错误捕获处理

比如,数字型的列中输入不正确的值的时候处理发生错误的时候,会出现图标。但是图标表示出来,不会第一时间通知用户。

这里介绍另一种处理方式:绑定DataError事件进行处理。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值