In RadGrid, BackColor Of Selected Cell

1 Answer 120 Views
Grid
anna
Top achievements
Rank 1
Bronze
Iron
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

Sort by
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:

protected void RadGrid1_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;
        }
    }
}

I hope this helps.

Kind regards,
Doncho
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

anna
Top achievements
Rank 1
Bronze
Iron
commented on 26 Jan 2022, 04:00 PM

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

 

protected void RadGrid1_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:

<style>
    html body .RadGrid .pinkCell{
        background-image:none;
        background-color: lightpink;
    }
</style>
<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" Width="800px" OnNeedDataSource="RadGrid1_NeedDataSource" OnItemDataBound="RadGrid1_ItemDataBound">
</telerik:RadGrid>

C#

protected void RadGrid1_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" });
}

protected void RadGrid1_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.

 

Tags
Grid
Asked by
anna
Top achievements
Rank 1
Bronze
Iron
Answers by
Doncho
Telerik team
Share this question
or