【问题标题】:How to dynamically change gridviewrow color based on gridviewcell value during page/grid load?如何在页面/网格加载期间根据 gridviewcell 值动态更改 gridviewrow 颜色?
【发布时间】:2014-11-05 06:23:57
【问题描述】:

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

我的代码:

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;

}

请帮忙...

【问题讨论】:

  • 我已删除我的 cmets 并将其发布为答案。
  • 既然你已经展示了你的源代码,你还没有使用我下面的代码,尤其是缺少if (e.Row.RowType == DataControlRowType.DataRow)。第一行是没有DataItem 的标题,这导致了NullReferenceException

标签: c# asp.net gridview


【解决方案1】:

您始终可以使用行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;

【讨论】:

  • @Karthick:ReportMain 是什么?如果是自定义类,则将 e.Row.DataItem 转换为该类。然后你可以像我使用DataRow 一样访问属性。我已经相应地编辑了我的答案。
  • @Karthick:然后e.Row.DataItemnull。您如何以及在何处分配了GridViewDataSource,请向我们展示该代码。什么是ReportMain,为什么是null
  • @Karthick:您没有提到为什么数据源在数据绑定时为空。但是,如果您不想直接使用源代码,您也可以阅读Cell.Text。假设状态为第 9 列:bool isCompleted = e.Row.Cells[8].Text == "Completed"; e.Row.BackColor = isCompleted ? Color.Green : Color.White;
  • @Karthick:既然你已经发布了你的源代码,我注意到你没有使用我的代码。您省略了if (e.Row.RowType == DataControlRowType.DataRow)-check,这很重要,因为第一行是Header,它没有DataItem
  • 非常感谢您的耐心帮助我......现在我明白了我的错误并得到了确切的结果......
猜你喜欢
  • 1970-01-01
  • 2011-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-24
相关资源
最近更新 更多