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

How to change the add row column contents from view model, or code behind

I'd like to know if it's possible to programmatically change the contents of the columns in the SfDataGrid add row from either it's DataContext (e.g. ViewModel) or from code behind. Thanks.


5 Replies 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team November 30, 2022 07:29 PM UTC

Hi Zeljko Lazic,

Your requirement to programmatically change the contents of the specific column in SfDataGrid can be achieved by using View.GetPropertyAccessProvider.SetValue method in SfDatGrid. Refer to the below code snippet,


private void btnChangeCellValueClicked(object sender, RoutedEventArgs e)

 {

     //Here update the cell value for specific column

     if (sfDataGrid.View != null)

     {

         //here pass the row index to modify the record in SfDataGrid

         var recordIndex = sfDataGrid.ResolveToRecordIndex(2);

 

         if (recordIndex < 0)

             return;

 

         object record = null;

 

         if (sfDataGrid.View.TopLevelGroup != null)

         {

             var displayElement = sfDataGrid.View.TopLevelGroup.DisplayElements[recordIndex];

 

             if (displayElement == null)

                 return;

             if (displayElement is RecordEntry)

                 record = ((RecordEntry)displayElement).Data;

         }

         else

             record = sfDataGrid.View.Records[recordIndex].Data;

 

         //set the value for particular cell in the record

         if (record != null)

             //Here passing Column mapping name to change the value for country column

             sfDataGrid.View.GetPropertyAccessProvider().SetValue(record, "Country", "ModifiedValue");

     }

 

 }


KB Link: https://www.syncfusion.com/kb/6655/how-to-change-the-cell-value-of-selected-cells-in-wpf-datagrid-sfdatagrid

https://www.syncfusion.com/kb/6384/how-to-read-cell-values-from-selecteditems

https://www.syncfusion.com/kb/2446/how-to-get-the-current-cell-value-when-you-click-the-cell


Your requirement to add row from code can be achieved by customizing as show below,

private void btnAddRowClicked(object sender, RoutedEventArgs e)

{

    //here get the collection from DataContext

    var dataContext = (this.DataContext as ViewModel).Orders;

 

    //here add the row data to the collection

    dataContext.Add(new OrderInfo(1011, "Christina Berglund", "France", "BERGS", 20300));

    dataContext.Add(new OrderInfo(1012, "Hanna Moos", "France", "BLAUS", 50700));

    dataContext.Add(new OrderInfo(1013, "Frederique Citeaux", "France", "BLONP", 80100));

    dataContext.Add(new OrderInfo(1014, "Martin Sommer", "France", "BOLID", 35000));

    dataContext.Add(new OrderInfo(1015, "Laurence Lebihan", "Italy", "BONAP", 20030));

}

 


UG Link: https://help.syncfusion.com/wpf/datagrid/data-binding

Find the sample in the attachment.

Regards,

Vijayarasan S


If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.


Attachment: Sample_419ad125.zip


ZL Zeljko Lazic November 30, 2022 08:10 PM UTC

Hello Vijayarasan,

My apologies, my question wasn't detailed enough. The SfDataGrid has a built in add row which is enabled in the xaml code AddNewRowPosition="Top".

Now in my case there are certain columns that the user doesn't manually edit in the SfDataGrid. They need to select from a separate control (SfTreeView). The other columns they can manually edit.


So in your example could you update the code and enable the add row

AddNewRowPosition="Top"

ItemsSource="{Binding Orders}"

AutoGenerateColumns="False">



Make the add row Order ID cell readonly (can you make add row cells readonly?), and when the user clicks Change Cell Value it changes the Order ID from the code behind or the viewmodel. All the other columns in the add row remain editable by the user.




VS Vijayarasan Sivanandham Syncfusion Team December 1, 2022 03:15 PM UTC

Zeljko Lazic,

Find the responses to your queries below.

Queries

Responses

 

Make the add row Order ID cell readonly (can you make add row cells readonly?),

 


Your requirement to make the AddNewRow Order ID cell read-only in SfDataGrid can be achieved by customizing the CurrentCellBeginEdit event. Refer to the below code snippet,

