Column with buttons in SfDataGrid

Hello all,

I am using an SfDataGrid in my Xamarin.Forms project and i would like to have a column in which all the cells are buttons. I am doing everything programmatically so i am not using any of the .xaml pages. I have been searching online and everything i find is through the .xaml pages. 

Any help would be greatly appreciated.

3 Replies

AA Arulraj A Syncfusion Team August 23, 2018 11:21 AM UTC

Hi Constantinos, 
 
Thanks for contacting Syncfusion support. 
 
You can achieve your requirement with the GridTemplateColumn inside which you can host any view using the DataTemplate. Refer the following code example. 
 
GridTemplateColumn orderIdColumn = new GridTemplateColumn(); 
orderIdColumn.MappingName = "OrderID"; 
 
    orderIdColumn.CellTemplate = new DataTemplate(() => 
    { 
    Button btn = new Button(); 
    btn.SetBinding(Button.TextProperty, new Binding("OrderID")); 
    return btn; 
}); 
 
We have attached a sample for your requirement you can download the same from the following link. 

Arulraj A 



CO Coz August 23, 2018 01:14 PM UTC

Hello Arulraj,

Thank you for the quick reply. 

What i forgot to mention is that my data grid is being filled in using an observable collection, so i do not create the columns individually. One of the fields in my model is a string and i would like that column to be filled in with buttons that have as text the string of that field. 

So i have a model with the fields: Id, Name and Status. The first two columns get filled in fine as they are, but the status i want the column to be buttons and use the "Status" string as text for the buttons.

If you require any more information please let me know.

Thanks again for all the help.


AA Arulraj A Syncfusion Team August 24, 2018 10:58 AM UTC

Hi Constantinos, 
 
The DataGrid creates columns automatically based on the bindable property and SfDataGrid.AutoGenerateColumns.Columns are generated based on type of individual properties in the underlying collection which is set as ItemsSource. For example, GridNumericColumn is added for int type property. Below table shows the column type created for the respective data type. For remaining types, GridTextColumn will be created. 
 
 
For more details refer the help documentation link 
 
The GridTemplateColumn will not be created when the SfDataGrid.AutoGenerateColumns is set as true which is the behaviour of the DataGrid. So if you want to display the button in the column cells you need to create the GridTemplateColumn manually to achieve your requirement. For more details about the template column refer the help documentation in the following link. 
 
Regards, 
Arulraj A 


Loader.
Up arrow icon