【发布时间】: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。