After DataBind in DataGrid set CssClass per row?

Discussion in 'ASP .Net' started by Randall Parker, Jan 18, 2006.

  1. Using ASP.Net v1.1.

    In a System.Web.UI.WebControls.DataGrid I can't find any method or property that
    looks like it would give one access to individual rows after a DataBind.

    What I want to do: Set either CssClass or other properties for a table tr tag for
    each row and make them different depending on tests of values in each row.

    I figure I can use the corresponding table that had a database bound to the grid to
    get at values to test. But I do not see how to set properties at a per row level in
    the DataGrid. Is this possible?

    Is it any more possible in ASP.Net v2.0?

    I want to style each individual row in ways dependent on that row's data values. I do
    not see how to do that either before or after the DataBind. Is it possible to do at all?
    Randall Parker, Jan 18, 2006
    #1
    1. Advertising

  2. Sure it is possible.

    The DataGrid control has Items property. Items represent the rows.

    The are 2 standard ways of setting row properties:

    1. In PreRender event loop through the myGrid.Items collection.

    2. In ItemDataBound event you get access to each individual item, including
    the header and footer if any. You get a reference to the item as a parameter
    to the event handler.

    Eliyahu

    "Randall Parker" <NOtechieSPAMpundit_please@future_avoidjunk_pundit.com>
    wrote in message news:Oz5SrH$...
    > Using ASP.Net v1.1.
    >
    > In a System.Web.UI.WebControls.DataGrid I can't find any method or
    > property that looks like it would give one access to individual rows after
    > a DataBind.
    >
    > What I want to do: Set either CssClass or other properties for a table tr
    > tag for each row and make them different depending on tests of values in
    > each row.
    >
    > I figure I can use the corresponding table that had a database bound to
    > the grid to get at values to test. But I do not see how to set properties
    > at a per row level in the DataGrid. Is this possible?
    >
    > Is it any more possible in ASP.Net v2.0?
    >
    > I want to style each individual row in ways dependent on that row's data
    > values. I do not see how to do that either before or after the DataBind.
    > Is it possible to do at all?
    Eliyahu Goldin, Jan 18, 2006
    #2
    1. Advertising

  3. In the ItemDataBound event I'm trying to get access to the dataset row that was used
    to populate that table row.

    Is e.Item.DataItem the data row? Or is something else?

    I want to look at values in the data row and test them and then decide which css to
    assign to that row.

    Here's an Item_Bound event routine:

    void Item_Bound(Object sender, DataGridItemEventArgs e)
    {
    //e.Item.Cells[2].Text
    //e.Item.Cells[2].CssClass
    //e.Item.CssClass
    // e.Item.DataItem


    }

    Also, what are the arguments for the PreRender event? Is there some system for
    knowing their args or is there a way to get VS 2003 to generate a method that has the
    right args?

    I'm guessing it is this:
    void PreRender(Object sender,System.EventArgs ee)

    But I'm not sure.


    Eliyahu Goldin wrote:
    > Sure it is possible.
    >
    > The DataGrid control has Items property. Items represent the rows.
    >
    > The are 2 standard ways of setting row properties:
    >
    > 1. In PreRender event loop through the myGrid.Items collection.
    >
    > 2. In ItemDataBound event you get access to each individual item, including
    > the header and footer if any. You get a reference to the item as a parameter
    > to the event handler.
    >
    > Eliyahu
    >
    Randall Parker, Jan 19, 2006
    #3
  4. Yes, e.Item.DataItem is the data row.You need to typecast it to the
    appropriate type.

    For the PreRender event info, why don't you look in the MSDN Library? It is
    available online if you don't have it locally. You can setup events in the
    aspx page Design view. And you guess is almost right. If you setup the event
    in the HTML view, the handler should be declared protected.

    Eliyahu

    "Randall Parker" <NOtechieSPAMpundit_please@future_avoidjunk_pundit.com>
    wrote in message news:...
    > In the ItemDataBound event I'm trying to get access to the dataset row
    > that was used to populate that table row.
    >
    > Is e.Item.DataItem the data row? Or is something else?
    >
    > I want to look at values in the data row and test them and then decide
    > which css to assign to that row.
    >
    > Here's an Item_Bound event routine:
    >
    > void Item_Bound(Object sender, DataGridItemEventArgs e)
    > {
    > //e.Item.Cells[2].Text
    > //e.Item.Cells[2].CssClass
    > //e.Item.CssClass
    > // e.Item.DataItem
    >
    >
    > }
    >
    > Also, what are the arguments for the PreRender event? Is there some system
    > for knowing their args or is there a way to get VS 2003 to generate a
    > method that has the right args?
    >
    > I'm guessing it is this:
    > void PreRender(Object sender,System.EventArgs ee)
    >
    > But I'm not sure.
    >
    >
    > Eliyahu Goldin wrote:
    >> Sure it is possible.
    >>
    >> The DataGrid control has Items property. Items represent the rows.
    >>
    >> The are 2 standard ways of setting row properties:
    >>
    >> 1. In PreRender event loop through the myGrid.Items collection.
    >>
    >> 2. In ItemDataBound event you get access to each individual item,
    >> including the header and footer if any. You get a reference to the item
    >> as a parameter to the event handler.
    >>
    >> Eliyahu
    >>
    Eliyahu Goldin, Jan 19, 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. Luis Esteban Valencia

    Make a DataGrid row editable after user selects the row

    Luis Esteban Valencia, Jan 12, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    351
    Luis Esteban Valencia
    Jan 12, 2005
  2. =?Utf-8?B?S01aX3N0YXRl?=

    Quick one - Is SESSION per browser instance or per IP Address?

    =?Utf-8?B?S01aX3N0YXRl?=, Apr 4, 2006, in forum: ASP .Net
    Replies:
    7
    Views:
    5,861
    gerry
    Apr 10, 2006
  3. Razvan
    Replies:
    1
    Views:
    402
    tony vee
    Sep 10, 2004
  4. Michael
    Replies:
    0
    Views:
    213
    Michael
    Dec 29, 2003
  5. D
    Replies:
    0
    Views:
    197
Loading...

Share This Page