gpt4 book ai didi

c# - 如何在页面/网格加载期间根据 gridviewcell 值动态更改 gridviewrow 颜色?

转载 作者:行者123 更新时间:2023-11-30 16:10:28 26 4
gpt4 key购买 nike

我有一个带有数据绑定(bind)值的 gridview。我在“ReportMain”表中有 5 行,其中包含字段(Id、日期、时间、名称、区域、工作、描述、优先级、状态),我正在显示所有 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;

}

请帮忙...

最佳答案

您始终可以使用行 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 中的每一行触发该事件),所以不一定在每次回发时。

The following error is shown. Unable to cast object of type 'ReportMain' to type '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;

关于c# - 如何在页面/网格加载期间根据 gridviewcell 值动态更改 gridviewrow 颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25785143/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com