我想根据某些条件更改gridview的特定行颜色,我使用ASP.NET和c#。 以下是输出示例:
<cc1:PagingGridView ID="TaskNameGrid"
runat="server"
AllowPaging="True"
AutoGenerateColumns="False"
CssClass="SearchResultsTable"
ShowHeader="true"
virtualitemcount1="-1"
OnRowdatabound="TaskNameGrid_Rowdatabound"
OnRowCancelingEdit="TaskNameGrid_RowCancelingEdit"
OnRowEditing="TaskNameGrid_RowEditing"
OnRowUpdating="TaskNameGrid_RowUpdating"
Width="400px">
<AlternatingRowStyle CssClass="alternate" />
<HeaderStyle ForeColor="White" />
<RowStyle />
<Columns>
<asp:BoundField HeaderText="Task Name" DataField="BusinessIdentifier" SortExpression="BusinessIdentifier" ReadOnly="True"></asp:BoundField>
<asp:TemplateField HeaderText="SLA" InsertVisible="False" SortExpression="sno">
<EditItemTemplate>
<asp:TextBox ID="SlaVariationTextBox" runat="server" Text='<%# Bind("SLA") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("SLA", "not set") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" />
</Columns>
</cc1:PagingGridView>
提前致谢
答案 0 :(得分:1)
HTML:
<div>
<asp:GridView ID="TaskNameGrid"
runat="server"
AllowPaging="True"
AutoGenerateColumns="False"
CssClass="SearchResultsTable"
virtualitemcount1="-1"
Width="400px" DataSourceID="ObjectDataSource1"
onrowdatabound="TaskNameGrid_RowDataBound">
<AlternatingRowStyle CssClass="alternate" />
<Columns>
<asp:BoundField DataField="BusinessIdentifier" HeaderText="BusinessIdentifier"
SortExpression="BusinessIdentifier" />
<asp:BoundField DataField="SLA" HeaderText="SLA" SortExpression="SLA" />
</Columns>
<HeaderStyle ForeColor="White" />
<RowStyle />
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Get"
TypeName="DataForGrid">
</asp:ObjectDataSource>
</div>
Aspx活动
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;
}
}
答案 1 :(得分:0)
使用RowDataBound
事件。您可以使用行DataItem
来获取基础DataSource
:
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;
}
}