473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 14167
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: dbuchanan | last post by:
Hello, Is this a bug? Is there some kind of work around? I want to add default values for a few columns in my datagridview I found the "DefaultValuesNeeded" event for the datagridview I...
7
by: steve | last post by:
Hi All I urgently need help on setting datagridview cell borders at runtime I found some code on the web from Programming Smart Client Data Applications with .NET 2.0 by Brian Noyes See...
2
by: Rich | last post by:
Hello, Some database applicatins have a tooltip feature where when you are dragging the scrollbar of the table view a tooltip appears next to the mouse cursor displaying the approximate record...
7
by: Ryan | last post by:
I have a DataGridView which displays numeric (Int32) data from an underlying database. I want the numbers to be displayed in numeric format "#,###" (with commas). I want to also limit the user so...
0
by: Scotty | last post by:
Hi, Hope someone can help me I have a datagridview sith data Code below works fine if I print the data if there is only 1 page (without using '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! If...
2
by: Scotty | last post by:
Hi, Hope someone can help me I have a datagridview I want to print Code below works fine if I print the data if there is only 1 page (without using hasmore pages...
1
by: sklett | last post by:
I've got a strange situation here. I have a databound DataGridView that also has un-bound columns. When the view loads, I want to update the values of the unbound columns. If I attempt to modify...
6
by: Miro | last post by:
Sorry for the cross post. I am stuck. I have a datagridview for poker rounds. Basically there are 3 columns in this datagridview. "Round" "SmallBlind" "BigBlind" I have an issue when I tab...
3
by: Stewart Berman | last post by:
I have an application that populates a DataGridView control with an XML file: private void Form1_Load(object sender, EventArgs e) { dataGridView1.DataSource = gridDataSet;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.