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

Overriding GridAddNewRowController to allow AddNewRow to be positioned at any row

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

5 Replies

MK Muthukumar Kalyanasundaram Syncfusion Team 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  



SC Sam Chan 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


MK Muthukumar Kalyanasundaram Syncfusion Team 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 



SC Sam Chan April 28, 2017 11:20 AM UTC

Hi Muthukumar,

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

Regards,

Sam


MK Muthukumar Kalyanasundaram Syncfusion Team 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 


Loader.
Live Chat Icon For mobile
Up arrow icon