Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在telerik winforms下显示cellvalidation错误消息_C#_Validation_Telerik_Radgridview - Fatal编程技术网

C# 在telerik winforms下显示cellvalidation错误消息

C# 在telerik winforms下显示cellvalidation错误消息,c#,validation,telerik,radgridview,C#,Validation,Telerik,Radgridview,我有一个Telerik.WinControls.UI.RadGridView,里面有多个列。 我混合使用rowvalidation和cellvalidation来验证我得到的输入(尽管对于当前的问题,我也尝试停用rowvalidation,但仍然得到相同的结果) 我有一个daterow,我使用cellvalidating事件对其进行验证(否则,如果用户键入错误的日期,我会得到一个异常)。我预期的行为是显示错误消息,而单元格未验证。第二件事起作用了,但只有在 我将鼠标移到单元格的边框上(否则就不会

我有一个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;
        }
    }
}

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

我使用的是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);
}
//
///RadGridView单元验证。
/// 
/// 
///e。
/// 
私有void单元验证(GridViewCellValidatingEventArgs e)
{
bool isValid=true;
string validationText=“验证失败。”;
GridViewCell=e.单元;
开关(cell.Column.UniqueName)
{
案例“代码”:
案例“名称”:
案例“县”:
案例“区域”:
isValid=e.NewValue!=null&!string.IsNullOrWhiteSpace(e.NewValue.ToString());
如果(!isValid)
{
validationText+=string.Format(“{0}是必需的。”,cell.Column.UniqueName);
}
打破
/*继续案例陈述*/
}
如果(!isValid)
{
标记单元格(单元格,validationText);
}
其他的
{
恢复细胞;
}
e、 ErrorMessage=validationText;
e、 IsValid=IsValid;
}
/// 
///使用红色框和验证文本的工具提示标记单元格。
/// 
/// 
///牢房。
/// 
/// 
///验证文本。
/// 
专用void标记单元格(控制单元格,字符串验证文本)
{
SetToolTip(单元格,validationText);
}
/// 
///恢复单元格并删除工具提示。
/// 
/// 
///牢房。
/// 
专用无效恢复单元(控制单元)
{
SetToolTip(单元格,空);
}

要打印工具提示并在鼠标悬停在单元格上时使其可见,需要做一些事情。请注意,在示例源代码中,我的网格名为myGrid,而表单名为myForm

我们需要做的第一件事是定义我们需要的两个帮助器变量:

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功能,因此何时显示工具提示:

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

完成这些步骤后,当用户将鼠标移动到单元格中时,将出现一个工具提示,当用户离开单元格时,该提示将再次消失

我不得不说,如果在winforms中这么简单的话,我会喜欢它的。刚刚与telerik支持人员进行了交谈(仍在分析他们发送给我的代码),但看起来wpf比winforms具有巨大的简单性优势。在winforms中,除了cellvalidation事件外,我还需要自定义5个事件。一旦我确信我完全理解他们在哪里做了什么以及为什么做了什么,我就会在那里发帖子。我删除了我以前的评论,因为有一点你是对的,使用“mouseover”(或更多的mousemove)事件是一个可能的解决方案的一部分,尽管目前为止不是唯一的部分。