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??
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With