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
close icon

How can I get RowIndex of SfDataGrid at button click

hello,

I am having a SfDataGrid with a button in each row. 

  1. How can I get RowIndex on Button Click. 
  2. How can I get CommandParameter  and Tag property value on button click
  3. How can I get Hidden cell value in a row on Button click. e.g ( <Grid1:GridTextColumn MappingName="CaseNumber" HeaderText="CASE NUMBER" IsHidden="True" /> )

Below is my XAML code.

 <Grid1:SfDataGrid x:Name="AppointmentResultGrid"  HorizontalAlignment="Left"
                        CellStyle="{StaticResource customCellStyle}"  HeaderStyle="{StaticResource headerCellStyle}" 
                        RowStyleSelector="{StaticResource rowStyleSelector}"
                        Grid.Row="3" Grid.Column="1" BorderThickness="0"   Margin="220,0,0,183"
                        Width="992"  SelectionMode="Single" 
                        Foreground="Black" ColumnSizer="Star" Visibility="Visible"
                        AllowDraggingColumns="True" RowHeight="42" AllowSorting="True" AllowGrouping="True"   
                        FontSize="16" FontFamily="Calibri"  Tapped="AppointmentResultGrid_Tapped" Grid.RowSpan="2">
                        
                        <Grid1:SfDataGrid.Columns>
                        
                            <Grid1:GridUnBoundColumn Width="120" MappingName="Counter" HeaderText="Action" TextAlignment="Left" HorizontalHeaderContentAlignment="Left" AllowSorting="False">
                                <Grid1:GridTemplateColumn.HeaderStyle>
                                    <Style TargetType="Grid1:GridHeaderCellControl">
                                        <Setter Property="Background" Value="#1F6996" />
                                    </Style>
                                </Grid1:GridTemplateColumn.HeaderStyle>
                                <Grid1:GridUnBoundColumn.CellTemplate>
                                    <DataTemplate>
                                        <Button Width="120" HorizontalAlignment="Left" Margin="-15,-2"
                                    Height="45" BorderBrush="Black" BorderThickness="1" Background="#3E61B9" Foreground="White"
                                     CommandParameter="{Binding AppointmentID}" Tag="{Binding PractiotionerID}"
                                    Content="Cancel" Click="Button_Tapped" />
                                   
                                    
                                    </DataTemplate>


                                </Grid1:GridUnBoundColumn.CellTemplate>
                            </Grid1:GridUnBoundColumn>

                           <!--*************************************-->

                            <Grid1:GridTextColumn MappingName="CaseNumber" HeaderText="CASE NUMBER" IsHidden="True" />
                            <Grid1:GridTextColumn MappingName="PractiotionerID" HeaderText="PRACTITIONER" IsHidden="True" />
                        </Grid1:SfDataGrid.Columns>
                    </Grid1:SfDataGrid>

Button click C# code is as below

private async void Button_Tapped(object sender, RoutedEventArgs e)
        {
            try
            {
                    var bt = sender as Button;
                    var itm = bt.Command;

               }
            }
            catch (Exception ex)
            { }

        }


3 Replies

JG Jai Ganesh S Syncfusion Team August 31, 2015 12:17 PM UTC


Hi Muhammad,


Thank you for using Syncfusion Products.


You have already created one incident for the same query asked in this forum. We have updated the response for this queries in that incident. Could you please follow up that incident for further details.


Thank you,

Jai Ganesh S



NE neelam October 4, 2016 09:27 AM UTC

I have same requirement in xamarin sfdatagrid control.pls help


AN Ashok N Syncfusion Team October 5, 2016 10:43 AM UTC

Hi Muhammad, 

Query regarding Row index: 
We have checked your query and currently we do not have CellTemplate support for GridUnboundColumn. So we have loaded Button in GridTemplateColumn. As in the below code snippet you can get the RowIndex. 
private void button_Clicked(object sender, EventArgs e) 
{ 
   var _button = sender as Button; 
   var record = _button.BindingContext as OrderInfo; 
   var recordrowindex = viewModel.OrderInfoCollection.IndexOf(record); 
   var rowindex = sfGrid.ResolveToRowIndex(recordrowindex); 
} 
 

Query regarding CommandParameter and Tag: 
You can get the value for CommandParameter as in the below code example 
<sfgrid:GridTemplateColumn HeaderText="ShipCity" MappingName="ShipCity"> 
  <sfgrid:GridTemplateColumn.CellTemplate> 
    <DataTemplate> 
        <Button Clicked="button_Clicked" CommandParameter="{Binding  Mark,Source={StaticResource viewModel}}"  WidthRequest="120" Text="{Binding ShipCity}"/> 
    </DataTemplate> 
  </sfgrid:GridTemplateColumn.CellTemplate> 
</sfgrid:GridTemplateColumn> 
 
private void button_Clicked(object sender, EventArgs e) 
{ 
    var _button = sender as Button; 
    var commandparam = _button.CommandParameter.ToString(); 
} 
 

We have checked Tag property for button in Xamarin but Xamarin.Froms didn’t have Tag property.  

Query regarding Hidden columns :  
In Xamarin we didn’t have GridColumn.IsHidden property. But you can hide the column by setting GridColumn.Width as 0 and you can get the cell value for this column by using SfDataGrid.GetCellValue method. Please refer the below code example 
private void button_Clicked(object sender, EventArgs e) 
{ 
    var _button = sender as Button; 
    var record = _button.BindingContext as OrderInfo; 
    var recordrowindex = viewModel.OrderInfoCollection.IndexOf(record); 
    var rowindex = sfGrid.ResolveToRowIndex(recordrowindex); 
            
    // get hidden columns value 
    for (int i = 0; i < sfGrid.Columns.Count; i++) 
    { 
        if (sfGrid.Columns[i].Width == 0) 
        { 
            var mappingName = this.sfGrid.Columns[i].MappingName; 
            var data = this.sfGrid.GetCellValue(record, mappingName); 
        } 
    } 
} 
 


Regards, 
Ashok 


Loader.
Live Chat Icon For mobile
Up arrow icon