469,898 Members | 1,755 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

DataGridView: Displaying empty cells for certain values

Hi,
I have a DataGridView which has as typed data-set associated with it.
For the table which I am displaying in it, I want to display certain
cells (corresponding to a column in the database) to be displayed as
blank instead of the value 0 (Zero - integer). Also, I want the value 0
(zero) to be saved to the database when the changes are applied/saved
and if the cells are blank.

I tried to change the text that is drawn/shown in the grid by handling
the CellPainting event, but the Value and FormattedValue properties are
read only.

How can I achieve this?

Thanks & Regards,
Ashutosh
Oct 30 '08 #1
5 13400
Ashutosh schrieb:
Hi,
I have a DataGridView which has as typed data-set associated with it.
For the table which I am displaying in it, I want to display certain
cells (corresponding to a column in the database) to be displayed as
blank instead of the value 0 (Zero - integer). Also, I want the value 0
(zero) to be saved to the database when the changes are applied/saved
and if the cells are blank.

I tried to change the text that is drawn/shown in the grid by handling
the CellPainting event, but the Value and FormattedValue properties are
read only.

How can I achieve this?
How about a quick and dirty one?
Use the CellPainting to make ForeColor=BackColor for those Cells with a
zero in them :)
Oct 30 '08 #2
Hi Ashutosh,

You can handle the CellFormatting event instead. For example:

void dataGridView1_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 0 && e.Value != null)
{
if ((int)e.Value == 0)
{
e.Value = "";
}
}
}
For more information about the CellFormatting event , you can refer to this
document:

CellFormatting event
http://msdn.microsoft.com/en-us/libr...atagridview.ce
llformatting.aspx

Please try my suggestion, and let me know the result.

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can

improve the support we provide to you. Please feel free to let my manager
know what you think of the level

of service provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to

http://msdn.microsoft.com/en-us/subs...#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the

community or a Microsoft Support Engineer within 2 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. 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/en-us/subs.../aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Oct 31 '08 #3
Hi Matthias,

Actually that was the first solution that came to my mind. But the
problem with that approach is that the value of zero will still be there
and user can select/copy it. Thanks anyways :)

Thanks & Regards,
Ashutosh

Matthias Krug wrote:
Ashutosh schrieb:
>Hi,
I have a DataGridView which has as typed data-set associated with it.
For the table which I am displaying in it, I want to display certain
cells (corresponding to a column in the database) to be displayed as
blank instead of the value 0 (Zero - integer). Also, I want the value
0 (zero) to be saved to the database when the changes are
applied/saved and if the cells are blank.

I tried to change the text that is drawn/shown in the grid by
handling the CellPainting event, but the Value and FormattedValue
properties are read only.

How can I achieve this?

How about a quick and dirty one?
Use the CellPainting to make ForeColor=BackColor for those Cells with
a zero in them :)
Oct 31 '08 #4
Hi Zhi,
Thanks for the information. I need little more help here.

For these columns, if the user doesn't enter any value (while editing or
adding a record), a value of NULL is added to the database. How can
override this so that I update the database with value of Zero and still
display an empty cell in the grid.

Thanks & Regards,
Ashutosh

Zhi-Xin Ye [MSFT] wrote:
Hi Ashutosh,

You can handle the CellFormatting event instead. For example:

void dataGridView1_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 0 && e.Value != null)
{
if ((int)e.Value == 0)
{
e.Value = "";
}
}
}
For more information about the CellFormatting event , you can refer to this
document:

CellFormatting event
http://msdn.microsoft.com/en-us/libr...atagridview.ce
llformatting.aspx

Please try my suggestion, and let me know the result.

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can

improve the support we provide to you. Please feel free to let my manager
know what you think of the level

of service provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to

http://msdn.microsoft.com/en-us/subs...#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the

community or a Microsoft Support Engineer within 2 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. 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/en-us/subs.../aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Oct 31 '08 #5
Hi Ashutosh,

You can handle the CellParsing event.

DataGridView.CellParsing event
http://msdn.microsoft.com/en-us/libr...atagridview.ce
llparsing.aspx

If you have any questions or concerns, please don't hesitate to let me know.

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can

improve the support we provide to you. Please feel free to let my manager
know what you think of the level

of service provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 31 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Scotty | last post: by
2 posts views Thread by Scotty | last post: by
6 posts views Thread by Miro | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.