DataGridView: Displaying empty cells for certain values

A

Ashutosh

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
 
M

Matthias Krug

Ashutosh said:
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 :)
 
Z

Zhi-Xin Ye [MSFT]

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/library/system.windows.forms.datagridview.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:
(e-mail address removed).

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

http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#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/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

Ashutosh

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
 
A

Ashutosh

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
 
Z

Zhi-Xin Ye [MSFT]

Hi Ashutosh,

You can handle the CellParsing event.

DataGridView.CellParsing event
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.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:
(e-mail address removed).

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top