我循环遍历每一列并检索行列值。现在基于某些条件我想为该特定行添加颜色。这是可能的吗
foreach (var columnName in ColumnNames)
{
if(something) //true , I want ot add colour and I tried
dr[columnName] = "<span style='background-color:yellow;'>" + row[columnName] + "</span>";
}
但它没有显示为文本而是没有应用样式。如何为其添加颜色
前端
<asp:GridView ID="gv" runat="server">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkAll" AutoPostBack="true" runat="server"/>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkItem" runat="server" AutoPostBack="true" OnCheckedChanged="chkItem_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
CS
foreach(GridViewRow gridRow in myGridview.Rows)
{
for(int i = 0; i < myGridview.Columns.Count, i++)
{
if(gridRow.Cells[i].Text.Contains('*'))
{
//Do your thing
gridRow.Cells[i].Text=gridRow.Cells[i].Text.Replace(@"*", "");
gridRow.Cells[i].Style.Font =
new Font("Ariel", 8, FontStyle.Underline);
}
}
}
答案 0 :(得分:2)
您可以像这样使用RowDataBound
事件: -
在您的gridview中附加事件: -
<asp:GridView ID="gv" runat="server" OnRowDataBound="gv_RowDataBound"></asp:GridView>
在代码背后: -
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[2].Text == "Youvalue") //condition here
{
e.Row.BackColor = System.Drawing.Color.Yellow;
}
}
}
在这里,您需要根据0
开始指定单元格索引。