在telerik winforms下显示cellvalidation错误消息

Tho*_*mas 5 c# validation telerik radgridview

我有一个包含多个列的Telerik.WinControls.UI.RadGridView.我正在使用rowvalidation和cellvalidation的混合来验证我得到的输入(虽然对于当前问题我也尝试停用rowvalidation但仍然得到相同的结果).

我有一个daterow,我使用cellvalidating事件来验证它(否则如果用户键入错误的日期我得到一个例外).我期望的行为是显示错误消息并且未验证单元格.第二件事有效,但是当我将鼠标移动到单元格的边框上时,仅显示错误消息(否则它只是未显示).

所以我的问题是如何才能实现错误消息尽快显示并且只要通过验证发现错误?

这是我使用的cellvalidation代码:

void MainFormGridView_CellValidating(object sender, CellValidatingEventArgs eventArgs)
{
    var currentCell = eventArgs.Row.Cells[eventArgs.ColumnIndex];

    if (eventArgs.Column.Name == "OrderDate")
    {
        if (eventArgs.Value == null)
        {
            eventArgs.Cancel = true;
        }
        else
        {
            try
            {
                DateTime dateValue;
                if (!DateTime.TryParse(eventArgs.Value.ToString(), out dateValue))
                {
                    eventArgs.Cancel = true;
                }
            }
            catch
            {
                // Error occured so validation error!
                eventArgs.Cancel = true;
            }
        }
        if (eventArgs.Cancel)
        {
            currentCell.ErrorText = "Error no valid date! Please type in a valid date";
        }
        else
        {
            currentCell.ErrorText = string.Empty;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Tho*_*mas 1

为了设法打印工具提示并在鼠标悬停在单元格上时使其可见,需要完成一些操作。请注意,我的网格在示例源代码中称为 myGrid,而表单称为 myForm。

我们需要做的第一件事是定义我们需要的 2 个辅助变量:

private ToolTip _tooltip;
private Point _mouse;
Run Code Online (Sandbox Code Playgroud)

然后我们需要定义工具提示,并且需要为我们需要的事件定义一个自定义处理程序

public myForm()
{
    InitializeComponent();
    ....//your additional initialization code

    _tooltip = new ToolTip();
    this.myGrid.CellEditorInitialized += myGrid_CellEditorInitialized;
    this.myGrid.CellValidating+= myGrid_CellValidating;
}
Run Code Online (Sandbox Code Playgroud)

单元格验证的代码非常简单:

private void myGrid_CellValidating(object sender, CellValidatingEventArgs e)
{
    string errorText = string.Empty;
    // Are we really on a column currently?
    if (e.ColumnIndex >= 0)
    {
        if (e.Value == null)
        {
             errorText = "No field may be empty";
        }
    }

    // Has an error occured? If so don't let the user out of the field until its corrected!
    if (errorText != string.Empty)
    {
        e.Cancel = true;
    }
    e.Row.ErrorText = errorText;
    e.Row.Cells[e.Column.Name].ErrorText = errorText;
}
Run Code Online (Sandbox Code Playgroud)

完成此操作后,我们需要确保在初始化单元格编辑器时,工具提示已准备就绪,以备需要时使用:

private void myGrid_CellEditorInitialized(objec sender, GridViewCellEventArgs e)
{
                RadTextBoxEditor radTextBoxEditor = e.ActiveEditor as RadTextBoxEditor;
                RadTextBoxEditorElement editorElement = radTextBoxEditor.EditorElement as RadTextBoxEditorElement;
                editorElement.AutoToolTip = true;  
                TextBox myTextBox= (TextBox)editorElement.TextBoxItem.HostedControl;  

                myTextBox.MouseMove -= new MouseEventHandler(textBox_MouseMove);  
                myTextBox.MouseLeave -= new EventHandler(textBox_MouseLeave); 
                myTextBox.MouseMove += new MouseEventHandler(textBox_MouseMove);  
                myTextBox.MouseLeave += new EventHandler(textBox_MouseLeave);  
}
Run Code Online (Sandbox Code Playgroud)

因此,首先我们获取编辑器的“文本框”(无论真正存在什么类型的编辑器)并设置该文本框所需的事件,因为我们想知道用户何时将鼠标悬停在文本框上,何时没有。

下一步是定义 mousemove 函数,因此何时应显示工具提示:

void textBox_MouseMove(object sender, MouseEventArgs e)  
{ 
    if (mousePos != e.Location)  
    { 
        RadTextBoxEditor radTextBoxEditor = this.myGrid.ActiveEditor as RadTextBoxEditor;
        GridDataCellElement gridCellElement = radTextBoxEditor.OwnerElement as GridDataCellElement;
        if (gridCellElement != null && gridCellElement.ContainsErrors)
        {
            RadTextBoxEditorElement radTextBoxEditorElement = radTextBoxEditor.EditorElement as RadTextBoxEditorElement;
            TextBox myTextBox = (TextBox)radTextBoxEditorElement.TextBoxItem.HostedControl;  
            _tooltip.Show(gridCellElement.RowInfo.Cells[gridCellElement.ColumnInfo.Name].ErrorText, myTextBox, new Point(e.Location.X + 8, e.Location.Y + 8));  
            _mouse = e.Location;  
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

之后,我们还需要让工具提示再次消失,就像我们使用 mouseleave 事件一样:

void textBox_MouseLeave(object sender, EventArgs e)  
        { 
            RadTextBoxEditor radTextBoxEditor = this.myGrid.ActiveEditor as RadTextBoxEditor;
            RadTextBoxEditorElement radTextBoxEditorElement = radTextBoxEditor.EditorElement as RadTextBoxEditorElement;
            TextBox myTextBox = (TextBox)radTextBoxEditorElement.TextBoxItem.HostedControl;  
            Rectangle textBoxBounds = new Rectangle(myTextBox.PointToScreen(Point.Empty), myTextBox.Size);  
            if (!textBoxBounds.Contains(Control.MousePosition))
            { 
                _tooltip.Hide(textBox);  
            }
        }
Run Code Online (Sandbox Code Playgroud)

完成这些步骤后,我们就得到了一个工具提示,当用户将鼠标移入单元格时,该工具提示会出现,而当用户离开单元格时,该工具提示又会消失。