We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to Change column to be a button or a link (underline and blue) in c# code behind

it is posible to display a button or a link in a cell so the user know that there is a event there not using XAML.

thanks

5 Replies

DS Divakar Subramaniam Syncfusion Team September 16, 2016 09:14 AM UTC

Hi Axel, 
 
Thanks for contacting Syncfusion Support.  
 
It is possible to display a button or a link in a cell in SfDataGrid using GridTemplateColumn. In GridTemplateColumn, you can able to load any custom views by using GridTemplateColumn.CellTemplate property and also you can able to load button in the header cell using GridTemplateColumn.HeaderTemplate property as in below code snippet. 
 
var column1 = new GridTemplateColumn() 
{ 
    MappingName = "CustomerID", 
    CellTemplate = new DataTemplate(typeof(ButtonClass)) //where ButtonClass is the custom view derived from Button. 
    HeaderTemplate = new DataTemplate(typeof(HeaderTemplate)) //where HeaderTemplate is the custom view derived from Button. 
 }; 
 
Please refer the below UG link to know more about GridTemplateColumn. 
 
 
We have prepared a sample by loading button in GridTemplateColumn for your reference and you can download the same from the below link. 
 
 
Also, you can able to load the buttons to the GridTemplateColumn at runtime. We have explained the same in the sample. 
 
Regards, 
Divakar.  
 



RP Robbie Philips October 19, 2016 10:20 AM UTC

How would you set the Clicked event property for the button? Only way I can find to do it is from within the ButtonClass but that's no good for me.

Thanks


DS Divakar Subramaniam Syncfusion Team October 20, 2016 09:56 AM UTC

Hi Robbie, 
 
 
Thanks for the update. 
 
 
You can set the clicked event property for the button in App.cs itself instead of setting in ButtonClass. Please refer the below code snippet to know how to achieve it. 
 
var column1 = new GridTemplateColumn() 
{ 
    MappingName = "CustomerID", 
    CellTemplate = new DataTemplate(() => 
    { 
        var button = new ButtonClass(); 
        button.Clicked += Button_Clicked; 
        return button; 
    }),          
    HeaderTemplate = new DataTemplate(typeof(HeaderTemplate)) 
}; 
 
 
Also, we have modified the sample based on your requirement and you can download the same from the below link. 
 
 
 
Regards, 
Divakar. 



RP Robbie Philips October 20, 2016 10:20 AM UTC

Thanks for that Divakar - seems I was just unsure of the syntax to initialise the object.


DS Divakar Subramaniam Syncfusion Team October 20, 2016 12:07 PM UTC

Hi Robbie, 
 
 
Thanks for the update. Please let us know if you need any other assistance from us. 
 
 
Regards, 
Divakar. 
 


Loader.
Up arrow icon