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. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

DataGrid Selected item event in cell?

Thread ID:

Created:

Updated:

Platform:

Replies:

128250 Jan 10,2017 09:25 PM UTC Jan 17,2017 06:19 PM UTC Xamarin.Forms 5
loading
Tags: SfDataGrid
Russ Fustino
Asked On January 10, 2017 09:25 PM UTC

Is there a SelectedItem event available at the cell level? I see one for the row, but not a cell on the row. If so, is there an example you can point me to?

Thanks!

Ashok N [Syncfusion]
Replied On January 11, 2017 06:13 PM UTC

Hi Russ,  
 
Thanks for contacting Syncfusion support.  
 
We don’t have event for handle the CellSelection. You can get the particular cell using this RowColumnIndex by handling the below events in SfDataGrid,  
 
sfGrid.GridTapped += SfGrid_GridTapped;  
sfGrid.GridDoubleTapped += SfGrid_GridDoubleTapped;  
sfGrid.GridLongPressed += SfGrid_GridLongPressed;  
  
private void SfGrid_GridLongPressed(object sender, GridLongPressedEventsArgs e)  
{  
    int rowindex = e.RowColumnindex.RowIndex;  
    int columnindex = e.RowColumnindex.ColumnIndex;  
}  
  
private void SfGrid_GridDoubleTapped(object sender, GridDoubleTappedEventsArgs e)  
{  
    int rowindex = e.RowColumnindex.RowIndex;  
    int columnindex = e.RowColumnindex.ColumnIndex;  
}  
  
private void SfGrid_GridTapped(object sender, GridTappedEventsArgs e)  
{  
    int rowindex = e.RowColumnindex.RowIndex;  
    int columnindex = e.RowColumnindex.ColumnIndex;  
}  
 
Regards,  
Ashok  


Russ Fustino
Replied On January 12, 2017 08:07 PM UTC

Thanks. This is very helpful... It works in my codebehind. If I wanted to bind to the tapped event in my XAML, what would the Tapped event handler look like in my ViewModel code?

XAML

                    GridTapped="{Binding objTappedDrug, Mode=TwoWay}"

I have one for SelectedItem from the grid in my ViewModel that looks like this, but I can't seem to figure out how it would be modified for GridTapped.


        Drug _selectedDrug = null;
        public Drug objSelectedDrug
        {
            get
            {
                return _selectedDrug;
            }
            set
            {
                _selectedDrug = value;
                if (value == null)
                {
                    return; //ItemSelected is called on deselection, which results in SelectedItem being set to null
                }
                Drug tappedDrug = (Drug)value;

                this.NavigationCommand.Execute(new TreatmentDetailPage(tappedDrug));
                // value = null;
            }
        }

This is what I have for the code on the attempted Tapped Grid event but I am getting an Error , Unable to cast object of type 'Xamarin.Forms.Xaml.ElementNode' to type 'Xamarin.Forms.Xaml.ValueNode'.   

        SfDataGrid _tappedDrug = null;
        public SfDataGrid objTappedDrug
        {
            get {
                return _tappedDrug;
                }
            set
            {
                _tappedDrug = value;
                if (value == null)
                {
                    return; //ItemSelected is called on deselection, which results in SelectedItem being set to null
                }
            SfDataGrid tappedDrug = value;
                // do something with the rows and columns here
                //int rowindex = e.RowColumnIndex.RowIndex;
                //int columnindex = e.RowColumnIndex.ColumnIndex;
                //var rowdata = e.RowData as DrugList;
                //await Bind5Cell(columnindex, rowdata);
            }
        }


Harikrishnan N [Syncfusion]
Replied On January 13, 2017 11:10 AM UTC

Hi Russ,

We have checked your query. This requirement can be achieved by using Behaviors in Xamarin.Forms. The Xamarin Forms Behaviors allows you to bind commands for events of a control and you can download Xamarin Forms Behaviors nuget from the below link. 

Nuget Link: 
https://www.nuget.org/packages/Xamarin.Forms.Behaviors/ 

There are two approaches to achieve your requirement.

Approach - 1: Simplify event to command

Please refer the below code example in which we have used the Xamarin Forms Behaviors to bind the GridTapped event of SfDataGrid with the GridTappedCommand property in the ViewModel.

The below code illustrates binding a command for GridTapped event in a SfDataGrid from XAML.
 

 

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage x:Class="DataGridDemo.DataGrid" 
             xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             xmlns:b="clr-namespace:Xamarin.Behaviors;assembly=Xamarin.Behaviors" 
             xmlns:local="clr-namespace:DataGridDemo" 
             xmlns:sfgrid="clr-namespace:Syncfusion.SfDataGrid.XForms;assembly=Syncfusion.SfDataGrid.XForms"> 
 
    <ContentPage.BindingContext> 
        <local:ViewModel x:Name="viewModel" /> 
    </ContentPage.BindingContext> 
 
    <ContentPage.Content> 
        <sfgrid:SfDataGrid x:Name="dataGrid" ColumnSizer="Star" ItemsSource="{Binding OrdersInfo}"> 
          <b:Interaction.Behaviors> 
            <b:BehaviorCollection> 
              <b:EventToCommand EventName="GridTapped" Command="{Binding GridTappedCommand}" /> 
            </b:BehaviorCollection> 
          </b:Interaction.Behaviors> 
        </sfgrid:SfDataGrid> 
    </ContentPage.Content> 
