473,698 Members | 2,102 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14250
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=BackC olor 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_C ellFormatting(o bject sender,
DataGridViewCel lFormattingEven tArgs 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.as px

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****@microsof t.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=BackC olor 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_C ellFormatting(o bject sender,
DataGridViewCel lFormattingEven tArgs 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.as px

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****@microsof t.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.Ce llParsing 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****@microsof t.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
6411
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 gave it a try using the example given in
7
9691
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 below This is what I have been trying to achieve, but when I run it ALL the cell
2
7443
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 number you are scrolling past (Excel, Access). Sql server has the rownumbers on the Row Header column of its table view. Ideally, I would like to add row numbers to the Row Header column of the datagridview like sql server - Any suggestions...
7
16436
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 they can only input numerical values. Is there any way to do this? Currently my formatting works but if the user enters a value manually the formatting is not applied to the new value. Also I haven't found a way to allow only numerical input....
0
1851
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 intRowPos <= 10 Then e.HasMorePages = False
2
2653
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 '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! If intRowPos <= 10 Then e.HasMorePages = False
1
7428
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 the unbound column's cells in the OnLoad() handler the results are very strange. If I add a button to my view and update the unbound cells in the butt's handler it works fine. Here is an example of the code I'm trying to use (it's test code):...
6
3215
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 through the new row being added. It does not 'Add' that row, nor setup the 'next blank add row' so I can continue to tab
3
5629
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; gridDataSet.ReadXml("E:\\ImageControl.xml"); dataGridView1.DataMember = "Images"; dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; dataGridView1.ColumnHeadersVisible = true;...
0
8668
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9152
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8885
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8855
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7708
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4358
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.