C# Windows 窗体 - ErrorProvider + DataGridView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/794459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 01:28:04  来源:igfitidea点击:

Windows Forms - ErrorProvider + DataGridView

c#.netwinformsdatagridviewerrorprovider

提问by Ronnie Overby

How can I hook in the ErrorProvider with individual cells on the DataGridView control?

如何将 ErrorProvider 与 DataGridView 控件上的单个单元格挂钩?

采纳答案by BFree

I'm not sure that you can use the ErrorProvider in this manner, however the DataGridView has functionality built into it that's basically the same idea.

我不确定您是否可以以这种方式使用 ErrorProvider,但是 DataGridView 内置了基本相同的功能。

The idea is simple. A DataGridViewCell has an ErrorText property. What you do is, you handle the OnCellValidating event and if fails validation, you set the error text property, and you get that red error icon to show up in the cell. Here's some pseudo code:

这个想法很简单。DataGridViewCell 有一个 ErrorText 属性。你要做的是,你处理 OnCellValidating 事件,如果验证失败,你设置错误文本属性,你会在单元格中显示红色错误图标。这是一些伪代码:

public Form1()
{
    this.dataGridView1.CellValidating += new DataGridViewCellValidatingEventHandler(dataGridView1_CellValidating);
}

private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (!this.Validates(e.FormattedValue)) //run some custom validation on the value in that cell
            {
                this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "Error";
                e.Cancel = true; //will prevent user from leaving cell, may not be the greatest idea, you can decide that yourself.
            }
        }

回答by Filip Skakun

You could add a column (like DataGridViewTextBoxColumn) to dataGridView.Columns that has the CellTemplate set to your own implementation (say, inherited from DataGridViewTextBoxCell). Then in your implementation - handle validation as you like - rendering and positioning of the editing panel to fit your needs.

您可以向 dataGridView.Columns 添加一列(如 DataGridViewTextBoxColumn),该列将 CellTemplate 设置为您自己的实现(例如,从 DataGridViewTextBoxCell 继承)。然后在您的实现中 - 根据需要处理验证 - 渲染和定位编辑面板以满足您的需求。

You can check a sample at http://msdn.microsoft.com/en-us/library/aa730881(VS.80).aspx.

您可以在http://msdn.microsoft.com/en-us/library/aa730881(VS.80).aspx查看示例。

But then again - there might be a simpler solution.

但话又说回来 - 可能有一个更简单的解决方案。

回答by Kluyg

private void myGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
    var dataGridView = (DataGridView)sender;
    var cell = dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex];
    if ( ... ) // Validation success
    {
        cell.ErrorText = string.Empty;
        return;
    }

    dataGridView.EndEdit();
    cell.ErrorText = error;
    e.Cancel = true;
}

回答by Oliver Friedrich

You can just implement IDataErrorInfointo your BusinessObjects, and set the BindingSource as DataSource for the ErrorProvider too. That way your BusinessObject intern validation shows up in the DataGrid and on all fields the objects are bound to automatically.

您可以在IDataErrorInfo您的 BusinessObjects 中实现,并将 BindingSource 设置为 ErrorProvider 的数据源。这样,您的 BusinessObject 实习生验证就会显示在 DataGrid 和对象自动绑定到的所有字段上。

回答by Dada Nada

The problem I have with BFree's solution is that nothing shows up while the cell is in edit mode, but if I end edit, I get a data format error (because my value is a double). I solved this by attaching the ErrorProvider directly to the cell edit control like this:

我使用 BFree 的解决方案遇到的问题是,当单元格处于编辑模式时没有任何显示,但是如果我结束编辑,我会收到数据格式错误(因为我的值是双精度值)。我通过将 ErrorProvider 直接附加到单元格编辑控件来解决这个问题,如下所示:

private ErrorProvider ep = new ErrorProvider();
private void DGV_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
    if (e.ColumnIndex < 0 || e.RowIndex < 0)
        return;
    double val;
    Control edit = DGV.EditingControl;
    if (edit != null && ! Double.TryParse(e.FormattedValue.ToString(), out val))
    {
        e.Cancel = true;
        ep.SetError(edit, "Numeric value required");
        ep.SetIconAlignment(edit, ErrorIconAlignment.MiddleLeft);
        ep.SetIconPadding(edit, -20); // icon displays on left side of cell
    }
}

private void DGV_CellEndEdt(object sender, DataGridViewCellEventArgs e)
{
    ep.Clear();
}