GridView - RowCreated EventHandler doesn't work if page IsPostBack

Discussion in 'ASP .Net' started by Paul, Apr 29, 2007.

  1. Paul

    Paul Guest

    Hello All,

    I am trying to use the following RowCreated Event Handler to make the
    BackColor of a cell different if it has a particular value.

    protected void GridView1_RowCreated(object sender,
    GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {

    object cellValue = DataBinder.Eval(e.Row.DataItem,
    "Primary Diagnosis");
    if(cellValue != null)
    {

    if (cellValue.Equals("Unknown or Service Recipient
    Declined"))
    {
    e.Row.Cells[1].BackColor = Color.Yellow;
    }

    }
    }
    }

    The page works as expected the first time it is loaded. But if it is a
    PostBack cellValue is null, so the code to color the cells BackColor
    does not run. If I take out condition "if(cellValue != null) I get an
    error message. How can I use the RowCreated Event handler to control
    the BackColor of cells even when it is a PostBack.

    Help is always appreciated. Thanks,

    Paul
    Paul, Apr 29, 2007
    #1
    1. Advertising

  2. Paul,

    Two things:

    1. Use RowDataBound event instead of RowCreated.

    2. There is no need to call DataBinder.Eval. You can use e.Row.DataItem with
    typecasting. If your datasource is a datatable, you can write something like

    object cellValue = (DataRowView)e.Row.DataItem["Primary Diagnosis"];

    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    "Paul" <> wrote in message
    news:...
    > Hello All,
    >
    > I am trying to use the following RowCreated Event Handler to make the
    > BackColor of a cell different if it has a particular value.
    >
    > protected void GridView1_RowCreated(object sender,
    > GridViewRowEventArgs e)
    > {
    > if (e.Row.RowType == DataControlRowType.DataRow)
    > {
    >
    > object cellValue = DataBinder.Eval(e.Row.DataItem,
    > "Primary Diagnosis");
    > if(cellValue != null)
    > {
    >
    > if (cellValue.Equals("Unknown or Service Recipient
    > Declined"))
    > {
    > e.Row.Cells[1].BackColor = Color.Yellow;
    > }
    >
    > }
    > }
    > }
    >
    > The page works as expected the first time it is loaded. But if it is a
    > PostBack cellValue is null, so the code to color the cells BackColor
    > does not run. If I take out condition "if(cellValue != null) I get an
    > error message. How can I use the RowCreated Event handler to control
    > the BackColor of cells even when it is a PostBack.
    >
    > Help is always appreciated. Thanks,
    >
    > Paul
    >
    Eliyahu Goldin, Apr 29, 2007
    #2
    1. Advertising

  3. Paul

    Paul Guest

    Eliyahu,

    Works beautifully. Thanks,

    Paul
    Paul, Apr 29, 2007
    #3
    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. Joey
    Replies:
    2
    Views:
    3,945
  2. Pipo
    Replies:
    1
    Views:
    655
    Teemu Keiski
    Apr 1, 2006
  3. Replies:
    2
    Views:
    3,296
    fiendiest
    Oct 25, 2007
  4. K B
    Replies:
    1
    Views:
    12,292
    PeterKellner
    Jun 6, 2006
  5. Intermittent IsPostBack/EventHandler problem

    , Mar 30, 2007, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    159
Loading...

Share This Page