467,081 Members | 961 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,081 developers. It's quick & easy.

GridView /// Hiding a BoundField value

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

Jun 14 '06 #1
  • viewed: 5900
Share:
3 Replies
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.

Jun 14 '06 #2
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.


Jun 14 '06 #3

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.



Jun 14 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Michael | last post: by
reply views Thread by Innova | last post: by
2 posts views Thread by Chris Davoli | last post: by
reply views Thread by ThePurpleCat | last post: by
2 posts views Thread by Blasting Cap | last post: by
4 posts views Thread by =?Utf-8?B?QmFyYmFyYSBBbGRlcnRvbg==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.