Click here to Skip to main content
15,891,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This statement will show a message box with empty message.
C#
string i="";
messagebox.show(i.tostring());

How I can get the same behavior if i is int?
I expect something like this
C#
int I="";
messagebox.show(I.tostring());

I tried many ways, but nothing worked. There is some simple solution I hope.
Posted
Updated 12-Oct-11 5:53am
v2

Sorry I thought that you had an int that you want to also be able to store whether it was set or not, i.e. nullable or not.

If you have a DataGridViewCell and you want to make it blank, but the cell type is an integer, you can't do this - there is no valid 'empty' integer. You'll need to change the cell type as well:

C#
DataGridViewCell someCell; // = your code here.

// Set the cell value type to a string so that we can clear it.
someCell.ValueType = typeof(string);

// Set the cell data to an empty string.
someCell.Value = string.Empty;



Just be really careful - if you're databound to an int type then you should never get to the state where the int has just disappeared. If, for example, you want to make the value 0 look empty, then you can do this with custom formatting:

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellformatting.aspx[^]

For example you can say that when the value is zero, it formats it as an empty string - this way the underlying data is still consistent.
 
Share this answer
 
Comments
RaviRanjanKr 12-Oct-11 17:00pm    
My 5+
BillWoodruff 12-Oct-11 21:14pm    
+5 Nailed that one.
[no name] 13-Oct-11 10:04am    
it gives commit error
[no name] 13-Oct-11 10:24am    
can you just describe witha small example for custom formatting
Dave Kerr 13-Oct-11 11:14am    
IT will indeed give a commit error - null is not a valid value for an integer.
Do you want it to be hidden if it is zero? Because this is entirely different!
If you need null as a value, you'll need to change your database to allow the column to be nullable.
In addition to other answers, I should add: string value = "" does not equal to null, you can easily proof it. You could have written string value = null.

By the way, avoid using any immediate constants, especially of string type. I think the literal null is perfectly fine, but "" is certainly not, for maintainability reasons. If you really want empty string and not null, always use string.Empty.

—SA
 
Share this answer
 
v2
Comments
Espen Harlinn 13-Oct-11 17:53pm    
Good point :)
Sergey Alexandrovich Kryukov 13-Oct-11 19:39pm    
Thank you, Espen.
--SA
you must make the int nullable, using
C#
Nullable<int></int>
or
C#
int?
- this makes it an int that can also have a null value!

C#
int? i = null;

String s = !(i.HasValue) ? "" : i.ToString(); // MRB: used ! operator (logical not) instead of '== false'


Essential reading: http://msdn.microsoft.com/en-us/library/1t3y8s4s(v=VS.100).aspx[^]
 
Share this answer
 
v4
Comments
[no name] 12-Oct-11 12:17pm    
here I have a datagridview which is linked to MS Access database,and its columns are integer type. what I want to do is while deleting selected cells from datagridview the cells should be blank .as the original database is number type i am unable to do this. I used the above code

int? i = null; cell.Value=object(i)it creates an error please help me
[no name] 12-Oct-11 12:19pm    
sorry int? i = null; cell.Value=(object)i it creates an error please help me
[no name] 12-Oct-11 12:23pm    
tried like this no change happens.
int? i = null; cell.Value = i.Value;

but when i try like this

int? i = 12; cell.Value = i.Value; all deleted cells are replaced with 12 . so what should I do to get a blank instead of a number please somebody help
Dave Kerr 12-Oct-11 12:31pm    
I'll help just on my way home from work at the moment tho!
[no name] 12-Oct-11 13:41pm    
thanks for the positive response
C#
string i = "";
MessageBox.Show(i.ToString());


C#
int? a = null;
MessageBox.Show(a.ToString());
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900