C# 如何在单元格TextChange上的DataGridViewCll中显示ErrorIcon?

C# 如何在单元格TextChange上的DataGridViewCll中显示ErrorIcon?,c#,winforms,datagridview,C#,Winforms,Datagridview,我在DataGridView中有两列: 订购数量 收到数量 如果输入的receivedQuantity大于orderdQuantity,则在该单元格上,我希望显示ErrorIcon。 目前,它的功能与我之前描述的不一样。当前的工作方式是,如果单击另一个单元格的DataGridview,则会显示ErrorIcon 我的代码如下: //txtBox_TextChanged is called in datagridview1_EditingControlShowing() event priv

我在DataGridView中有两列:

  • 订购数量
  • 收到数量
如果输入的receivedQuantity大于orderdQuantity,则在该单元格上,我希望显示ErrorIcon。
目前,它的功能与我之前描述的不一样。当前的工作方式是,如果单击另一个单元格的
DataGridview
,则会显示
ErrorIcon

我的代码如下:

//txtBox_TextChanged is called in datagridview1_EditingControlShowing() event

private void txtBox_TextChanged(object sender, EventArgs e)
{
    try
    {
        if (datagridview1.Rows.Count > 0)
        {
            if (txtBox.Text != "")
            {
                int rowIndex = datagridview1.CurrentRow.Index;
                int recQty = Convert.ToInt32(txtBox.Text);
                int orderedQty = Convert.ToInt32(datagridview1.Rows[rowIndex].Cells["Ordered Qty"].Value);
                if (recQty > orderedQty)
                {
                    this.datagridview1.CurrentCell.ErrorText = "Invalid Quantity";
                }
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Oops something went wrong.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

当用户在单元格中键入内容时,如何显示错误图标?

默认情况下,为单元格设置
ErrorText
时,当单元格处于编辑模式时,错误图标将不显示。此外,
DataGridView
的任何事件都不会在用户键入单元格时处理文本的更改。 要在用户键入单元格时设置错误文本并显示错误图标,应遵循以下步骤:

  • EditingControlShowing
    中获取编辑控件,并处理其
    TextChanged
    事件
  • 在文本框的
    TextChanged
    事件中,设置或删除单元格的
    ErrorText
  • 开始编辑时,在
    CellBeginEdit
    事件中对单元格应用右填充,并在
    CellEndEdit
    中删除它
  • CellPainting
    事件中绘制错误图标

示例

此示例显示当用户在第一个单元格中键入文本时,如果该单元格的文本为空,则该单元格中会出现错误

在显示的
编辑控件中,将
文本更改
事件附加到
文本框中

void grid_EditingControlShowing(object sender,DataGridViewEditingControlShowingEventArgs e)
{
    if (this.grid.CurrentCell.ColumnIndex == 0)
    {
        var textbox = e.Control as DataGridViewTextBoxEditingControl;
        if (textbox != null)
        {
            textbox.TextChanged -= textBox_TextChanged;
            textbox.TextChanged += textBox_TextChanged;
        }
    }
}
TextChanged
事件中的
TextBox
执行验证并删除或设置单元格的
ErrorText

void textBox_TextChanged(object sender, EventArgs e)
{
    var textbox = (TextBox)sender;
    if (string.IsNullOrEmpty(textbox.Text))
        this.grid.CurrentCell.ErrorText = "Invalid Value";
    else
        this.grid.CurrentCell.ErrorText = null;
}
CellBeginEdit
中添加右侧填充以显示错误图标,在
CellEndEdit
中删除填充:

void grid_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
    var cell = grid.Rows[e.RowIndex].Cells[e.ColumnIndex];
    cell.Style.Padding = new Padding(0, 0, 18, 0);
}
void grid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    var cell = grid.Rows[e.RowIndex].Cells[e.RowIndex];
    cell.Style.Padding = new Padding(0, 0, 0, 0);
}
CellPainting
中,自己绘制错误图标:

private void grid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    e.Paint(e.ClipBounds, DataGridViewPaintParts.All &
                        ~(DataGridViewPaintParts.ErrorIcon));
    if (!String.IsNullOrEmpty(e.ErrorText))
    {
        GraphicsContainer container = e.Graphics.BeginContainer();
        e.Graphics.TranslateTransform(e.CellStyle.Padding.Right, 0);
        e.Paint(e.CellBounds, DataGridViewPaintParts.ErrorIcon);
        e.Graphics.EndContainer(container);
    }
    e.Handled = true;
}

当用户在单元格中键入内容时,如何显示错误图标?

默认情况下,为单元格设置
ErrorText
时,当单元格处于编辑模式时,错误图标将不显示。此外,
DataGridView
的任何事件都不会在用户键入单元格时处理文本的更改。 要在用户键入单元格时设置错误文本并显示错误图标,应遵循以下步骤:

  • EditingControlShowing
    中获取编辑控件,并处理其
    TextChanged
    事件
  • 在文本框的
    TextChanged
    事件中,设置或删除单元格的
    ErrorText
  • 开始编辑时,在
    CellBeginEdit
    事件中对单元格应用右填充,并在
    CellEndEdit
    中删除它
  • CellPainting
    事件中绘制错误图标

示例

此示例显示当用户在第一个单元格中键入文本时,如果该单元格的文本为空,则该单元格中会出现错误

在显示的
编辑控件中,将
文本更改
事件附加到
文本框中

void grid_EditingControlShowing(object sender,DataGridViewEditingControlShowingEventArgs e)
{
    if (this.grid.CurrentCell.ColumnIndex == 0)
    {
        var textbox = e.Control as DataGridViewTextBoxEditingControl;
        if (textbox != null)
        {
            textbox.TextChanged -= textBox_TextChanged;
            textbox.TextChanged += textBox_TextChanged;
        }
    }
}
TextChanged
事件中的
TextBox
执行验证并删除或设置单元格的
ErrorText

void textBox_TextChanged(object sender, EventArgs e)
{
    var textbox = (TextBox)sender;
    if (string.IsNullOrEmpty(textbox.Text))
        this.grid.CurrentCell.ErrorText = "Invalid Value";
    else
        this.grid.CurrentCell.ErrorText = null;
}
CellBeginEdit
中添加右侧填充以显示错误图标,在
CellEndEdit
中删除填充:

void grid_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
    var cell = grid.Rows[e.RowIndex].Cells[e.ColumnIndex];
    cell.Style.Padding = new Padding(0, 0, 18, 0);
}
void grid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    var cell = grid.Rows[e.RowIndex].Cells[e.RowIndex];
    cell.Style.Padding = new Padding(0, 0, 0, 0);
}
CellPainting
中,自己绘制错误图标:

private void grid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    e.Paint(e.ClipBounds, DataGridViewPaintParts.All &
                        ~(DataGridViewPaintParts.ErrorIcon));
    if (!String.IsNullOrEmpty(e.ErrorText))
    {
        GraphicsContainer container = e.Graphics.BeginContainer();
        e.Graphics.TranslateTransform(e.CellStyle.Padding.Right, 0);
        e.Paint(e.CellBounds, DataGridViewPaintParts.ErrorIcon);
        e.Graphics.EndContainer(container);
    }
    e.Handled = true;
}