的WinForms:验证问题在一个DataGridView细胞 [英] Winforms: Problems validating a cell in a datagridview

查看:275
本文介绍了的WinForms:验证问题在一个DataGridView细胞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想验证一个WinForms datagridview的细胞是 CellValidating 。如果不是由用户设置正确的值我将 ERRORTEXT 并使用 e.Cancel ,使光标停留在细胞中。 现在的问题是,是,错误符号(和错误文本)不显示(在电池)。当我删除e.Cancel细胞失去焦点和错误符号显示。我怎样才能做到这一点的单元格保持在编辑模式和错误符号显示呢?

 如果(...)
{
   this.datagridviewX.Rows [e.RowIndex] .Cells [e.ColumnIndex] .ErrorText =ERRORTEXT;
   e.Cancel =真;
}
其他
{
   this.datagridviewX.Rows [e.RowIndex] .Cells [e.ColumnIndex] .ErrorText =;
}
 

解决方案

您所看到的行为实际上是由于画的问题,而不是由于不被显示的错误图标。正在发生的事情是,当你设置单元格的错误文本显示的图标,但在编辑模式下细胞的文本框涂黑的图标,从而向用户显示没有图标!

您有两个选择固定这一点 - 一个是简单地使用了该行的错误文本,而不是:

  this.datagridviewX.Rows [e.RowIndex] .Cells [e.ColumnIndex] .ErrorText =ERRORTEXT;
e.Cancel =真;
 

您有:

  this.datagridviewX.Rows [e.RowIndex] .ErrorText =ERRORTEXT;
e.Cancel =真;
 

另一种选择是改变细胞的细胞填充(移动编辑控制),并画在图标

其实,我发现这种方法解决问题<一href="http://social.msdn.microsoft.com/forums/en-US/vblanguage/thread/28d812bb-5f33-46d6-94dd-6a2d1f442a29"相对=nofollow>这里并再现低于code(C#中,而不是VB.Net)。

首先,你必须在其中添加了一些code要更改单元格填充你的手机验证活动:

 无效dataGridView1_CellValidating(对象发件人,DataGridViewCellValidatingEventArgs E)
{
    如果(string.IsNullOrEmpty(e.FormattedValue.ToString()))
    {
        DataGridViewCell的电池= dataGridView1.Rows [e.RowIndex] .Cells [e.ColumnIndex]

        cell.ErrorText =
            公司名称不能为空;

        如果(cell.Tag == NULL)
        {
            cell.Tag = cell.Style.Padding;
            cell.Style.Padding =新填充(0,0,18,0);
        }
        e.Cancel =真;

    }
    其他
    {
        dataGridView1.Rows [e.RowIndex] .ErrorText =的String.Empty;
    }
}
 

这使得可以看到图标不是编辑控制已经转移,除了图标也感动呢!因此,我们还需要画一个新的图标。

 无效dataGridView1_CellPainting(对象发件人,DataGridViewCellPaintingEventArgs E)
{
    如果(dataGridView1.IsCurrentCellDirty)
    {
        如果(!string.IsNullOrEmpty(e.ErrorText))
        {
            的GraphicsContainer容器= e.Graphics.BeginContainer();
            e.Graphics.TranslateTransform(18,0);
            e.Paint(this.ClientRectangle,DataGridViewPaintParts.ErrorIcon);
            e.Graphics.EndContainer(容器);
            e.Handled =真实;
        }
    }
}
 

然后当你结束编辑,你需要重新填充的单元格:

 无效dataGridView1_CellEndEdit(对象发件人,DataGridViewCellEventArgs E)
{
    如果(!string.IsNullOrEmpty(dataGridView1的[e.ColumnIndex,e.RowIndex] .ErrorText))
    {
        DataGridViewCell的电池= dataGridView1.Rows [e.RowIndex] .Cells [e.ColumnIndex]
        cell.ErrorText =的String.Empty;
        cell.Style.Padding =(填充)cell.Tag;
        cell.Tag = NULL;
    }
}
 

在这里,我发现这个忽略设置鼠标为新图标绘的岗位 - 这是一些粗糙的code,解决的是,我没有时间去真的工作,所以有一些轻微的呓语道这种想法会解决 - 我会整理的,如果我得到一分钟后

我设置DataGridView.ShowCellToolTips = true并且引进一个布尔inError跟踪,如果我们现在有一个编辑错误。然后我处理MouseHover事件:

 无效dataGridView1_MouseHover(对象发件人,EventArgs的)
{
    如果(inError)
    {
        点POS = this.PointToClient(Cursor.Position);

        如果(r.Contains(pos.X  -  20,pos.Y  -  5))
        {
            t.Show(有错误,dataGridView1.EditingControl,3000);
        }
    }
}
 

在code中的t是一个表单级工具提示控制,r是一个矩形。

我填充为r下面的单元格画处理程序:

 无效dataGridView1_CellPainting(对象发件人,DataGridViewCellPaintingEventArgs E)
{
    如果(dataGridView1.IsCurrentCellDirty)
    {
        如果(!string.IsNullOrEmpty(e.ErrorText))
        {
            的GraphicsContainer容器= e.Graphics.BeginContainer();

            R = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex,e.RowIndex,真正的);
            e.Graphics.TranslateTransform(18,0);
            e.Paint(this.ClientRectangle,DataGridViewPaintParts.ErrorIcon);
            e.Graphics.EndContainer(容器);

            e.Handled =真实;
        }
    }
}
 

