Discussion:
Cell.ErrorText combined with e.cancel during validation of a cell in a Datagridview
(too old to reply)
Patrik
2007-07-24 14:26:48 UTC
Permalink
Hi,

I have a datagridview where I perform cell validation. I do not want the
user to be able to leave edit mode if the value is incorrect. The reason is
that i use CellEndEdit to write the value to my hardware. I validate the
value and set the cancel flag if the value is invalid. The problem is that I
also want the user to get an error message informing him that the value is
incorrect.

My problem is that it does not work to set cell.ErrorText because it will
not show until I leave edit mode. I use errorproviders through out my
program so I do not want to use a messagebox or something like that. Is
there a solution so that I can show the errortext and force the user to stay
in edit mode by setting the cancel flag at the same time?

My grid do not have the rowHeader visible so I can not display anything
there.

/Patrik
ClayB
2007-07-24 19:55:21 UTC
Permalink
You can ty painting an error icon yourself. Here is some simple code
that displays an error unless you type "abc" in a cell.

Rectangle errorRect = Rectangle.Empty;
void dataGridView1_CellValidating(object sender,
DataGridViewCellValidatingEventArgs e)
{
if (e.FormattedValue.Equals("abc"))
{
e.Cancel = true;
errorRect =
dataGridView1.GetCellDisplayRectangle(dataGridView1.CurrentCell.ColumnIndex,
dataGridView1.CurrentCell.RowIndex, true);
errorRect.X = errorRect.X - 20;
errorRect.Width = 20;
dataGridView1.Refresh();
}
else
{
errorRect = Rectangle.Empty;
dataGridView1.Refresh();
}
}

void dataGridView1_CellPainting(object sender,
DataGridViewCellPaintingEventArgs e)
{
if (!errorRect.IsEmpty)
{
e.Paint(e.ClipBounds, DataGridViewPaintParts.All);
e.Graphics.DrawIcon(SystemIcons.Error, errorRect);
e.Handled = true;
}
}

===============
Clay Burch
Syncfusion, Inc.
Linda Liu [MSFT]
2007-07-25 05:12:25 UTC
Permalink
This post might be inappropriate. Click to display it.
mohsen bazmi
2010-08-25 06:14:29 UTC
Permalink
thank u i was getting nerves u realy helped me
Post by Patrik
Hi,
I have a datagridview where I perform cell validation. I do not want the
user to be able to leave edit mode if the value is incorrect. The reason is
that i use CellEndEdit to write the value to my hardware. I validate the
value and set the cancel flag if the value is invalid. The problem is that I
also want the user to get an error message informing him that the value is
incorrect.
My problem is that it does not work to set cell.ErrorText because it will
not show until I leave edit mode. I use errorproviders through out my
program so I do not want to use a messagebox or something like that. Is
there a solution so that I can show the errortext and force the user to stay
in edit mode by setting the cancel flag at the same time?
My grid do not have the rowHeader visible so I can not display anything
there.
/Patrik
Post by ClayB
You can ty painting an error icon yourself. Here is some simple code
that displays an error unless you type "abc" in a cell.
Rectangle errorRect = Rectangle.Empty;
void dataGridView1_CellValidating(object sender,
DataGridViewCellValidatingEventArgs e)
{
if (e.FormattedValue.Equals("abc"))
{
e.Cancel = true;
errorRect =
dataGridView1.GetCellDisplayRectangle(dataGridView1.CurrentCell.ColumnIndex,
dataGridView1.CurrentCell.RowIndex, true);
errorRect.X = errorRect.X - 20;
errorRect.Width = 20;
dataGridView1.Refresh();
}
else
{
errorRect = Rectangle.Empty;
dataGridView1.Refresh();
}
}
void dataGridView1_CellPainting(object sender,
DataGridViewCellPaintingEventArgs e)
{
if (!errorRect.IsEmpty)
{
e.Paint(e.ClipBounds, DataGridViewPaintParts.All);
e.Graphics.DrawIcon(SystemIcons.Error, errorRect);
e.Handled = true;
}
}
===============
Clay Burch
Syncfusion, Inc.
Post by Linda Liu [MSFT]
Hi Patrik,
I performed a test on this issue and saw the same thing as you did. The
reason why the error icon of a DataGridViewCell is not visible when the
DataGridViewCell is in edit mode is that the editing control hosted in the
DataGridViewCell covers the error icon.
I have tried to diminish the width of the hosted editing control, but
without success. Then I tried to use a ErrorProvider and set error text for
the hosted editing control. However, it turned out that the error icon of
the ErrorProvider didn't appear, because the hosted editing control seemed
as if it is docked in the DataGridViewCell.
In fact what you want is a notification to the user if there's an error
during cell validation. I think a solution is to use balloon ToolTip to
show error message.
The following is a sample. It requires you to add a ToolTip component on
the form and set the IsBalloon property of the ToolTip to true.
void dataGridView1_CellValidating(object sender,
DataGridViewCellValidatingEventArgs e)
{
if (e.FormattedValue.ToString() != "123")
{
if (this.dataGridView1.IsCurrentCellInEditMode)
{
// To ensure the stem of the balloon ToolTip to point
to the target control for the first time, we need to call the ToolTip.Show
method twice. This is a known bug of ToolTip.
this.toolTip1.Show("",
this.dataGridView1.EditingControl);
this.toolTip1.Show("must be 123",
this.dataGridView1.EditingControl);
e.Cancel = true;
}
}
else
{
if (this.dataGridView1.IsCurrentCellInEditMode)
{
this.toolTip1.Show("",
this.dataGridView1.EditingControl);
}
}
}
Hope this helps.
If you have any question, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Post by Patrik
Thank you Linda and ClayB for your answers!
/Patrik
Submitted via EggHeadCafe - Software Developer Portal of Choice
Simple .NET HEX PixelColor Utility
http://www.eggheadcafe.com/tutorials/aspnet/5617a491-963d-4510-b8f1-1863ddf52bc1/simple-net-hex-pixelcolor-utility.aspx
Patrik
2007-07-25 14:56:26 UTC
Permalink
Thank you Linda and ClayB for your answers!

/Patrik
Post by Patrik
Hi,
I have a datagridview where I perform cell validation. I do not want the
user to be able to leave edit mode if the value is incorrect. The reason
is that i use CellEndEdit to write the value to my hardware. I validate
the value and set the cancel flag if the value is invalid. The problem is
that I also want the user to get an error message informing him that the
value is incorrect.
My problem is that it does not work to set cell.ErrorText because it will
not show until I leave edit mode. I use errorproviders through out my
program so I do not want to use a messagebox or something like that. Is
there a solution so that I can show the errortext and force the user to
stay in edit mode by setting the cancel flag at the same time?
My grid do not have the rowHeader visible so I can not display anything
there.
/Patrik
Loading...