</ContentPage> 
 
The below code example illustrates creating a command with a call back for the GridTapped event.
 

  


public class ViewModel 
{ 
    public ViewModel() 
    { 
        gridTappedCommand = new Command(SfGrid_GridTapped); 
        SetRowstoGenerate(50); 
    } 
 
    private ObservableCollection<OrderInfo> ordersInfo; 
    public ObservableCollection<OrderInfo> OrdersInfo 
    { 
        get { return ordersInfo; } 
        set { this.ordersInfo = value; } 
    } 
 
    private Command gridTappedCommand; 
    public Command GridTappedCommand 
    { 
        get { return gridTappedCommand; } 
        set { gridTappedCommand = value; } 
    } 
 
    public void SetRowstoGenerate(int count) 
    { 
        OrderInfoRepository order = new OrderInfoRepository(); 
        ordersInfo = order.GetOrderDetails(count); 
    } 
 
    private void SfGrid_GridTapped() 
    { 
 
    } 
} 
 

Approach - 2: Writing behavior for SfDataGrid

Please refer the below code example in which we have written Behavior for SfDataGrid to implement the GridTapped event of SfDataGrid in the ViewModel.

The below code illustrates implementing the GridTapped event in ViewModel by writing Behavior for SfDataGrid.

 

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage x:Class="DataGridDemo.DataGrid" 
             xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             xmlns:b="clr-namespace:Xamarin.Behaviors;assembly=Xamarin.Behaviors" 
             xmlns:local="clr-namespace:DataGridDemo" 
             xmlns:sfgrid="clr-namespace:Syncfusion.SfDataGrid.XForms;assembly=Syncfusion.SfDataGrid.XForms"> 
 
    <ContentPage.BindingContext> 
        <local:ViewModel x:Name="viewModel" /> 
    </ContentPage.BindingContext> 
 
    <ContentPage.Content> 
        <sfgrid:SfDataGrid x:Name="dataGrid" 
                           AutoGenerateColumns="False" 
                           ColumnSizer="Star" 
                           ItemsSource="{Binding OrdersInfo}"> 
           
          <sfgrid:SfDataGrid.Behaviors> 
            <local:SfDataGridBehavior Parameter="{x:Reference Name=viewModel}" /> 
          </sfgrid:SfDataGrid.Behaviors> 
        </sfgrid:SfDataGrid> 
    </ContentPage.Content> 
</ContentPage> 
  

The below code illustrates writing behavior for SfDataGrid to implement the GridTapped event in ViewModel.
 

 

//Behavior class implementation 
public class SfDataGridBehavior : Behavior<SfDataGrid> 
{ 
    public static readonly BindableProperty ParameterProperty = 
            BindableProperty.Create("Parameter", typeof(ViewModel), typeof(SfDataGridBehavior), null); 
 
    public ViewModel Parameter 
    { 
        get { return (ViewModel)GetValue(ParameterProperty); } 
        set { SetValue(ParameterProperty, value); } 
    } 
 
    public SfDataGrid AssociatedObject { get; private set; } 
 
    protected override void OnAttachedTo(SfDataGrid bindable) 
    { 
        base.OnAttachedTo(bindable); 
        AssociatedObject = bindable; 
        bindable.GridTapped += Parameter.SfGrid_GridTapped; 
    } 
 
    protected override void OnDetachingFrom(SfDataGrid bindable) 
    { 
        base.OnDetachingFrom(bindable); 
        bindable.GridTapped -= Parameter.SfGrid_GridTapped; 
        AssociatedObject = null; 
    } 
} 
 
//ViewModel implementation 
public class ViewModel 
{ 
    public ViewModel() 
    { 
        SetRowstoGenerate(50); 
    } 
 
    private ObservableCollection<OrderInfo> ordersInfo; 
    public ObservableCollection<OrderInfo> OrdersInfo 
    { 
        get { return ordersInfo; } 
        set { this.ordersInfo = value; } 
    } 
 
    public void SetRowstoGenerate(int count) 
    { 
        OrderInfoRepository order = new OrderInfoRepository(); 
        ordersInfo = order.GetOrderDetails(count); 
    } 
 
    internal void SfGrid_GridTapped(object sender, GridTappedEventsArgs e) 
    { 
        int rowindex = e.RowColumnindex.RowIndex; 
        int columnindex = e.RowColumnindex.ColumnIndex; 
    } 
}
 

 


We have prepared a sample to adapt both the approaches for your reference and you can download the same from the below link.

Sample link: http://www.syncfusion.com/downloads/support/forum/128250/ze/DataGridDemo745397101

Regards,
Harikrishnan  




Russ Fustino
Replied On January 16, 2017 07:00 AM UTC

Thanks! I am loving the sample project link. Very nice support. Is there a place for feature requests? I can use a Grid format option on the ListView with a span count property. That would save all of the coding above.

Ashok N [Syncfusion]
Replied On January 17, 2017 06:19 PM UTC

Hi Russ, 
 
Thanks for your update. We have checked your query but we can’t able to understand your requirement clearly. Could you please share more details regarding your requirement that would be more helpful for us to serve you better. Also please confirm are you using SfDataGrid or ListView in your application? 
 
Regards, 
Ashok 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;