我正在测试它的 cellValidating 事件,该事件有一些问题,或者可能是我遗漏了一些东西。当 radgridview 单元格处于编辑模式时,我无法单击表单上的任何位置,这很好。但是当我单击按钮时,代码会执行并且我的表单会挂起。所以我的问题是,当 radgridview 单元格在 cellValidating 事件期间处于编辑模式时,如何禁用按钮单击时的代码执行。注意:当我调试代码并在 cellValidating 事件和按钮上插入断点时。代码运行良好,但按钮代码不执行。以下是我正在使用的代码。如果有人可以立即回答我将非常感谢他。
私有(private)子 rgv_CellValidating(sender As System.Object, e As Telerik.WinControls.UI.CellValidatingEventArgs) 处理 rgv.CellValidating
Dim column As GridViewDataColumn = TryCast(e.Column, GridViewDataColumn)
If TypeOf e.Row Is GridViewDataRowInfo AndAlso column IsNot Nothing AndAlso column.Name = "ProductId" Then
If String.IsNullOrEmpty(DirectCast(e.Value, String)) OrElse DirectCast(e.Value, String).Trim() = String.Empty Then
e.Cancel = True
DirectCast(e.Row, GridViewDataRowInfo).ErrorText = "Validation error!"
Else
DirectCast(e.Row, GridViewDataRowInfo).ErrorText = String.Empty
End If
End If
最佳答案
您描述的有关按钮代码仍在执行的现象对于功能区菜单按钮和功能区菜单选项卡也是如此。我无法重现挂起问题,所以我想这与您执行的按钮的代码有关。
对于一种可能的解决方法,您可以将私有(private)变量放入表单中,并在单元格验证代码中,如果发生错误,则将其设置为 true;如果没有发生错误,则将其设置为 false(并且自然地在表单初始化代码中将其设置为 false)。
然后在按钮代码中测试变量值,如果其值为 true,则不执行任何代码。
因此,如果您有一个ribbonform(或任何其他形式)作为类:
public partial class MF : RadRibbonForm
{
private bool _hasValidationError;
public MF()
{
...//Normal construction handler code
_hasValidationError = false;
}
void MF_CellValidating(objec sender, CellValidatingEventArgs e)
{
.....//Do validation and set e.Cancel true if the validation fails.
if (e.Cancel)
{
hasValidationError = true;
}
else
{
hasValidationError = false;
}
}
private void MyButton_Click(object sender, EventArgs e)
{
if (_hasValidationError)
{
// Before return you could put in a messagebox that there are cell errors that need to be looked at first
return;
}
else
{
.......//Button code execution occurs here
}
}
使用此代码,您可以确保您不想执行的按钮的代码不会在单元格验证错误时执行。
代码采用 C# 编写,但对于 VB winforms 的工作方式应该类似。
关于winforms - 限制按钮单击 telerik radgridview cellValidating 事件 - winforms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23578653/