Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Gridview Row Background Color using value in Binding DataSet

I'm having a GridView which contains a Column ID

I'm having a DataTable which contains two columns

ID
DONE

I'm Binding the ID Column in the DataTable to the GridView. Until no Its fine.

But now I need to set the Background color of the GridView Row Basing on the DONE Column Value in DataTable.( If DONE value is true the Row Background Color has to be changed.)

How can I achieve this without binding the DONE Row to the GridView??

like image 754
Krishna Thota Avatar asked Oct 31 '12 08:10

Krishna Thota


2 Answers

Create GridView1_RowDataBound event for your GridView.

if (e.Row.RowType == DataControlRowType.DataRow)
{
    //Check your condition here
    //Get Id from here and based on Id check value in the 
    //underlying dataSource Row where you have "DONE" column value
    // e.g.
    // (gridview.DataSource as DataTable), now you can find your row and cell 
    // of "Done"
    If(Condition True)
    {
        e.Row.BackColor = Drawing.Color.Red;  // your color settings 
    }
}

example code snippet:

protected void EmployeeAvailabilityGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
               if (e.Row.RowType == DataControlRowType.DataRow)
                {                  
                    if(Convert.ToBoolean(DataBinder.Eval(e.Row.DataItem, "DONE")))
                    {
                        e.Row.BackColor = System.Drawing.Color.LightPink;
                    }
                }
            }
            catch (Exception ex)
            {
                //ErrorLabel.Text = ex.Message;
            }
        }

Refer following link for more detailed implementation:
Change GridView row color based on condition

Note: If that row does not exist in DataSource then you must have some logic to get that from other place. May be you have ID as Foreign Key in another table.

like image 143
Niranjan Singh Avatar answered Oct 01 '22 10:10

Niranjan Singh


This link might help you

http://deepak-sharma.net/2012/01/27/how-to-change-the-background-color-of-rows-in-a-gridview-based-on-the-value-of-a-column-in-asp-net-3-5/

if (e.Row.RowType == DataControlRowType.DataRow)
    {
            // determine the value of the UnitsInStock field
            if((DataBinder.Eval(e.Row.DataItem,"strShift")).ToString() =="Alarm")
            {
                    // color the background of the row yellow
                    e.Row.BackColor = Color.Yellow;
            }
like image 40
Ramu Avatar answered Oct 01 '22 10:10

Ramu