从标签 ID 更改整个 gridview 单元格颜色

Change entire gridview cell color from label ID

试图找出当我有标签 ID 时如何更改 gridview 单元格的背景颜色。

<ItemTemplate> <asp:Label ID="thisLabel" runat="server" Text='<%# Bind("thisLabel") %>'></asp:Label> </ItemTemplate>

Label lbStdPrice = (Label)e.Row.FindControl("lbStdPrice"); lbPrice.BackColor = System.Drawing.Color.Yellow;

这只会突出显示文本。我希望它改变整个单元格的颜色,如:

e.Row.Cells[10].BackColor = System.Drawing.Color.LimeGreen;

您可以将父级转换回 TableCell 并更改颜色。

Label lbStdPrice = (Label)e.Row.FindControl("thisLabel");
TableCell cell = lbStdPrice.Parent as TableCell;
cell.BackColor = Color.LimeGreen;