Edit text/numbers in SfDataGrid in MVVM approach

How to handle editing in DataGrid to know when editing of a given cell is finished (then I will be able to save the change in the database). It must comply with the MVVM template.

11 Replies 1 reply marked as answer

KK Karthikraja Kalaimani Syncfusion Team December 24, 2020 06:13 AM UTC

Hi Marcin,

 
Your requirement can be achieved by using the CurrentCellEditEvent. Please refer to the below code snippets to achieve your requirement based on MVVM pattern.

Code snippets :

 
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             xmlns:local="clr-namespace:DataGridDemo" 
             xmlns:sfPager="clr-namespace:Syncfusion.SfDataGrid.XForms.DataPager;assembly=Syncfusion.SfDataGrid.XForms" 
             xmlns:sfgrid="clr-namespace:Syncfusion.SfDataGrid.XForms;assembly=Syncfusion.SfDataGrid.XForms" 
             x:Class="DataGridDemo.MainPage" > 
 
    <ContentPage.BindingContext> 
        <local:ViewModel x:Name="viewModel"/> 
    </ContentPage.BindingContext> 
    <ContentPage.Content> 
        <StackLayout x:Name="stack" Orientation="Vertical"> 
           
            <!--<Button Text="ClickToExport" Clicked="Button_Clicked"></Button>--> 
            <sfgrid:SfDataGrid x:Name="mainDataGrid" 
                         Grid.Row="1" 
                         AutoGenerateColumns="False" 
                         ColumnSizer="Star" 
                         AllowEditing="True"  
                         SelectionMode="Single" 
                         NavigationMode="Cell" 
                         ItemsSource="{Binding OrdersInfo}"   
                         > 
                <sfgrid:SfDataGrid.Behaviors> 
                    <local:DataGridBehavior></local:DataGridBehavior> 
                </sfgrid:SfDataGrid.Behaviors> 
                <sfgrid:SfDataGrid.Columns> 
                    <sfgrid:GridTextColumn HeaderText="Column 2" Width="100" MappingName="OrderID"></sfgrid:GridTextColumn> 
                    <sfgrid:GridTextColumn Width="200"  MappingName="EmployeeID"></sfgrid:GridTextColumn> 
                    <sfgrid:GridTemplateColumn  Width="50" MappingName="EmployeeID"> 
                        <sfgrid:GridTemplateColumn.CellTemplate> 
                            <DataTemplate> 
                                <Label Text="{Binding EmployeeID}" FontFamily="fontawesome-webfont"> 
                                </Label> 
                            </DataTemplate> 
                        </sfgrid:GridTemplateColumn.CellTemplate> 
                    </sfgrid:GridTemplateColumn> 
                </sfgrid:SfDataGrid.Columns> 
            </sfgrid:SfDataGrid> 
        </StackLayout> 
    </ContentPage.Content> 
</ContentPage>
….
public
class DataGridBehavior : Behavior<SfDataGrid> 
    { 
        public DataGridBehavior() 
        { 
 
        } 
 
        protected override void OnAttachedTo(SfDataGrid bindable) 
        { 
            bindable.CurrentCellEndEdit += Bindable_CurrentCellEndEdit; 
        } 
 
        private void Bindable_CurrentCellEndEdit(object sender, GridCurrentCellEndEditEventArgs e) 
        { 
             
        } 
    } 

Regards,
Karthik Raja
 



MA Marcin December 24, 2020 10:06 AM UTC

Thank You for this code snippet.
I place DataGridBehavior class in ViewModel and it works, but it's possible totally isolate ViewModel form View?
It is possible not to use reference in ViewModel to any classes from View? (class DataGridBehavior uses SfDataGrid class)



KK Karthikraja Kalaimani Syncfusion Team December 28, 2020 06:06 AM UTC

Hi Marcin,

Thanks for the update. We don’t understand what your saying, can you please explain your requirement in detail ?

Regards,
Karthik Raja



MA Marcin December 28, 2020 07:38 PM UTC

In my MVVM pattern it looks like this:
In View: I have placed sfDataGrid with binding to the data source in ViewModel.
In ViewModel: it's defined data source and whole logic.
Your snippet works. I defined a separate class but I'm having trouble referencing this new class from the ViewModel.

Problem:
1) I would like to know (in class from ViewModel) when the user finishes editing a cell in the grid to do commit in the data source.
2) Maybe there is an easier way to save the edited data in the grid immediately?




KK Karthikraja Kalaimani Syncfusion Team December 29, 2020 05:42 AM UTC

 

Hi Marcin,

Currently, we don’t have command support for the CurrentCellEndEdit event. However, You can get the ViewModel properties from the DataGridBehaviorClass by casting the SfDataGrid.BindingContext as ViewModel.

Regards,
Karthik Raja



MA Marcin December 29, 2020 06:57 AM UTC

Thanks for the hint. 
Could you please give me an example of how to do this?


KK Karthikraja Kalaimani Syncfusion Team December 30, 2020 10:55 AM UTC

Hi Marcin,

Please refer to the below code snippet to access the ViewModel properties on DataGridBehavior class.

Code snippet :

 
public class DataGridBehavior : Behavior<SfDataGrid> 
    { 
        public DataGridBehavior() 
        { 
 
        } 
 
        protected override void OnAttachedTo(SfDataGrid bindable) 
        { 
            bindable.CurrentCellEndEdit += Bindable_CurrentCellEndEdit; 
        } 
 
        private void Bindable_CurrentCellEndEdit(object sender, GridCurrentCellEndEditEventArgs e) 
        { 
            var a = ((sender as SfDataGrid).BindingContext as ViewModel).OrdersInfo; 
        } 
    } 

Regards,
Karthik Raja 


Marked as answer

MA Marcin December 31, 2020 04:38 PM UTC

Thank you very much for help. 
The solution works great :)


PK Pradeep Kumar Balakrishnan Syncfusion Team January 4, 2021 05:49 AM UTC

Hi Marcin, 
 
Thank you for the update. We are glad that the given solution meets your requirement. Please get in touch with us if you would require any further assistance in future. 
 
Regards, 
Pradeep Kumar B 



HS Harsh Shah January 22, 2021 05:39 AM UTC

Hi, refer to this tutorial it provides information about working with  Datagrid (with MVVM) may this helps you.https://help.syncfusion.com/xamarin/datagrid/mvvm  


PK Pradeep Kumar Balakrishnan Syncfusion Team January 25, 2021 03:08 AM UTC

Hi Harsh Shah, 
 
Thank you for the update and your suggestion, 
 
Yes, provided user guide documentation link is helpful to user to use SfDataGrid in MVVM. 
 
Regards, 
Pradeep Kumar B 


Loader.
Up arrow icon