GridView RowDataBound Color and SelectedRowStyle-BackColor

Last post 05-16-2008 6:17 PM by chriswight. 4 replies.

Sort Posts:

  • GridView RowDataBound Color and SelectedRowStyle-BackColor

    05-15-2008, 4:01 PM
    • Loading...
    • chriswight
    • Joined on 06-28-2006, 11:20 PM
    • Posts 53

    Hi Folks. What is the best way to make a RowDataBound event work with the SelectedRowStyle-BackColor? The BackColor of each row in the GridView is determined by the RowDataBound event (see below code), which over-rides the SelectedRowStyle-BackColor in the aspx page. How can I make it so that each row is color-coded, but when selected the color changes? Many thanks,

    if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView drv = e.Row.DataItem as DataRowView;
                if (drv["column1"].ToString().Equals("blah"))
                {
                    e.Row.BackColor = System.Drawing.Color.LightGreen;
                }
             }
     
  • Re: GridView RowDataBound Color and SelectedRowStyle-BackColor

    05-15-2008, 5:03 PM
    • Loading...
    • vinz
    • Joined on 10-05-2007, 11:47 AM
    • Cebu Philippines
    • Posts 6,026

    refer here 

    http://www.codeproject.com/KB/grid/GridViewRowColor.aspx

    Cheers,
    Vincent Maverick Durano


  • Re: GridView RowDataBound Color and SelectedRowStyle-BackColor

    05-16-2008, 2:06 PM
    • Loading...
    • chriswight
    • Joined on 06-28-2006, 11:20 PM
    • Posts 53

    OK, thanks for the link; however I'm not sure I want to go with the java workaround.

    Is there a way to change the BackColor of just one cell with RowDataBound? Thought I saw it somewhere earlier in another post, but now can't find it. Been playing around with it for about an hour but can't figure out how to do it.

  • Re: GridView RowDataBound Color and SelectedRowStyle-BackColor

    05-16-2008, 2:56 PM
    Answer
    • Loading...
    • vinz
    • Joined on 10-05-2007, 11:47 AM
    • Cebu Philippines
    • Posts 6,026

    chriswight:

    Is there a way to change the BackColor of just one cell with RowDataBound?

    if (GridView1.Rows.Count > 0)
    {
        GridView1.Rows[0].Cells[0].BackColor = System.Drawing.Color.Blue;
    }

    Cheers,
    Vincent Maverick Durano


  • Re: GridView RowDataBound Color and SelectedRowStyle-BackColor

    05-16-2008, 6:17 PM
    • Loading...
    • chriswight
    • Joined on 06-28-2006, 11:20 PM
    • Posts 53

    Thanks Vinz. You rule. For anybody who's interested, here's the full code I used to color just one cell of a row with RowDataBound, which allows the rest of the row to be highlighted in another color by using the SelectedRowStyle-BackColor in the aspx page:
     

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
         //ensure that the row is a data row (not a header or footer)
         if (e.Row.RowType == DataControlRowType.DataRow)
         {
              //retrieve the dataitem, and cast it to the appropriate type 
              DataRowView drv = e.Row.DataItem as DataRowView;
              //determine the value of the Status field
              if (drv["column0"].ToString().Equals("Blah"))
              {
                   e.Row.Cells[3].BackColor = System.Drawing.Color.Red;
              }
         }
    }
     
Page 1 of 1 (5 items)