Urgent: How to NOT display " " in datagrid when NULL

G

Guest

Hi everyone,
Can someone show me how to display an empty string in a boundcolumn of a
datagrid when the data source item is NULL. Datagrid by default displays the
and that's BAD!!!!!
I've tried the following in the ItemDataBound event but it gave me an
"invalid cast" error:

DataRowView dataRowView = (DataRowView)e.Item.DataItem;
for (int i=0; i<e.Item.Cells.Count - 1; i++)
{
if (dataRowView.ToString().Trim() == System.DBNull.Value.ToString())
{
e.Item.Cells.Text = "";
}
}

PS. It's a web form, not a windows form, if it makes any difference. Also,
the datagrid rows (items) are NOT EditItem type rows.

Any suggestion is greatly appreciated. I'm very desperate for an answer.

KD
 
G

Guest

Can you try using ISNULL function in the Database. Like if you are using the
Stored Procedure, use ISNULL for that particular column.
 
G

Guest

Hi Abhi,
Thanks for your post. Yes, I've tried that too and did not help. Datagrid
seems to interpret null or empty string "" the same and " " is displayed
unless I return the null value as an empty string with a space, " " rather
than "". But that's not good.
However, the following works fine but I'm just wondering if there's a better
way of doing it.
Datagrid_ItemDataBound ()
{
...
for (int i=0; i<e.Item.Cells.Count; i++)
{
if (e.Item.Cells == null || e.Item.Cells.Text == " ")
e.Item.Cells.Text = "";
}
}


Abhi said:
Can you try using ISNULL function in the Database. Like if you are using the
Stored Procedure, use ISNULL for that particular column.

Calvin KD said:
Hi everyone,
Can someone show me how to display an empty string in a boundcolumn of a
datagrid when the data source item is NULL. Datagrid by default displays the
and that's BAD!!!!!
I've tried the following in the ItemDataBound event but it gave me an
"invalid cast" error:

DataRowView dataRowView = (DataRowView)e.Item.DataItem;
for (int i=0; i<e.Item.Cells.Count - 1; i++)
{
if (dataRowView.ToString().Trim() == System.DBNull.Value.ToString())
{
e.Item.Cells.Text = "";
}
}

PS. It's a web form, not a windows form, if it makes any difference. Also,
the datagrid rows (items) are NOT EditItem type rows.

Any suggestion is greatly appreciated. I'm very desperate for an answer.

KD
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top