Question

I created a datatable of X rows and Y columns. Then I populated certain cells with integers ranging from 1 to 10. When I set my datatable to a datagrid I noticed that by default, the cells that were not populated by me have values of "0" (zero) in the datagrid. I would prefer they be blank instead of "0".

Does anyone know if there is a default option that I have to change to make them all blank instead of zeros? Or, must I manually replace all zeroes with blanks? If so, how do I do this?

if (datatable.Rows[x][y] == "0")
{
    datadable.Rows[x][y] = null;
}

For this code I get the warning: "Warning Possible unintended reference comparison; to get a value comparison, cast the left hand side to type 'string'" and I'm not sure why. I tried adding .ToString but that didn't seem to work. I am using C#, Visual Studio Express 2010.

Was it helpful?

Solution

Use DBNull.Value

datadable.Rows[x][y] = DBNull.Value;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top