how to pass name as a parameter at each row button

i have a datagrid with name and email for the column and last with the button , i would like to pass the name for each row into the button as parameter, how can i do it ? please help.


my xaml code : 


            <syncfusion:SfDataGrid

            x:Name="dataGrid"

            AllowSorting="True"

            VerticalOptions="FillAndExpand"

                AutoGenerateColumns="False"

                ColumnSizer="Star"

                 ItemsSource="{Binding Getdataview}"

        >

                    <syncfusion:GridTextColumn x:Name="myname" MappingName="Name"/>


                    <syncfusion:GridTextColumn MappingName="Customer Email" />


                    <syncfusion:GridTemplateColumn MappingName="Action" >

                        <syncfusion:GridTemplateColumn.CellTemplate>

                            <DataTemplate>

                                <Grid Padding="5,5,5,5">

                                 <!-- <Button x:Name="btn" Text="View" Clicked="Button_Clicked" CommandParameter="{Binding Source={x:Reference myname}, Path=MappingName}"/>

                                    -->

                                    <Button x:Name="btn" Text="View" Clicked="Button_Clicked" CommandParameter="{Binding}"/>

                                </Grid>

                            </DataTemplate>

                        </syncfusion:GridTemplateColumn.CellTemplate>

                    </syncfusion:GridTemplateColumn>

                 </syncfusion:SfDataGrid.Columns>




            </syncfusion:SfDataGrid>


what should i do with this : 


  private  void Button_Clicked(object sender,System.EventArgs e) {

}

        {



1 Reply

SY Suthi Yuvaraj Syncfusion Team September 26, 2022 01:59 PM UTC

Hi Kenny ,


We would like to let you can achieve your requirement by binding the Command to the button and passing the parameter to the command as CommandParameter, Here you can specific the field defined in the model to pass them as the parameter. Command is used to response to click in MVVM Pattern. We have attached the code snippet and documentation for your reference


Code snippet

In XAML:

<sfgrid:SfDataGrid x:Name="dataGrid"

ItemsSource="{Binding State}"

NavigationMode="Cell"

AllowEditing="True"

ColumnSizer="Auto"

SelectionMode="Multiple"

AutoGenerateColumns="True">

<sfgrid:SfDataGrid.Columns>

<sfgrid:GridTextColumn MappingName="Name"/>

<sfgrid:GridTemplateColumn MappingName="Province" >

<sfgrid:GridTemplateColumn.CellTemplate>

<DataTemplate>

<Grid Padding="5,5,5,5">

<Button x:Name="btn" Text="{Binding Province}" Command="{Binding Path=BindingContext.ClickCommand, Source={x:Reference dataGrid}}" CommandParameter="{Binding Name}"/>

</Grid>

</DataTemplate>

</sfgrid:GridTemplateColumn.CellTemplate>

</sfgrid:GridTemplateColumn>


In ViewModel.cs:

class ViewModel : INotifyPropertyChanged

{

public Command<object> ClickCommand { get; set; }

public ViewModel()

{

    ClickCommand = new Command<object>(Click);

}

 

public void Click(object obj)

{

   var name = obj;   // here you can get the obj as command parameter ,In this example Command parameter is “Name”, you can get name in the obj

   //Required action to do         

}


Documentation link:


Xamarin.Forms Button - Xamarin | Microsoft Learn


Please let us know if you need further assistance.


Regards,

Suthi Yuvaraj


Loader.
Up arrow icon