Windows Forms – ErrorProvider + DataGridView

Windows Forms – ErrorProvider + DataGridView

时光清浅 发布于 2021-11-29 字数 93 浏览 767 回复 5 原文

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

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。

扫码加入群聊

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

尸血腥色 2022-06-07 5 楼

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.

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

But then again - there might be a simpler solution.

只等公子 2022-06-07 4 楼
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;
}
一曲爱恨情仇 2022-06-07 3 楼

You can just implement IDataErrorInfo into 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.

淡水深流 2022-06-07 2 楼

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.

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:

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.
            }
        }
为你拒绝所有暧昧 2022-06-07 1 楼

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:

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