locked
Highlight row in Gridview if column meets condition RRS feed

  • Question

  • User-124626679 posted

    Hi, i'm new to this stuff and am trying to figure out how to automatically highlight a row if column meets a certain condition.

     I just want the row to change to red if the column with a value of "MsgPriority" equals "Local0.Alert".

     help?

     Thanks,

    Brandon

    Thursday, March 1, 2007 8:48 AM

Answers

  • User77042963 posted
     
     protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
    
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (Convert.ToString(DataBinder.Eval(e.Row.DataItem, "MsgPriority")) == "Local0.Alert")
                {
                    e.Row.BackColor = System.Drawing.Color.Red;
                }
                else 
                {
                    e.Row.ForeColor = System.Drawing.Color.Yellow;
                }
    
            }
        }
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 1, 2007 5:24 PM

All replies

  • User60581005 posted

    You can set the OnDataBound property of a gridview to a subroutine where you can do the checking.

    Sub gvHighlight(ByVal s As Object, ByVal e As EventArgs)
         For Each gr As GridViewRow In s.Rows
              If gr.Cells(1).Text = "Local0.Alert" Then
                    gr.BackColor = Drawing.Color.Red
              End If
         Next
    End Sub
    
    ...
    
    <asp:GridView ID="GridView1" runat="server" OnDataBound="gvHighlight" />
      
    Thursday, March 1, 2007 9:53 AM
  • User-124626679 posted

    any possibility you could hit me with the C# code as that's what the pages are in.

     this extremely helpful.

    thanks,

    Brandon

    Thursday, March 1, 2007 11:45 AM
  • User77042963 posted

    You can check your value conditon from OnRowBataBound too: 

       <asp:GridView ID="GridView1" runat="server"  OnRowDataBound="GridView1_OnRowDataBound" ....>
      
    protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {       
    
           if (e.Row.RowType == DataControlRowType.DataRow)
             {
    
                 Single  myWr=100.00F;
    
                 if (DataBinder.Eval(e.Row.DataItem, "Wr") != System.DBNull.Value) 
               {
                 myWr = System.Convert.ToSingle(DataBinder.Eval(e.Row.DataItem, "Wr"));
               }
    
                 if ((myWr < 75 && myWr >= 40))
                 {
                     e.Row.BackColor = System.Drawing.Color.Yellow;
                     e.Row.ForeColor = System.Drawing.Color.Red;
                 }
                 else if (myWr < 40 || myWr > 125)
                 {
                    e.Row.BackColor = System.Drawing.Color.Red;
                     e.Row.ForeColor = System.Drawing.Color.Yellow;
                 }
              
             }
     }
    Thursday, March 1, 2007 1:10 PM
  • User-124626679 posted

    I'm not much of a programmer, but I'm trying, is there anyway you could give me some help getting my specific variables in there.

     thanks,

    Brandon

    Thursday, March 1, 2007 3:27 PM
  • User77042963 posted
     
     protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
    
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (Convert.ToString(DataBinder.Eval(e.Row.DataItem, "MsgPriority")) == "Local0.Alert")
                {
                    e.Row.BackColor = System.Drawing.Color.Red;
                }
                else 
                {
                    e.Row.ForeColor = System.Drawing.Color.Yellow;
                }
    
            }
        }
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 1, 2007 5:24 PM
  • User-124626679 posted

    worked perfect. Thanks for the help!

     

    Brandon

    Friday, March 2, 2007 8:19 AM
  • User1939924996 posted

    It worked perfect thanks for ur help

    Wednesday, February 6, 2008 5:24 AM