473,395 Members | 1,466 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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
3 6109
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Michael | last post by:
Hi Everyone, I have a gridview control with the following markup: <asp:GridView ID="grdPOs" runat="server" AllowPaging="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#CC9966"...
0
by: Innova | last post by:
Hi, We are working on a gridview inside the gridview (parent-child) scenario. The data of child grid will depend on the data of parent. Objectives: 1.Add new row in parent grid after each row...
2
by: Chris Davoli | last post by:
How do you enable a check box in the GridView. I selected Checkbox Field in the Columns of the GridView, and the check box shows up in the Grid view, but it is disabled. How do I enable it so I can...
1
by: DC | last post by:
The problem I'm using the .NET GridView and FormView objects for the first time and im getting the error "An OleDbParameter with ParameterName '@ID' is not contained by this...
0
by: ThePurpleCat | last post by:
Hi, I'm a newbie to ASP.NET programming but not to Visual Studio. I'm having trouble getting my Master-Details page to work. I have a page enabled GridView which is linked to a FormView control...
0
by: landesjoe | last post by:
Hi, here's my problem in short: Text boxes in gridview don't seem to hold their value if the column's .Visible property is changed back and forth. I've got a form with a gridview populated from...
2
by: Blasting Cap | last post by:
I've got a gridview (that I converted over from a datagrid, which had been working properly), that is doubling up the number of rows returned. When it was running as a datagrid, the same code sent...
4
by: =?Utf-8?B?QmFyYmFyYSBBbGRlcnRvbg==?= | last post by:
I setup a simple gridview as a utility just to do some updates, nothing fancy just wanted easy UI to make updates. When I select ‘Edit’, I get the fields I want to edit. I edit them and click...
0
by: sharonrao123 | last post by:
hello all, I have a parent gridview company and in this one a nested gridview people, Is it possible to allow the user to select one row or multiple rows from the people gridview using a check box...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.