//Event subscription

sfDataGrid.CurrentCellBeginEdit += OnCurrentCellBeginEdit;

 

//Event customization

private void OnCurrentCellBeginEdit(object sender, CurrentCellBeginEditEventArgs e)

{

    var dataGrid = sender as SfDataGrid;

    //Here handle the CurrentCellBeginEdit event to make AddNewRow Order ID cell as readonly

    if (dataGrid.IsAddNewIndex(e.RowColumnIndex.RowIndex) && e.Column != null && e.Column.MappingName == "OrderID")

        e.Cancel = true;

}


UG Link:
https://help.syncfusion.com/wpf/datagrid/editing

 
https://help.syncfusion.com/wpf/datagrid/editing#canceledit

https://help.syncfusion.com/wpf/datagrid/editing#currentcellbeginedit-event

 

 

when the user clicks Change Cell Value it changes the Order ID from the code behind or the viewmodel.

 

 

Your requirement to change the OrderID call value while adding a new row in SfDataGrid can be achieved by setting the value in the CurrentAddItem property in SfDataGrid.View. Refer to the below code snippet,

private void btnChangeOrderIDClicked(object sender, RoutedEventArgs e)

{

    //Here update the cell value when user click the button

   

    if (sfDataGrid.View != null)

    {  

        //Here get the currently adding item

        var record = sfDataGrid.View.CurrentAddItem;

 

        //set the value for particular cell in the record

        if (record != null)

            //Here passing Column mapping name to change the value for OrderID column

            sfDataGrid.View.GetPropertyAccessProvider().SetValue(record, "OrderID", 10);

    }

}      

 

Note: CurrentAddItem property is only applicable when adding a new row in SfDataGrid

UG Link: https://help.syncfusion.com/wpf/datagrid/data-manipulation#add-new-rows

KB Link: https://www.syncfusion.com/kb/10814/how-to-allow-user-to-add-a-new-row-in-wpf-datagrid-sfdatagrid

 


Find the modified sample in the attachment.

If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.


Attachment: ModifiedSample_fa89007a.zip

Marked as answer

ZL Zeljko Lazic December 2, 2022 06:47 PM UTC

Regarding your updated sample. If you press the button right after the application starts.

The value of "record" is null, and sfDataGrid.View.GetPropertyAccessProvider().SetValue(record, "OrderID", 10); never gets called.

if (sfDataGrid.View != null)

{

//Here get the currently adding item

var record = sfDataGrid.View.CurrentAddItem;


//set the value for particular cell in the record

if (record != null)

//Here passing Column mapping name to change the value for OrderID column

sfDataGrid.View.GetPropertyAccessProvider().SetValue(record, "OrderID", 10);

}


In my production code i'm using an attached property since i'm not using code behind, and i have the same problem that sfDataGrid.View.CurrentAddItem returns null.



VS Vijayarasan Sivanandham Syncfusion Team December 5, 2022 03:41 PM UTC

Zeljko Lazic,

We have checked the reported scenario with the provided details. You are trying to access the CurrentAddItem before the AddNewRow gets initialized. That’s why the reported problem occurs. This is the default behavior of SfDataGrid AddNewRow. You can resolve this by setting the default value to the constructor itself or else you can define the default value by using the AddNewRowInitiating event. Refer to the below code snippet,

Code Snippet related to AddNewRowInitiating event:

//Event subscription

sfDataGrid.AddNewRowInitiating += OnAddNewRowInitiating;

 

//Event customization

private void OnAddNewRowInitiating(object sender, AddNewRowInitiatingEventArgs e)

{

     //Here assign the Default value when initiating the AddNewRow

     var data = e.NewObject as OrderInfo;

     data.OrderID = 101;

}

 


UG Link: https://help.syncfusion.com/wpf/datagrid/data-manipulation#initializing-default-values-for-addnewrow

Add the default value in the constructor:

//Constructor

 public OrderInfo()

 {

     //Here define the Value based on your scenario

     this.OrderID = 1001;

 }


If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.


Loader.
Live Chat Icon For mobile
Up arrow icon