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

Add button to grid

9 Answers 998 Views
GridView
This is a migrated thread and some comments may be shown as answers.
roy z
Top achievements
Rank 1
roy z asked on 17 May 2010, 01:57 PM
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.

9 Answers, 1 is accepted

Sort by
0
Alexander
Telerik team
answered on 19 May 2010, 03:51 PM
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.
0
roy z
Top achievements
Rank 1
answered on 20 May 2010, 05:14 AM
-----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.
0
roy z
Top achievements
Rank 1
answered on 20 May 2010, 05:56 AM
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.



0
Accepted
Alexander
Telerik team
answered on 20 May 2010, 05:20 PM
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.
0
Denius
Top achievements
Rank 1
answered on 31 Aug 2016, 08:00 AM

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<

0
Denius
Top achievements
Rank 1
answered on 31 Aug 2016, 01:55 PM
I made button it's works fine, please tell me how to add Image button on grid?
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 02 Sep 2016, 08:01 AM
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.
0
Denius
Top achievements
Rank 1
answered on 02 Sep 2016, 08:36 AM

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);

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 05 Sep 2016, 01:40 PM
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.
Tags
GridView
Asked by
roy z
Top achievements
Rank 1
Answers by
Alexander
Telerik team
roy z
Top achievements
Rank 1
Denius
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or