GridView /// Hiding a BoundField value

S

sloan

<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.
 
B

Balasubramanian Ramanathan

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.
 
S

sloan

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>
 

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

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top