首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

DataGridView怎么在用户编辑控件的时候(而不是在验证时)就显示异常图标

2013-01-28 
DataGridView如何在用户编辑控件的时候(而不是在验证时)就显示错误图标?前几天在网上下载了datagridviewfa

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

        // now move error icon over to fill in the padding space


        GraphicsContainer container = e.Graphics.BeginContainer();
        e.Graphics.TranslateTransform(18, 0);
        e.Paint(this.ClientRectangle, DataGridViewPaintParts.ErrorIcon);
        e.Graphics.EndContainer(container);

        e.Handled = true;
    }
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    if (dataGridView1[e.ColumnIndex, e.RowIndex].ErrorText != String.Empty)
    {
        DataGridViewCell cell = dataGridView1[e.ColumnIndex, e.RowIndex];
        cell.ErrorText = String.Empty;
        cellInError = new Point(-2,-2);

        // restore padding for cell. This moves the editing control
        cell.Style.Padding = (Padding)cell.Tag;

        // hide and dispose tooltip 
        if (errorTooltip != null)
        {
            errorTooltip.Hide(dataGridView1);
            errorTooltip.Dispose();
            errorTooltip = null;
        }
    }
}

// show and hide the tooltip for error
private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
{
    if (cellInError.X == e.ColumnIndex &&
        cellInError.Y == e.RowIndex)
  {
     DataGridViewCell cell = dataGridView1[e.ColumnIndex, e.RowIndex];

        if (cell.ErrorText != String.Empty)
        {
            if (!errorTooltip.Active)
            {
                errorTooltip.Show(cell.ErrorText, dataGridView1, 1000);
            }
            errorTooltip.Active = true;
        }
    }
}

private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
{
    if (cellInError.X == e.ColumnIndex &&
        cellInError.Y == e.RowIndex)
    {
        if (errorTooltip.Active)
        {
            errorTooltip.Hide(dataGridView1);
            errorTooltip.Active = false;


        }
    }
}

datagridview,winform
[解决办法]
你在textChanged里触发验证,错误了就会出错误的图标啊
[解决办法]
引用:
引用:你在textChanged里触发验证,错误了就会出错误的图标啊
怎么处罚验证并且出现错误图标

不在文本框里面输入东西。然后光标离开。看看报不报错。
[解决办法]
就grid而言,验证事件应该是最合适的地方
[解决办法]
一般都在验证事件或失去焦点的时候写代码验证下
[解决办法]
引用:
引用:你在textChanged里触发验证,错误了就会出错误的图标啊
怎么处罚验证并且出现错误图标


在textChanged事件里
 void txtBox_TextChanged(object sender, EventArgs e)
        {
            TextBox txtBox = sender as TextBox;
            if (txtBox != null)
            {
                //在这里对txtBox验证
                    System.Windows.Forms.ErrorProvider errorProv=new System.Windows.Forms.ErrorProvider();
                errorProv(txtBox,"出错了");
              }
        }

热点排行