How to add image to gridview command column

3 posts, 0 answers
  1. Mark
    Mark avatar
    2 posts
    Member since:
    Jan 2012

    Posted 18 Nov 2015 Link to this post

    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. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 20 Nov 2015 Link to this post

    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
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Mark
    Mark avatar
    2 posts
    Member since:
    Jan 2012

    Posted 01 Dec 2015 in reply to Dess Link to this post

    Sorry for the delay on my response. That worked - Thanks
Back to Top