Removing empty fields from a bound datagrid

  • Thread starter Thread starter JackBusiness
  • Start date Start date
J

JackBusiness

Howdy folks:

Long time listener...first time poster.

My issue is that I am binding DB data to a datagrid using the
<%# Container.DataItem("FOO") %> methodology. So my question is
this...if FOO is empty, how can I substitute it out and replace it with
something like "Not Entered"?

Yes, I know I can choose to do this programatically, but before I go
down that road I wanted to know if it's possible with a hidden Eval
command or something of the like.

Danke,
Jacko
 
The formal way, I think, is to handle ItemDataBound envent, for example:

private void myGrid_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
ListItemType iType=e.Item.ItemType;

if (iType==ListItemType.Item || iType==ListItemType.AlternatingItem )
{
if (e.Item.Cells[0].Text.Length==0)
{
e.Item.Cells[0].Text="N/A";
}
}
}

Or you can simply:

<%# Container.DataItem("FOO").ToString().Length==0?"N/A":
Container.DataItem("FOO").ToString() %>
 
Excellent. Now I just have to dig out the VB equivalent of the inline
statement. Thanks Norman.

J
 
Back
Top