anna
asked on 23 Jan 2022, 11:28 AM
| edited on 23 Jan 2022, 12:13 PM
RadGrid, ASP.NET, Web
BackColor does not work ....in the source below.
if (gv.Items.Count > 0) { foreach (GridDataItem item in gv.Items) { if (item.Cells[2].Text == "Total") {
item.BackColor = System.Drawing.Color.LightPink;
break; } } }
Please Help Me
1 Answer, 1 is accepted
0
Doncho
Telerik team
answered on 26 Jan 2022, 01:35 PM
Hi Anna,
Note that accessing cells in a GridDataItem should be done by using a Column UniqueName, instead of using the Cells collection, see Accessing Cells in DataItem.
One approach to color a specific item conditionally is to use the ItemDataBound event of the Grid:
protectedvoidRadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
var dataItem = e.Item as GridDataItem;
if (dataItem["columnUniqueName"].Text == "Total")
{
dataItem.BackColor = System.Drawing.Color.LightPink;
}
}
}
My question is that the BackColor function doesn't work.
RadGrid, ASP.NET, Web
item.BackColor = System.Drawing.Color.LightPink;
I wrote the source code as below. BackColor does not work.
please please help
protectedvoidRadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
var dataItem = e.Item as GridDataItem;
if (dataItem["columnUniqueName"].Text == "Total")
{
dataItem.BackColor = System.Drawing.Color.LightPink;
}
}
}
Doncho
Telerik team
commented on 31 Jan 2022, 09:35 AM
Hi Anna,
There might be some settings I am missing or custom CSS that affects the appearance when setting the BackColor property.
On my side, it is working as expected. Yet, an alternative approach to color the row is to add a custom CSS class to its cells and add the background color via CSS:
protectedvoidRadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
(sender as RadGrid).DataSource = Enumerable.Range(1, 10).Select(x => new { ID = x, CustomField = x % 2 == 0 ? "Total" : "SubTotal" });
}
protectedvoidRadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
var dataItem = e.Item as GridDataItem;
if (dataItem["CustomField"].Text == "Total")
{
//dataItem.BackColor = System.Drawing.Color.LightPink;foreach (TableCell cell in dataItem.Cells)
{
cell.CssClass = "pinkCell";
}
}
}
}
If this is not helping, it would be helpful if you share the entire ASPX and aspx.cs pages so we can get a better idea of the current setup and assist more accurately.