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.
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.
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.
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?),
|
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,
Note: CurrentAddItem property is only applicable when adding a new row
in 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.
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.
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.