我不开心的定位点零下20零下5 - 这就是我解决了,如果我有更多的时间

I want to validate a Winforms datagridview cell with CellValidating. If a value was not set correctly by the user I set ErrorText and use e.Cancel, so that the cursor remains in the cell. The problem is now, that the error-symbol (and the error text) is not displayed (in the cell). When I delete e.Cancel the cell looses the focus and error-symbol is displayed. How can I achieve that the cell remains in edit mode and the error-symbol is displayed too?

if (...)
{
   this.datagridviewX.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "Errortext";
   e.Cancel = true;
}
else
{
   this.datagridviewX.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "";
}

解决方案

The behaviour you are seeing is actually due to a painting issue and not due to the error icon not being shown. What is happening is that when you set the cell's error text the icon is displayed but the text box of the cell in edit mode is painted over the icon, hence no icon shown to the user!

You have two options for fixing this - one is to simply use the row's error text so instead of:

this.datagridviewX.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "Errortext";  
e.Cancel = true;  

You have:

this.datagridviewX.Rows[e.RowIndex].ErrorText = "Errortext";
e.Cancel = true;

The other option is to change the cell padding of the cell (moving the editing control) and painting the icon in.

I actually found this technique for solving the problem here and reproduced their code below (in C# and not VB.Net).

First you have your cell validating event where you add some code to change the cell padding:

void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
    if (string.IsNullOrEmpty(e.FormattedValue.ToString()))
    {
        DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];

        cell.ErrorText =
            "Company Name must not be empty";

        if (cell.Tag == null)
        {
            cell.Tag = cell.Style.Padding;
            cell.Style.Padding = new Padding(0, 0, 18, 0);
        }
        e.Cancel = true;

    }
    else
    {
        dataGridView1.Rows[e.RowIndex].ErrorText = string.Empty;
    }
}

That allows the icon to be seen not the editing control has moved, except the icon has moved too! So we also need to paint a new icon.

void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (dataGridView1.IsCurrentCellDirty)
    {
        if (!string.IsNullOrEmpty(e.ErrorText))
        {
            GraphicsContainer container = e.Graphics.BeginContainer();
            e.Graphics.TranslateTransform(18,0);
            e.Paint(this.ClientRectangle, DataGridViewPaintParts.ErrorIcon);
            e.Graphics.EndContainer(container);
            e.Handled = true;
        }
    }
}

Then when you end editing on the cell you need to reset the padding:

void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    if (!string.IsNullOrEmpty(dataGridView1[e.ColumnIndex, e.RowIndex].ErrorText))
    {
        DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
        cell.ErrorText = string.Empty;
        cell.Style.Padding = (Padding)cell.Tag;
        cell.Tag = null;
    }
}

The post where I found this neglects to set the mouse over for the new painted icon - Here is some rough code that addresses that, I don't have time to get it really working so there are some slight fudges that thought would fix - I'll tidy that up if I get a minute later.

I set DataGridView.ShowCellToolTips = true and introduce a boolean inError to track if we currently have an editing error. I then handle the MouseHover event:

void dataGridView1_MouseHover(object sender, EventArgs e)
{
    if (inError)
    {                
        Point pos = this.PointToClient(Cursor.Position);               

        if (r.Contains(pos.X - 20, pos.Y - 5))
        {                   
            t.Show("There was an error", dataGridView1.EditingControl, 3000); 
        }
    }
}

The t in that code is a form level ToolTip control, and r is a rectangle.

I populate r as below in the cell painting handler:

void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (dataGridView1.IsCurrentCellDirty)
    {
        if (!string.IsNullOrEmpty(e.ErrorText))
        {            
            GraphicsContainer container = e.Graphics.BeginContainer();

            r = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
            e.Graphics.TranslateTransform(18, 0);
            e.Paint(this.ClientRectangle, DataGridViewPaintParts.ErrorIcon);
            e.Graphics.EndContainer(container);            

            e.Handled = true;
        }
    }
}

I'm not happy about the minus 20 and minus 5 on the position point - that is what I'd fix up if I had a bit more time.

这篇关于的WinForms:验证问题在一个DataGridView细胞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