Reordering columns programmatically with MVVM

Hello,

I'd like to know if it would be possible to change the order of the columns in a SfDataGrid in a Mvvm-way. I didn't find nothing for this in the documentation, and I can't figure out how could I do it.

So, if it's possible, what would be the best way to achieve this?


Thanks.

7 Replies 1 reply marked as answer

SS Sivaraman Sivagurunathan Syncfusion Team April 1, 2020 07:50 AM UTC

Hi Paco, 

Thanks for using Syncfusion support. 

We have checked your query. if you want to change the order of the column when grid loading you need se the AutoGenerateColumns as False and create the column manually. Or if you want to change the order of the column use the AllowDraggingColumn property to change the columns order. We have prepared the sample based on you requirement and attach for your reference. You can download the same from the below link. 


<sfgrid:SfDataGrid x:Name="dataGrid" 
                ItemsSource="{Binding OrdersInfo}"  
                AutoGenerateColumns="False" 
                AllowDraggingColumn="True" 
                ColumnSizer="Star"> 
    <sfgrid:SfDataGrid.Columns > 
        <sfgrid:GridTextColumn MappingName="OrderID" /> 
        <sfgrid:GridTextColumn MappingName="EmployeeID" /> 
        <sfgrid:GridTextColumn MappingName="FirstName" /> 
        <sfgrid:GridTextColumn MappingName="LastName" /> 
        <sfgrid:GridDateTimeColumn MappingName="ShippingDate"/> 
    </sfgrid:SfDataGrid.Columns> 
</sfgrid:SfDataGrid> 





Regards, 
Sivaraman S 



AG Aicom Global Software replied to Sivaraman Sivagurunathan April 1, 2020 09:02 AM UTC

Hi Paco, 

Thanks for using Syncfusion support. 

We have checked your query. if you want to change the order of the column when grid loading you need se the AutoGenerateColumns as False and create the column manually. Or if you want to change the order of the column use the AllowDraggingColumn property to change the columns order. We have prepared the sample based on you requirement and attach for your reference. You can download the same from the below link. 


<sfgrid:SfDataGrid x:Name="dataGrid" 
                ItemsSource="{Binding OrdersInfo}"  
                AutoGenerateColumns="False" 
                AllowDraggingColumn="True" 
                ColumnSizer="Star"> 
    <sfgrid:SfDataGrid.Columns > 
        <sfgrid:GridTextColumn MappingName="OrderID" /> 
        <sfgrid:GridTextColumn MappingName="EmployeeID" /> 
        <sfgrid:GridTextColumn MappingName="FirstName" /> 
        <sfgrid:GridTextColumn MappingName="LastName" /> 
        <sfgrid:GridDateTimeColumn MappingName="ShippingDate"/> 
    sfgrid:SfDataGrid.Columns> 
sfgrid:SfDataGrid> 





Regards, 
Sivaraman S 


Thanks you for your help.


Nevertheless, I'm trying to achieve another thing; I want to change the order of the columns programmatically, without the user having to drag the colums by himself.


For example:

Column1 | Column2

    test1          test2

--> When the user click a Button, I want to change the columns like this from the code, without redrag (MVVM-way, preferably).

Column2 | Column1

     test2          test1



Is there any way to achieve this?


SS Sivaraman Sivagurunathan Syncfusion Team April 2, 2020 07:26 AM UTC

Hi Paco, 

Thanks for your update. 

We have checked your query. you can achieve your requirement by setting the FlowDirection as RightToLeft at run time. we have prepared the sample for you requirement and attached for your reference. You can download the same from the below link. 

Code Example: 

dataGrid.FlowDirection = FlowDirection.RightToLeft; 


 

 


Regards, 
Sivaraman S 



AG Aicom Global Software replied to Sivaraman Sivagurunathan April 2, 2020 08:21 AM UTC

Hi Paco, 

Thanks for your update. 

We have checked your query. you can achieve your requirement by setting the FlowDirection as RightToLeft at run time. we have prepared the sample for you requirement and attached for your reference. You can download the same from the below link. 

Code Example: 

dataGrid.FlowDirection = FlowDirection.RightToLeft; 


 

 


Regards, 
Sivaraman S 


Hello,

That won't work for me, as I have more than two columns and I just want to change the order of two of them. That way, it will change the order of all the columns.


The example I posted on my previous reply just was to make it easier to understand, but imagine if I had a Column3 and I don't want to change the order, just alter order of Column1 or Column2.


Thanks for your help.


SS Sivaraman Sivagurunathan Syncfusion Team April 2, 2020 10:18 AM UTC

Hi Paco, 

Thanks for your update. 

We have checked your query. you can change the order particular column using  Move method. You need to pass the column index to change the order of the column. We have prepared the sample for your requirement and attached for your reference. You can download the same from the below link. 

Code Example: 

private void Button_Clicked(object sender, EventArgs e) 
{ 
    dataGrid.Columns.Move(1, 2); 
} 


 

 


Regards, 
Sivaraman S 


Marked as answer

AG Aicom Global Software April 2, 2020 10:22 AM UTC

That's exactly what I want. Didn't see it in the documentation.


Thank you so much for your help.


SS Sivaraman Sivagurunathan Syncfusion Team April 3, 2020 08:47 AM UTC

Hi Paco, 
 
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      
 


Loader.
Up arrow icon