Add button to grid

10 posts, 1 answers
  1. roy z
    roy z avatar
    1 posts
    Member since:
    Oct 2009

    Posted 17 May 2010 Link to this post

    Hello,
    I have a telerik gridview whice have a datatable as his datasource.
    I want to add column with a button (after I bound the grid to the datatable in run time).
    the button will use to get an Id from the row to delete a record.
    I managed to add column with a button but I can't change the button image.

    Any help?

    Thanks,
    Roy.
  2. Alexander
    Admin
    Alexander avatar
    306 posts

    Posted 19 May 2010 Link to this post

    Hello roy z,

    Thank you for your question.

    You can use the ViewCellFormatting event to set image to the GridViewCommandColumn cells:
    void radGridView1_ViewCellFormatting(object sender, CellFormattingEventArgs e)
    {
        if (e.CellElement is GridCommandCellElement)
        {
            ((GridCommandCellElement)e.CellElement).CommandButton.DisplayStyle = Telerik.WinControls.DisplayStyle.Image;
            ((GridCommandCellElement)e.CellElement).CommandButton.Image = Resources.Image; // get your image from the project Resources
        }
    }

    Please write back if you have additional questions.

    Regards,
    Alexander
    the Telerik team

    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 Public Issue Tracking system and vote to affect the priority of the items.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. roy z
    roy z avatar
    1 posts
    Member since:
    Oct 2009

    Posted 20 May 2010 Link to this post

    -----edited-----
    Ok, I found the element. Don't know why I didn't saw it before... strange but It seems ok now.
    ------end of edit--------

    Hello Alexander ,
    I want to the grid events and double click on the ViewCellFormatting event to create the event.
    Then I started typing your solution but It didn't find me the object  "GridCommandCellElement".

    I tried to add the telerik.wincontrol.Ui before it but it didn't work.
    Do you know why I can't find this element?

    The header of the function I've created look like this:
    void radGridViewMain_ViewCellFormatting(object sender, CellFormattingEventArgs e)
    {
    }
    Thanks,
    Roy.
  5. roy z
    roy z avatar
    1 posts
    Member since:
    Oct 2009

    Posted 20 May 2010 Link to this post

    Hi again,
    It still seems like I have Some problem with the telerik definitions.
    I manage to get inside the : if (e.CellElement is GridCommandCellElement)
    but when I'm at the row:
    ((GridCommandCellElement)e.CellElement).CommandButton
    I get the error that "CommandButton" was not found.
    Maybe the cast doesn't work?
    I have the "telerik.wincontrols.Ui" in the using.

    Any ideas why I can't find the CommandButton property?

    Thanks,
    Roy.



  6. Answer
    Alexander
    Admin
    Alexander avatar
    306 posts

    Posted 20 May 2010 Link to this post

    Hi Roy,

    GridCommandCellElement is in the namespace Telerik.WinControls.UI, as you have found:

    using Telerik.WinControls.UI;

    I assumed you add GridViewCommandColumn which seems most appropriate for your case (it is found in the same namespace). You can do that after RadGridView is bound using the code:

    GridViewCommandColumn commandColumn = new GridViewCommandColumn();
    this.radGridView1.Columns.Add(commandColumn);

    Then you can handle the ViewCellFormatting event with the code given in the previous post:

    this.radGridView1.ViewCellFormatting += new CellFormattingEventHandler(radGridView1_ViewCellFormatting);
     
    void radGridView1_ViewCellFormatting(object sender, CellFormattingEventArgs e)
    {
        if (e.CellElement is GridCommandCellElement)
        {
            ((GridCommandCellElement)e.CellElement).CommandButton.DisplayStyle = Telerik.WinControls.DisplayStyle.Image;
            ((GridCommandCellElement)e.CellElement).CommandButton.Image = Resources.Image; // get your image from the project Resources
        }
    }

    Please, verify also that you are using the last version of the Telerik WinForms Controls.

    A bit off topic, please ask the person who has purchased our controls in your company to add you as a License Developer to the purchase. This will give you full access to the products your company has purchased, to our downloads section, and to our support ticketing system. Additionally, all your questions will be reviewed according to the license you have. More information on License Developers you can find here: www.telerik.com/account/faqs.aspx.

    Regards,
    Alexander
    the Telerik team

    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 Public Issue Tracking system and vote to affect the priority of the items.
  7. Denius
    Denius avatar
    11 posts
    Member since:
    Oct 2015

    Posted 31 Aug in reply to Alexander Link to this post

    Hi,

    please explain to me , where put this line code?

    this.radGridView1.ViewCellFormatting += new CellFormattingEventHandler(radGridView1_ViewCellFormatting);

    On page Load, or DataBound grid event.

     

    Now i have one error for last line Image is underline red... compiler says

    The type or namespace name 'Image' does not exist in the namespace 'GUI.Resources' (are you missing an assembly reference?)

    use >using Telerik.WinControls.UI<

  8. Denius
    Denius avatar
    11 posts
    Member since:
    Oct 2015

    Posted 31 Aug in reply to Denius Link to this post

    I made button it's works fine, please tell me how to add Image button on grid?
  9. Dess
    Admin
    Dess avatar
    1605 posts

    Posted 02 Sep Link to this post

    Hello Goran,

    Thank you for writing.  

    It is recommended to subscribe to the ViewCellFormatting event either at design time or before the grid is populated with data, e.g. before setting the DataSource property. In order to show image button in grid cells, you can use the GridViewCommandColumn and assign an image in the ViewCellFormatting event. I have prepared a sample project for your reference where the image is taken from the Resources.

    I hope this information helps. Should you have further questions I would be glad to help.

    Regards,
    Dess
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
  10. Denius
    Denius avatar
    11 posts
    Member since:
    Oct 2015

    Posted 02 Sep in reply to Dess Link to this post

    Hi Dess's ,

    tnx for halp me.

     

    I find solution , I put code on DataBounding, and works only add flag when bound gird only one time show buttons.

    To add image on button this is code

     commandColumn.Image = Properties.Resources.(your name image);

  11. Dess
    Admin
    Dess avatar
    1605 posts

    Posted 05 Sep Link to this post

    Hello Goran, 

    Thank you for writing back. 

    I am glad that you have found a suitable solution for your case. However, note that by setting the GridViewCommandColumn.Image property you will have one common picture for all rows in this column. If you use the ViewCellFormatting/CellFormatting event you can specify a different image for each cell.

    I hope this information helps. If you have any additional questions, please let me know.

    Regards,
    Dess
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
Back to Top
UI for WinForms is Visual Studio 2017 Ready