Button in Datagrid

Good afternoon, excuse me, I have a datagrid that has a column with a button, when I click on the button I have to be able to get the value of one of the cells in the row where the button is located, but when I click on it the button the datagrid.selectitem = null. How could I do it?

5 Replies

SS Sivaraman Sivagurunathan Syncfusion Team March 3, 2020 10:26 AM UTC

Hi David, 

Thank you for using Syncfusion controls.   

You can achieve your requirement by  set the DataGrid.SelectedItem as BindingContext of the Button when clicked the button. 

Please refer the below codes: 
  
// Xaml Code 

<sfgrid:SfDataGrid x:Name="dataGrid" 
            ItemsSource="{Binding OrdersInfo}"  
            AutoGenerateColumns="False" 
            SelectionMode="Single" 
            ColumnSizer="Star"> 
    <sfgrid:SfDataGrid.Columns > 
        <sfgrid:GridTemplateColumn MappingName="LastName" > 
            <sfgrid:GridTemplateColumn.CellTemplate> 
                <DataTemplate> 
                    <Grid Padding="5,5,5,5"> 
                        <Button Text="click here" Clicked="Button_Clicked"/> 
                    </Grid> 
                </DataTemplate> 
            </sfgrid:GridTemplateColumn.CellTemplate> 
        </sfgrid:GridTemplateColumn> 
        <sfgrid:GridTextColumn MappingName="FirstName" /> 
        <sfgrid:GridTextColumn MappingName="LastName" /> 
        <sfgrid:GridTextColumn MappingName="FirstName"/> 
    </sfgrid:SfDataGrid.Columns> 
</sfgrid:SfDataGrid> 
 
//Code behind 
 
private void Button_Clicked(object sender, EventArgs e) 
{ 
    dataGrid.SelectedItem = (sender as Button).BindingContext; 
} 


If you are using the MVVM pattern. Please refer the below code snippet.  

//XAML Code  
 
<sfgrid:SfDataGrid x:Name="dataGrid" 
                ItemsSource="{Binding OrdersInfo}"  
                AutoGenerateColumns="False" 
                SelectionMode="Single" 
                SelectedItem="{Binding DataGridSelectedItem}" 
                ColumnSizer="Star"> 
    <sfgrid:SfDataGrid.Columns > 
        <sfgrid:GridTemplateColumn MappingName="LastName" > 
            <sfgrid:GridTemplateColumn.CellTemplate> 
                <DataTemplate> 
                    <Button WidthRequest="100" Text="Click"  Command="{Binding Source={x:Reference dataGrid}, Path=BindingContext.TapCommand}" CommandParameter="{Binding .}" /> 
                </DataTemplate> 
            </sfgrid:GridTemplateColumn.CellTemplate> 
        </sfgrid:GridTemplateColumn> 
        <sfgrid:GridTextColumn MappingName="FirstName" /> 
        <sfgrid:GridTextColumn MappingName="LastName" /> 
        <sfgrid:GridTextColumn MappingName="FirstName"/> 
    </sfgrid:SfDataGrid.Columns> 
</sfgrid:SfDataGrid> 
 
// ViewModel Codes 
 
private object dataGridSelectedItem; 
public ICommand TapCommand { private set; get; } 
 
public object DataGridSelectedItem 
{ 
    get 
    { 
        return dataGridSelectedItem; 
    } 
    set 
    { 
        this.dataGridSelectedItem = value; 
        RaisePropertyChanged("DataGridSelectedItem"); 
    } 
} 
public ViewModel() 
{ 
    order = new OrderInfoRepository(); 
    SetRowstoGenerate(5); 
    TapCommand = new Command(OnTapped); 
} 
 
public void OnTapped(object selectedItem) 
{ 
    this.DataGridSelectedItem = (selectedItem as OrderInfo); 
} 


We have prepared the sample based on your requirement and attached for your reference. You can download the same from the below link. 



MVVM Sample: https://www.syncfusion.com/downloads/support/forum/152089/ze/DataGridSample_(4)943544164 

Regards, 
Sivaraman S 
 



DA David March 3, 2020 02:23 PM UTC

Thank you!! this is that I need!


SS Sivaraman Sivagurunathan Syncfusion Team March 4, 2020 08:51 AM UTC

Hi David, 

We are happy to hear, that your issue has been resolve. Please let us know if you need any further details on this. 

Regards, 
Sivaraman S         



EV Enrique Velez April 27, 2024 10:17 PM UTC

Hello. 

I when I try to work with the fields of the button selected they are unabled.

Something like that:


private void Button_Clicked(object sender, EventArgs e) 
{ 
    dataGrid.SelectedItem = (sender as Button).BindingContext; 
    
    string Name = dataGrid.SelectedItem.OrdersInfo. LastName +  dataGrid.SelectedItem.OrdersInfo.FirstName; 
} 


Can you give some advice ?


Thaks 


Enrique





NY Nirmalkumar Yuvaraj Syncfusion Team April 29, 2024 10:09 AM UTC

Hi Enrique


Based on the information provided, the issue you are experiencing appears to be related to type conversion. Within the clicked event, you are accessing the binding context of the button. The binding context received will be of object type. Therefore, it is necessary to convert it to the model class for further use elsewhere. We have prepared a simple sample that fulfills your requirement. We have attached a code snippet and sample for your reference.


private void Button_Clicked(object sender, EventArgs e)

{

    dataGrid.SelectedItem = (sender as Button).BindingContext;

    var empInfo = dataGrid.SelectedItem as Employee;

    string Name = empInfo.EmployeeID + empInfo.Name;

}


Regards,

Nirmalkumar


Attachment: Xamarin_SfDataGridSample_fef4c197.zip

Loader.
Up arrow icon