日期:2014-05-17  浏览次数:20732 次

DataGridView如何在用户编辑控件的时候(而不是在验证时)就显示错误图标?
前几天在网上下载了datagridview  faq看看
里面有一个列子(如何在用户编辑控件的时候(而不是在验证时)就显示错误图标?)
我把代码复制运行。在同一个单元格中只有第一次输入BAD出现错误图标,改过提交以后,下次再BAD没有图标?看了半天感觉代码没错啊。高手帮忙看看

private ToolTip errorTooltip;
private Point cellInError = new Point(-2, -2);
public Form1()
{
    InitializeComponent();
    dataGridView1.ColumnCount = 3;
    dataGridView1.RowCount = 10;
}

private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
    if (dataGridView1.IsCurrentCellDirty)
    {
        if (e.FormattedValue.ToString() == "BAD")
        {
            DataGridViewCell cell = dataGridView1[e.ColumnIndex, e.RowIndex];
            cell.ErrorText = "Invalid data entered in cell";

            // increase padding for icon. This moves the editing control
            if (cell.Tag == null)
            {
                cell.Tag = cell.Style.Padding;
                cell.Style.Padding = new Padding(0, 0, 18, 0);
                cellInError = new Point(e.ColumnIndex, e.RowIndex);
            }
            if (errorTooltip == null)
            {
                errorTooltip = new ToolTip();
                errorTooltip.InitialDelay = 0;
                errorTooltip.ReshowDelay = 0;
                errorTooltip.Active = false;
            }

            e.Cancel = true;
        }
    }
}

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (dataGridView1.IsCurrentCellDirty && !String.IsNullOrEmpty(e.ErrorText))
    {
        // paint everything except error icon
        e.Paint(e.ClipBounds, DataGridViewPaintParts.All &  ~(DataGridViewPaintParts.ErrorIcon));

       &n