Hiding images in one cells depending on data in another cell

1 Answer 67 Views
GridView
Carl
Top achievements
Rank 1
Iron
Iron
Iron
Carl asked on 03 Jan 2024, 07:42 PM | edited on 03 Jan 2024, 07:52 PM

I have a grid that has 2 image columns - one for an edit icon and one for Delete.  I need separate icons to so they show properly when the row is highlighted and when it is not. I'm adding the icons as shown below. Each row will get these icons. However, if the value in the Relation column is set to Maker then there should be no Delete icon and the Delete event should not fire.

When I set e.Row.Cells["Delete"].Value = null;, nothing happens. The Delete icon remains. What am I missing here?

void gvRelatedPhoneNumbers_CellFormatting(object sender, CellFormattingEventArgs e)
{

    if (e.CellElement is GridDataCellElement)
    {
        if (e.Column.Name == "Propertys") // Edit
            e.CellElement.Image = e.Row.IsSelected ? Properties.Resources.EditPenWhite16 : Properties.Resources.EditPen16;

        if (e.Column.Name == "Delete") // Delete
            e.CellElement.Image = e.Row.IsSelected ? Properties.Resources.TrashcanWhite16 : Properties.Resources.Trashcan16;

        if (e.Column.Name == "Relation")
        {
            if (e.CellElement.Value != "Maker")
                e.Row.Cells["Delete"].Value = null;
        }

    }
}

Thanks

Carl

1 Answer, 1 is accepted

Sort by
0
Nadya | Tech Support Engineer
Telerik team
answered on 08 Jan 2024, 09:48 AM

Hello, Carl,

Thank you for sharing your code snippet.

To achieve the desired behavior, it is not necessary to change the cell value to null in the CellFormatting event. Instead, you can add one additional check and show the delete icon only if the cell value in the "Relation" column is not set to "Maker". I updated the provided code snippet which result is demonstrated below:

private void RadGridView1_CellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e)
{
    if (e.CellElement is GridDataCellElement)
    {
        if (e.Column.Name == "Properties") // Edit
            e.CellElement.Image = e.Row.IsSelected ? Properties.Resources.pen : Properties.Resources.pen;

        if (e.Column.Name == "Delete") // Delete
            if (e.Row.Cells["Relation"].Value != null && e.Row.Cells["Relation"].Value.ToString() != "Maker")
                e.CellElement.Image = e.Row.IsSelected ? Properties.Resources.delete : Properties.Resources.delete;

    }

}

I hope this helps. If you have any other questions do not hesitate to contact me.

Regards,
Nadya | Tech Support Engineer
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.

Tags
GridView
Asked by
Carl
Top achievements
Rank 1
Iron
Iron
Iron
Answers by
Nadya | Tech Support Engineer
Telerik team
Share this question
or