Hi,
<Syncfusion:SfDataGrid ItemsSource="{Binding}"
AllowEditing="True"
AddNewRowPosition="Bottom"
RowValidating="sfDataGridTest_RowValidating"
LostKeyboardFocus="sfDataGridTest_LostKeyboardFocus">
<Syncfusion:SfDataGrid.Columns>
<Syncfusion:GridTextColumn MappingName="Name"/>
<Syncfusion:GridTemplateColumn HeaderText="Extend Setting">
<Syncfusion:GridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Extend Setting" Click="Button_Click"/>
</DataTemplate>
</Syncfusion:GridTemplateColumn.CellTemplate>
</Syncfusion:GridTemplateColumn>
</Syncfusion:SfDataGrid.Columns>
</Syncfusion:SfDataGrid>
public partial class MainWindow : Window
{
private List<ViewModel> ViewModels { get; } = new List<ViewModel>();
public MainWindow()
{
DataContext = ViewModels;
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Window window = new Window();
window.ShowDialog();
}
private void sfDataGridTest_RowValidating(object sender, RowValidatingEventArgs e)
{
//checking user input and then commit or cancel
MessageBox.Show("RowValidating");
}
private void sfDataGridTest_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
if (!(sender is SfDataGrid grid)) return;
if (!grid.IsKeyboardFocusWithin)
{
if (grid.View != null && grid.View.CurrentEditItem != null && !grid.View.IsAddingNew)
{
// it commit the modified value in the grid.
grid.View.CommitEdit();
}
if (grid.View != null && grid.View.IsAddingNew)
{
// it commit the entered value in the grid.
if (grid.SelectionController.CurrentCellManager.CurrentCell.IsEditing)
grid.SelectionController.CurrentCellManager.EndEdit(true);
var addNewRowController = grid.GetAddNewRowController();
addNewRowController.CommitAddNew();
}
}
}
}
public class ViewModel
{
public string Name { get; set; }
//other complex properties
}
Hi Wssyncfusion,
Please find answer for your queries below
|
Queries |
Solutions |
|
|
Then i click the "extend setting" button while adding the next new row,the row is commited,but RowValidating event not fired.
|
Your requirement to trigger
the RowValidating event in SfDataGrid can be achieved by setting focus to a particular element inside Template when the cell gets activated or edited. Please
refer to the below code snippet,
UG
Link: https://help.syncfusion.com/wpf/datagrid/column-types#keyboard-interaction-for-uielement-loaded-inside-celltemplate
|
|
|
and add the third row,the program is crashed |
|
Please find the sample in the attachment and let us know if you have any
concerns in this.
Regards,
Vijayarasan S
Hi,
Thansk for your reply,it solved almost all my problem,only the exception can still be raised following the steps :
1.Add a button or other control to the sample above
2.Click the CustomerName column of the new row adding a new record
3.Click the "Extend Setting" button once, RowValidating event fired and button click event doesn't
4.Click the new button or other control, LostKeyboardFocus event fired and CommitAddNew method excuted
5. Click the CustomerName column of the new row again,the exception raised
The reported problem occurs due to the new row not being properly committed in SfDataGrid.
You can overcome this problem by committing the new row in a button click. Please refer to the below code snippet,
I also add a handle to commit new row on new button control, but the exception still threw
private void Button_Click_1(object sender, RoutedEventArgs e)
{
if (sfDataGrid.View != null && sfDataGrid.View.IsAddingNew)
{
// it commit the entered value in the sfDataGrid.
if (sfDataGrid.SelectionController.CurrentCellManager.CurrentCell.IsEditing)
sfDataGrid.SelectionController.CurrentCellManager.EndEdit(true);
var addNewRowController = sfDataGrid.GetAddNewRowController();
addNewRowController.CommitAddNew();
Console.WriteLine("Button_Click_1 CommitAddNew");
}
}
Thanks
Hi Wssyncfusion,
We have modified the sample based on the provided information. The reported issue is “Invalid
exception occurs while clicking the Extend Setting button” and is unable to
replicate the issue from our end. It is working fine as expected. Please find
the tested sample and video in the attachment.
If we misunderstood your requirement, please provide more information related to your query?
Kindly revert to us with the above requested details. It
will be more helpful for us to check the possibilities to resolve the reported
problem.
Regards,
Vijayarasan S
Thanks for replying,
The sample in video work perfectly,but when i run the code,the exception occurs again.
This is my enviroment:
Windows 10 21H2 119044.1415
Microsoft Visual Studio Community 2022 (64-bit) - Current Version 17.1.5
And i also took a video
about the
replication procedure
Thanks
Hi Wssyncfusion,
The reported problem occurs due to trying to edit another cell before committing
the previous edited cell’s value. You can resolve the reported problem by committing
the previous edited cell’s value like below mentioned code snippet,
|
private void sfDataGridTest_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) { if (!(sender is SfDataGrid grid)) return; if (!grid.IsKeyboardFocusWithin) { if (grid.View != null && grid.View.CurrentEditItem != null && !grid.View.IsAddingNew ) { // it commit the modified value in the grid. grid.View.CommitEdit();
} if (grid.View != null && grid.View.IsAddingNew) { // it commit the entered value in the grid. if (grid.SelectionController.CurrentCellManager.CurrentCell.IsEditing) grid.SelectionController.CurrentCellManager.EndEdit(true);
//need to check the item is still editing or not //if items is editing need to commit the value if (grid.View.IsEditingItem) grid.View.CommitEdit();
var addNewRowController = grid.GetAddNewRowController(); addNewRowController.CommitAddNew();
} } } |
Please find the modified sample in the attachment and let us know if you have
any concerns in this.
Regards,
Vijayarasan S