This is a migrated thread and some comments may be shown as answers.

How to add image to gridview command column

2 Answers 500 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Mark
Top achievements
Rank 1
Mark asked on 18 Nov 2015, 02:30 PM
I am attempting to add an image to the gridview command column programmatically depending on certain conditions. I can add an image through the property builder for all the rows, but how do I do this with code?

2 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 20 Nov 2015, 01:22 PM
Hello Mark,

Thank you for writing.

You can use the CellFormatting event and apply the desired image to the GridCommandCellElement.CommandButton.Image property: 
private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    GridCommandCellElement commandCell = e.CellElement as GridCommandCellElement;
    if (commandCell != null)
    {
        if (e.RowIndex % 2 == 0)
        {
            commandCell.CommandButton.Image = Properties.Resources.crayon;
            commandCell.CommandButton.ImageAlignment = ContentAlignment.MiddleCenter;
        }
        else
        {
            commandCell.CommandButton.ResetValue(LightVisualElement.ImageProperty, ValueResetFlags.Local);
            commandCell.CommandButton.ResetValue(LightVisualElement.ImageAlignmentProperty, ValueResetFlags.Local);
        }
    }
}

I hope this information helps. Should you have further questions I would be glad to help.
 
Regards,
Dess
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Mark
Top achievements
Rank 1
answered on 01 Dec 2015, 07:51 PM
Sorry for the delay on my response. That worked - Thanks
Tags
General Discussions
Asked by
Mark
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Mark
Top achievements
Rank 1
Share this question
or