Hi All,

I know c# has limitations on assigning null values to datetime and integer value types. But is there any trick I can utilize. I'm converting my appl. from another dec. tool wher I have no null issue, to c#. What I want to do is ; I populate my datagrdiview,
I do not want the same info to repeat itself on the second and following rows just for appearance purposes (like same nama/lastname not to appear after the first row, no problem with strings though). So I try to assign null values to the repeating info cells (string,int,date). How do I overcome int and datetime cells nulling limitations.

Thanks in advance

7 Years
Discussion Span
Last Post by snakay

Value types can not become null.
But C# has what is called nullable types.
You define and use them like this :

bool? MyNullableBool;
            double? MyNullableDouble = 0.0;
            MyNullableBool = null;
            MyNullableDouble = null;

I see what you do but my problem is with the datagridview cell which is already populated with int or datetime data, and now I want to nullify them as follows;

// below is a string--no problem
r.Cells["colName"].Value = null;

// below is a datetime.I don't want to do this
r.Cells["colMyDate"].Value = DateTime.MinValue;

// below is an int.
r.Cells["colMyint"].Value =null;

I know I can't do what I did for int and datetime. But what can I do to clear those cells of grid so that you don't see any data there

Edited by Dani: Formatting fixed


You cannot assign null values like that to the grid but what you can do is use DBNull.Value . Here is a snippet where I am populating a grid and I check for nulls. If it is null then I use a DBNull in the grid:

dtRow[i1] = (gridRow.Cells[i1].Value == null ? DBNull.Value : gridRow.Cells[i1].Value);
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.