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.
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Overriding GridAddNewRowController to allow AddNewRow to be positioned at any row

Thread ID:

Created:

Updated:

Platform:

Replies:

130144 Apr 25,2017 05:07 AM UTC Apr 29,2017 05:55 AM UTC WPF 5
loading
Tags: SfDataGrid
Sam Chan
Asked On April 25, 2017 05:07 AM UTC

Hi,

I have a requirement to insert a new row anywhere in a SfDataGrid. However, SfDataGrid.AddNewRowPosition only supports a position at the top or the bottom. Is it a lot of effort to override GridAddNewRowController such that the AddNewRow can be positioned at any row and all the associated events and methods such as AddNewRowInitiating(), CommitAddNew(), etc. will continue to work?

I don't need an AddNewRow to be visible when the user is not editing. I just want to be able to insert an AddNewRow anywhere in a grid and put it in edit mode immediately.

Regards,

Sam

Muthukumar Kalyanasundaram [Syncfusion]
Replied On April 27, 2017 01:41 AM UTC

Hi Sam, 

Thank you for contacting Syncfusion support. 

We have checked your query. In SfDataGrid.AddNewRowPosition only support to insert the position at top or bottom. If you want to insert the new row at any position, you can use the underlying collection to insert the new row.  For your reference, we have attached the sample in the below location. In the sample, we have added the new row at any position and focus the edit mode in first column. Could you please refer to it. 

Code Snippet: 
 
private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    var index = Convert.ToInt16(txtBox.Text); 
    var view = (this.datagrid.DataContext as ViewModel).GDCSource; 
    var item = new Model(); 
    view.Insert(index, item); 
    this.datagrid.SelectedItem = item; 
    var rowIndex = this.datagrid.ResolveToRowIndex(index); 
    this.datagrid.MoveCurrentCell(new RowColumnIndex(rowIndex, 0), true); 
    this.datagrid.SelectionController.CurrentCellManager.BeginEdit(); 
 
} 


Please let us know if you have any query. 

Regards, 
Muthukumar K  


Sam Chan
Replied On April 27, 2017 09:05 AM UTC

Hi Muthukumar,

Thank you for providing the solution. I can adapt it for my requirement.

I noticed that despite calling this.datagrid.SelectionController.CurrentCellManager.BeginEdit(), datagrid is not put into edit mode as the CurrentCellBeginEdit handler is not called if I added the handler. I can workaround it by calling BeginEdit() in a Dispatcher.BeginInvoke(). BeginEdit() would work correctly if the row is not newly inserted. Is there a better way to put a newly inserted row into edit mode than using the Dispatcher?

Regards,

Sam

Muthukumar Kalyanasundaram [Syncfusion]
Replied On April 28, 2017 10:45 AM UTC

Hi Sam, 
 
As you mentioned, we have to calling BeginEdit inside Dispatcher to meet your needs. Please find code snippet and sample below, 

Code Snippet: 

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    var index = Convert.ToInt16(txtBox.Text); 
    var view = (this.datagrid.DataContext as ViewModel).GDCSource; 
    var item = new Model(); 
    view.Insert(index, item); 
    this.datagrid.SelectedItem = item; 
    var rowIndex = this.datagrid.ResolveToRowIndex(index); 
    this.datagrid.MoveCurrentCell(new RowColumnIndex(rowIndex, 0), true); 
    this.Dispatcher.BeginInvoke(new Action(() => 
    {                 
        this.datagrid.SelectionController.CurrentCellManager.BeginEdit(); 
    }), System.Windows.Threading.DispatcherPriority.ApplicationIdle); 
} 



There are no other ways. As the BeginEdit should get called after row insertion. 
 
Please let us know if you have any query. 
 
Regards, 
Muthukumar K 


Sam Chan
Replied On April 28, 2017 11:20 AM UTC

Hi Muthukumar,

Thank you for the reply. I'm happy with the solution.

Regards,

Sam

Muthukumar Kalyanasundaram [Syncfusion]
Replied On April 29, 2017 05:55 AM UTC

Hi Sam, 
 
Thanks for the update. Please let us know if you need any other assistance. 
 
Regards, 
Muthukumar K 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;