在telerik winforms下显示cellvalidation错误消息

时间:2014-05-06 06:00:30

标签: 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;
        }
    }
}

3 个答案:

答案 0 :(得分:2)

查找在鼠标悬停在单元格上时触发的事件,如果存在验证错误,则从代码中触发该事件。它不是最优雅的解决方案,但它应该有效。

答案 1 :(得分:1)

我使用Telerik WPF RadGridView控件,但它仍然是Windows命名空间,试一试。

/// <summary>
/// RadGridView cell validating.
/// </summary>
/// <param name="e">
/// The e.
/// </param>
private void CellValidating(GridViewCellValidatingEventArgs e)
{
  bool isValid = true;
  string validationText = "Validation failed. ";
  GridViewCell cell = e.Cell;
  switch (cell.Column.UniqueName)
  {
    case "Code":
    case "Name":
    case "County":
    case "Region":
      isValid = e.NewValue != null && !string.IsNullOrWhiteSpace(e.NewValue.ToString());
      if (!isValid)
      {
        validationText += string.Format("{0} is required.", cell.Column.UniqueName);
      }

      break;

      /* Continue case statements... */

  }

  if (!isValid)
  {
    MarkCell(cell, validationText);
  }
  else
  {
    RestoreCell(cell);
  }

  e.ErrorMessage = validationText;
  e.IsValid = isValid;
}

/// <summary>
/// Marks the cell with a red box and tooltip of the validation text.
/// </summary>
/// <param name="cell">
/// The cell.
/// </param>
/// <param name="validationText">
/// The validation text.
/// </param>
private void MarkCell(Control cell, string validationText)
{
  ToolTipService.SetToolTip(cell, validationText);
}

/// <summary>
/// Restores the cell and removes the tooltip.
/// </summary>
/// <param name="cell">
/// The cell.
/// </param>
private void RestoreCell(Control cell)
{
  ToolTipService.SetToolTip(cell, null);
}

答案 2 :(得分:0)

为了设法打印工具提示并在鼠标悬停在单元格上时可以看到它,需要做一些事情。请注意,我的网格在示例源中称为myGrid,而表单名为myForm。

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

private ToolTip _tooltip;
private Point _mouse;

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

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

    _tooltip = new ToolTip();
    this.myGrid.CellEditorInitialized += myGrid_CellEditorInitialized;
    this.myGrid.CellValidating+= myGrid_CellValidating;
}

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

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;
}

完成此操作后,我们需要确保当celleditor初始化时,即工具提示 在需要的时候做好准备:

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);  
}

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

下一步是定义mousemove函数,因此WHEN将显示工具提示:

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;  
        }
    }
}

在此之后,我们还需要让工具提示再次消失,这与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);  
            }
        }

完成这些步骤后,我们会有一个工具提示,当用户将鼠标移动到单元格时会出现,并在他离开单元格时再次消失。