C# 4.0 如何在C中根据条件更改GridView行颜色#
我想根据某些条件更改gridview的特定行颜色,我正在使用ASP.NET和c#。 以下是示例输出:C# 4.0 如何在C中根据条件更改GridView行颜色#,c#-4.0,gridview,C# 4.0,Gridview,我想根据某些条件更改gridview的特定行颜色,我正在使用ASP.NET和c#。 以下是示例输出: 提前感谢正如蒂姆所说,使用RowDataBound Html: 数据层类 public class DataStructure { public string BusinessIdentifier { get; set; } public string SLA { get; set; } public DataStructure() {
提前感谢正如蒂姆所说,使用RowDataBound Html: 数据层类
public class DataStructure
{
public string BusinessIdentifier { get; set; }
public string SLA { get; set; }
public DataStructure()
{
}
}
public class DataForGrid
{
public DataForGrid()
{
this.Stuff = new List<DataStructure>();
this.Stuff.Add(new DataStructure { BusinessIdentifier = "TaskName1", SLA = "1.2" });
this.Stuff.Add(new DataStructure { BusinessIdentifier = "Taskname2", SLA = null });
this.Stuff.Add(new DataStructure { BusinessIdentifier = "TaskName3", SLA = "1.2" });
this.Stuff.Add(new DataStructure { BusinessIdentifier = "TaskName4", SLA = "1.2" });
this.Stuff.Add(new DataStructure { BusinessIdentifier = "TaskName5", SLA = "1.2" });
}
public List<DataStructure> Stuff { get; set; }
public List<DataStructure> Get()
{
return this.Stuff;
}
}
公共类数据结构
{
公共字符串BusinessIdentifier{get;set;}
公共字符串SLA{get;set;}
公共数据结构()
{
}
}
公共类DataForGrid
{
公共数据格式()
{
this.Stuff=新列表();
this.Stuff.Add(新数据结构{BusinessIdentifier=“TaskName1”,SLA=“1.2”});
this.Stuff.Add(新数据结构{BusinessIdentifier=“Taskname2”,SLA=null});
this.Stuff.Add(新数据结构{BusinessIdentifier=“TaskName3”,SLA=“1.2”});
this.Stuff.Add(新数据结构{BusinessIdentifier=“TaskName4”,SLA=“1.2”});
this.Stuff.Add(新数据结构{BusinessIdentifier=“TaskName5”,SLA=“1.2”});
}
公共列表内容{get;set;}
公共列表Get()
{
归还这个东西;
}
}
使用行数据绑定
事件。您可以使用这些行来获取底层的数据源
:
protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// modify it accoording to your datasource, use the debugger if you're not sure
DataRow row = ((DataRowView)e.Row.DataItem).Row;
// just an example:
bool redCondition = row.Field<string>("SomColumn") == "Some Value";
e.Row.BackColor = redCondition ? Color.Red : GridView1.RowStyle.BackColor;
}
}
受保护的无效gridview1\u行数据绑定(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
//根据您的数据源修改它,如果您不确定,请使用调试器
DataRow row=((DataRowView)e.row.DataItem).row;
//举个例子:
bool redCondition=row.Field(“SomColumn”)=“Some Value”;
e、 Row.BackColor=redCondition?Color.Red:GridView1.RowStyle.BackColor;
}
}
这是一个ASP.NET网格
protected void TaskNameGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var d = (DataStructure)e.Row.DataItem;
if (string.IsNullOrEmpty(d.SLA))
{
e.Row.BackColor = System.Drawing.Color.Red;
}
}
}
public class DataStructure
{
public string BusinessIdentifier { get; set; }
public string SLA { get; set; }
public DataStructure()
{
}
}
public class DataForGrid
{
public DataForGrid()
{
this.Stuff = new List<DataStructure>();
this.Stuff.Add(new DataStructure { BusinessIdentifier = "TaskName1", SLA = "1.2" });
this.Stuff.Add(new DataStructure { BusinessIdentifier = "Taskname2", SLA = null });
this.Stuff.Add(new DataStructure { BusinessIdentifier = "TaskName3", SLA = "1.2" });
this.Stuff.Add(new DataStructure { BusinessIdentifier = "TaskName4", SLA = "1.2" });
this.Stuff.Add(new DataStructure { BusinessIdentifier = "TaskName5", SLA = "1.2" });
}
public List<DataStructure> Stuff { get; set; }
public List<DataStructure> Get()
{
return this.Stuff;
}
}
protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// modify it accoording to your datasource, use the debugger if you're not sure
DataRow row = ((DataRowView)e.Row.DataItem).Row;
// just an example:
bool redCondition = row.Field<string>("SomColumn") == "Some Value";
e.Row.BackColor = redCondition ? Color.Red : GridView1.RowStyle.BackColor;
}
}