Set Gridview Row Background Color using value in B

2020-07-13 08:44发布

问题:

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??

回答1:

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.



回答2:

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;
            }


回答3:

Create MyGridView _RowDataBound event for your GridView.

if (e.Row.RowType = DataControlRowType.DataRow)
{
    //Check your condition here, Cells[1] for ex. is DONE/Not Done column 
    If(e.Row.Cells[1].Text == "DONE")
    {
        e.Row.BackColor = Drawing.Color.Green // This will make row back color green
    }
}


回答4:

I also solved my condition. But For Alternative row type, the background color is not set.

       if (e.Row.RowType == DataControlRowType.DataRow)
        {
          Label LabelStatus = (Label)e.Row.FindControl("lblStatus");
            if(LabelStatus.Text.Trim().ToLower().Equals("inactive"))
            {
                e.Row.BackColor = System.Drawing.Color.Gray;
            }                   

        }

Can you please let me know what may be the reason?