GridView /// Hiding a BoundField value

Discussion in 'ASP .Net' started by sloan, Jun 14, 2006.

  1. sloan

    sloan Guest

    <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.
     
    sloan, Jun 14, 2006
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > <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.
    >
    >
    >
     
    Balasubramanian Ramanathan, Jun 14, 2006
    #2
    1. Advertising

  3. sloan

    Winista Guest

    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" <> wrote in message
    news:%...
    > 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" <> wrote in message
    > news:...
    >> <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.
    >>
    >>
    >>

    >
    >
     
    Winista, Jun 14, 2006
    #3
  4. sloan

    sloan Guest

    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" <> wrote in message
    news:e5F57R%...
    > 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" <> wrote in message
    > news:%...
    > > 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" <> wrote in message
    > > news:...
    > >> <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.
    > >>
    > >>
    > >>

    > >
    > >

    >
    >
     
    sloan, Jun 14, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QmVuamFtaW4=?=

    ImageField and BoundField columns in a gridview control:

    =?Utf-8?B?QmVuamFtaW4=?=, Apr 20, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    2,712
    =?Utf-8?B?QmVuamFtaW4=?=
    Apr 20, 2004
  2. mz
    Replies:
    0
    Views:
    681
  3. =?Utf-8?B?RGFiYmxlcg==?=
    Replies:
    6
    Views:
    13,400
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    Apr 4, 2006
  4. hazz
    Replies:
    0
    Views:
    820
  5. David C
    Replies:
    2
    Views:
    14,593
    David C
    Jan 14, 2008
Loading...

Share This Page