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 FormatLabelWith DateString(Labe l lbl)
{
try
{
string lblText = lbl.Text;
DateTime currentItemDate = Convert.ToDateT ime(lblText);
if (currentItemDat e < new DateTime(1900, 1, 1))
{
lbl.Text = "--n/a--";
}
else
{
lbl.Text = String.Format(" {0:d}", currentItemDate );
}
}
catch
{ lbl.Text = "--ex--"; }
}
protected void GridView1_RowDa taBound(object sender, GridViewRowEven tArgs e)
{
if (e.Row.RowType == DataControlRowT ype.DataRow)
{
Label mylabel = (Label)e.Row.Fi ndControl("lblM yDate");
if (null != mylabel)
{
FormatLabelWith DateString(myla bel);
}
}
}
I think alot of people use the DateTime.MinVal ue 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:TemplateFi eld HeaderText="My Cool Date"
ItemStyle-HorizontalAlign ="Center"
SortExpression= "MyDate">
<ItemTemplate >
<asp:Label ID="lblMyDate" runat="server" Text='<%# Eval("MyDate")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateFie ld>
"Winista" <na*********@ho tmail.com> wrote in message
news:e5******** ********@TK2MSF TNGP04.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
"Balasubramania n Ramanathan" <rb***********@ hotmail.com> wrote in message
news:%2******** ********@TK2MSF TNGP04.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.ne t> wrote in message
news:OL******** ******@TK2MSFTN GP02.phx.gbl... <asp:BoundFie ld DataField="MyDa te" DataFormatStrin g="{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.