hide these null values in datagrid

D

desmoduck

Hey Group,

I may just be suffering brain fade but I have searched around and not
been able to find a solution. My difficulty: I have a datagrid that
shows name, address, tel, fax and toll free number. I would like to
hide the empty values. For instance; no fax = no fax number row, no
toll free = no TF row. I'm sure it must be something I am overlooking
or not thinking through.

All I can give back is my gratitude.....

<asp:datagrid AllowCustomPaging="true" AllowPaging="true"
AutoGenerateColumns="false" DataSource="<%# listings.DefaultView %>"
id="dgListings" PagerStyle-Mode="NextPrev" PageSize="<%#
listings.PageSize %>" runat="server"
OnPageIndexChanged="listings.OnDataGridPageIndexChanged"
virtualitemcount="<%# listings.RecordCount %>">
<Columns>
<asp:BoundColumn DataField="Business Category"
HeaderText="Business Category"
ReadOnly="true"
Visible="False"/>
<asp:TemplateColumn HeaderText="Business Information">
<ItemTemplate>
<table border="0">
<tr>
<td align="right"><b>Name:</b></td>
<td> <%# DataBinder.Eval(Container.DataItem, "Business
Name") %> </td>
</tr>
<tr>
<td align="right"><b>Address:</b></td>
<td> <%# DataBinder.Eval(Container.DataItem, "Address")
%> </td>
</tr>
<tr>
<td align="right"></td>
<td> <%# DataBinder.Eval(Container.DataItem, "Town") %>,
<%# DataBinder.Eval(Container.DataItem, "State") %> <%#
DataBinder.Eval(Container.DataItem, "ZipCode") %> </td>
</tr>
<tr>
<td align="right"><b>Telephone:</b></td>
<td> <%# DataBinder.Eval(Container.DataItem, "WorkPhone")
%> </td>
</tr>
<tr>
<td align="right"><b>Fax:</b> </td>
<td> <%# DataBinder.Eval(Container.DataItem, "FaxNumber")
%> </td>
</tr>
<tr>
<td aligh="right"><b>Toll Free:</b></td>
<td> <%# DataBinder.Eval(Container.DataItem, "TollFree")
%> </td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateColumn> </Columns>
</asp:datagrid>

*******************************************************
they have the internet on computers now?!?
*******************************************************
 
K

Kevin Jones

You can wrap the entire production up in a method, so something like

<asp:TemplateColumn HeaderText="Business Information">
<ItemTemplate>
<table border="0">
<%# GetTableRow(Container.DataItem) %>
<tr>
<td align="right"><b>Address:</b></td>
<td> <%# DataBinder.Eval(Container.DataItem, "Address")> %> </td>
</tr>
</table>
...

protected string GetTableRow(object o)
{
string s = Eval(Container.DataItem, "Business Name");
if(s.IsNullOrEmpty()
return "";
else
return string.Format("<tr><td a lign='right'>
<b>Name:</b></td><td>{0}</td></tr>", s);
}

This is typed into my news reader so there are probably compiler errors,
but the idea is that you can provide a method, have the template call
the method passing the current data item. The method returns a string
from that containing the data you need to display. The method can then
call Eval on the dataitem passed in,
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top