Search This Blog

Monday, September 6, 2010

GridView color change depending on inner value

I think most of the coders need it to check a particular value and need to highlight it on the gridview...

If you are looking for the same wtih a integer value, then you can follow this...

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // This line will get the reference to the underlying row
            DataRowView _row = (DataRowView)e.Row.DataItem;
            if (_row != null)
            {
                // get the field value which you want to compare and
                // convert to the corresponding data type
                // i assume the fieldName is of int type
                int _field = Convert.ToInt32(_row.Row["fieldName"]);
                if (_field == 2)
                    e.Row.BackColor = System.Drawing.Color.Green;
                else
                    e.Row.BackColor = System.Drawing.Color.Red;
            }
        }
    }

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.