如何在页面/网格加载期间根据网格单元格值动态更改网格行颜色

本文关键字:网格 单元格 动态 颜色 加载 | 更新日期: 2023-09-27 18:21:27

我有一个带有数据绑定值的网格视图。我在"ReportMain"表中有5行,其中有字段(Id、Date、Time、Name、Area、Work、Description、Priority、status),我在gridview中显示所有5行(运行时使用c#asp.net中的linq查询显示gridview名称为"gridview1",运行时显示这5行(3行状态为"已完成",其他2行状态为为"挂起")。现在我想用绿色显示状态字段为"完成"的行而其他2行在加载期间没有任何颜色。所有行单元格值都有值,并且不为null。。

我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Drawing;
using System.Web.UI.WebControls;
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        var query = from row in db.ReportMains select row;
        GridView1.DataSource = query;
        GridView1.DataBind();
    }
}
protected void chkRow_CheckedChanged(object sender, EventArgs e)
{
    CheckBox checkbox = (CheckBox)sender;
    GridViewRow gdrow = (GridViewRow)checkbox.NamingContainer;
    if (checkbox.Checked)
    {
        gdrow.BackColor = System.Drawing.Color.LimeGreen;
        int rowindex = gdrow.RowIndex;
        int i = Convert.ToInt32( gdrow.Cells[0].Text);
        string a = gdrow.Cells[3].Text.ToString();          
        var Query = (from row in db.ReportMains where row.EmployeeName==a && row.Id==i
                     select new
                     {
                        row.EmployeeName,row.Id
                     }).Distinct();
        if (Query.Count() > 0)
        {
            var Q1 = from row in db.ReportMains where row.Id == Query.Single().Id select row;
            Q1.Single().Status = "Completed";
            db.SubmitChanges();
        }           
    }      
    else
    {
        gdrow.BackColor = System.Drawing.Color.MintCream;
        int rowindex = gdrow.RowIndex;
        int i = Convert.ToInt32(gdrow.Cells[0].Text);
        string a = gdrow.Cells[3].Text.ToString(); 
        var Query = (from row in db.ReportMains
                     where row.EmployeeName == a && row.Id == i
                     select new
                     {
                         row.EmployeeName,
                         row.Id
                     }).Distinct();
        var Q1 = from row in db.ReportMains where row.Id == Query.Single().Id select row;
        Q1.Single().Status = null;
        db.SubmitChanges();
    }
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    ReportMain rm = (ReportMain)e.Row.DataItem;
    string st = rm.Status;
    bool isCompleted = st == "Completed";
    e.Row.BackColor = isCompleted ? Color.Green : Color.White;
}

请帮忙。。。

如何在页面/网格加载期间根据网格单元格值动态更改网格行颜色

您可以始终使用行DataItem来获取底层DataSource:

protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DataRow row   = ((DataRowView)e.Row.DataItem).Row;
        string name   = row.Field<string>("Name");
        string dept   = row.Field<string>("Dept");
        string work   = row.Field<string>("Work");
        string status = row.Field<string>("Status");
        bool isCompleted = status == "Completed"; // or status.Equals("Completed", StringComparison.CurrentCultureIgnoreCase)
        e.Row.BackColor = isCompleted ? Color.Green : Color.White;
    }
}

我会使用RowDataBound事件,因为它只在数据绑定期间(gridView1.DataBind()之后)为GridView中的每一行触发,所以不一定在每次回发时触发。

显示以下错误。无法强制转换类型为的对象"ReportMain"以键入"System.Data.DataRowView"。

什么是ReportMain?如果它是一个自定义类,则将e.Row.DataItem强制转换为它。然后,您可以像上面使用DataRow那样访问属性。

例如:

// ....
ReportMain rm = (ReportMain) e.Row.DataItem;
string name   = rm.Name;
string dept   = rm.Dept;
string work   = rm.Work;
string status = rm.Status;
bool isCompleted = status == "Completed"; // or status.Equals("Completed", StringComparison.CurrentCultureIgnoreCase)
e.Row.BackColor = isCompleted ? Color.Green : Color.White;