While I agree the UI should not be driving the DataLayer... There wasn't
much to be done when the
DataSet.Select("mycriteria"); was blowing up the databind, because of a
NULL value.
I ended up changing the <asp:BoundField> to an <asp:Label>
and putting this code in:
private void FormatLabelWithDateString(Label lbl)
{
try
{
string lblText = lbl.Text;
DateTime currentItemDate = Convert.ToDateTime(lblText);
if (currentItemDate < new DateTime(1900, 1, 1))
{
lbl.Text = "--n/a--";
}
else
{
lbl.Text = String.Format("{0:d}", currentItemDate);
}
}
catch
{ lbl.Text = "--ex--"; }
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label mylabel = (Label)e.Row.FindControl("lblMyDate");
if (null != mylabel)
{
FormatLabelWithDateString(mylabel);
}
}
}
I think alot of people use the DateTime.MinValue since a datetime can't be
null.
I think the 1899 thing is kinda like that.
Not perfect, but gets the job done.
Here is the aspx code to be complete:
<asp:TemplateField HeaderText="My Cool Date"
ItemStyle-HorizontalAlign="Center"
SortExpression="MyDate">
<ItemTemplate>
<asp:Label ID="lblMyDate" runat="server" Text='<%# Eval("MyDate")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
"Winista" <na*********@hotmail.com> wrote in message
news:e5****************@TK2MSFTNGP04.phx.gbl...
UI should not be driving your DataAccess layer.
Use the ItemCreated, ItemDataBound etc. events to modify the text or
formatting. Here are some arcticles that may help you with it.
http://www.netomatix.com/DataGridSeries.aspx
"Balasubramanian Ramanathan" <rb***********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl... Easy way is to add a Calculated Column to the datatable using IIF
function and use this new calculated column in the datagrid
you can use onitemdatabound event of datagrid...but needs little work.
"sloan" <sl***@ipass.net> wrote in message
news:OL**************@TK2MSFTNGP02.phx.gbl... <asp:BoundField DataField="MyDate" DataFormatString="{0:d}"
HeaderText="My
Date" />
I have a BoundField being populuated with the "MyDate" column.
The MyDate is in a strongly typed DataSet. The values of course come
from
the DataBase.
I have a few values in the Database that have NULL as the value for
MyDate.
Its not ideal, but I don't have control of this one.
Instead of passing back a NULL, I'm passing back a 12/31/1899 date
value. ( This is because the Grid breaks, when I run a
DataSet.Select("somefield=123"); )
(The tsql looks like
Select
ISNULL ( MyDate , '12/31/1899') as MyDate
From
blah blah blah
I'd like to just show an emptystring or a nbsp; when I encounter dates
< 1/1/1900.
Yeah, its kind of a hack, but its sufficient for this project.
Which event should I be checking?
Or is there any way to hide a the display .. based on some rule in the
aspx
definition of the <asp:BoundField> ?
Thanks for any hints.