C# 使用绑定数据集中的值设置Gridview行背景颜色

C# 使用绑定数据集中的值设置Gridview行背景颜色,c#,asp.net,gridview,datatable,C#,Asp.net,Gridview,Datatable,我有一个GridView,其中包含一列ID 我有一个包含两列的数据表 ID DONE 我正在将DataTable中的ID列绑定到GridView。 直到天亮 但是现在我需要根据DataTable中的DONE列值设置GridView行的背景色。(如果DONE值为true行背景色必须更改。) 如何在不将DONE行绑定到GridView的情况下实现这一点???为GridView创建GridView1\u RowDataBound事件 if (e.Row.RowType == DataControlR

我有一个GridView,其中包含一列
ID

我有一个包含两列的数据表

ID
DONE
我正在将DataTable中的
ID
列绑定到GridView。 直到天亮

但是现在我需要根据DataTable中的
DONE
列值设置GridView行的背景色。(如果
DONE
值为
true
行背景色必须更改。)


如何在不将
DONE
行绑定到GridView的情况下实现这一点???

为GridView创建
GridView1\u RowDataBound
事件

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 
    }
}
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
    }
}
示例代码段:

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;
            }
        }
有关更详细的实现,请参阅以下链接:


注意:如果数据源中不存在该行,那么您必须有一些逻辑才能从其他位置获取该行。可能是您在另一个表中将
ID
作为外键。

为您的GridView创建MyGridView\u RowDataBound事件

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

我也解决了我的问题。但对于替代行类型,不设置背景色

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

        }

您能告诉我原因吗?

此链接可能会对您有所帮助


我是从DataBinder而不是GridView行中的内容中请求的!!很抱歉它很好用。看起来css的属性被超越了。