Adding rows to a WPF RadGridView based on a DataTable

1 Answer 257 Views
GridView
Daniel
Top achievements
Rank 1
Daniel asked on 09 Nov 2021, 10:29 AM

Hi,

I want to use the RadGridView to show data from a DataTable and also allow the user to add new rows. My current version kind of works but there are still some problems:

New rows are not focused

This article says:

If the ItemsSource is a DataTable.DefaultView, you can initialize the newly inserted item as shown in Example 4:

private void radGridView_AddingNewDataItem2(object sender, GridViewAddingNewEventArgs e) 
{ 
    e.Cancel = true; 
    var newRow = this.dataSource.DefaultView.AddNew(); 
    newRow["FirstName"] = "John"; 
    newRow["LastName"] = "Doe"; 
    e.NewObject = newRow; 
}

This creates a new row, but it does not set the focus to the new row. I guess this is because the event is actually cancelled and the row is added to the DataTable instead. I tried to manually set the focus afterwards, but so far without success. Is there a recommended solution?

New row is not visible when the grid is already on the last page

I tried combining the RadGridView with a RadDataPager as described here. The paging works, but new rows are always added to the last page. That's okay, and the proposed solution is to move the grid to the last page. This works when the grid is not already on the last page. If it is, however, then the new row is not visible. Only when I navigate to another page and back to the last page, the new row appears. What can I do to show the new row in this case without changing the page?

1 Answer, 1 is accepted

Sort by
1
Accepted
Stenly
Telerik team
answered on 12 Nov 2021, 10:07 AM

Hello Daniel,

In order to focus the newly added rows, you could set the e.NewObject property to the SelectedItem property of the grid view control, inside of the provided AddingNewDataItem method.

private void radGridView_AddingNewDataItem2(object sender, GridViewAddingNewEventArgs e) 
{ 
    e.Cancel = true; 
    var newRow = this.dataSource.DefaultView.AddNew(); 
    newRow["FirstName"] = "John"; 
    newRow["LastName"] = "Doe"; 
    e.NewObject = newRow; 
	
	this.gridView.SelectedItem = e.NewObject;
}

Regarding the second question, I have created a sample project so that I can reproduce the mentioned behavior, however, when the grid view is on the last page and a new item is added, it automatically becomes visible in the view. With this said, I have attached the mentioned project for you to test. It has a RadGridView and RadDataPager controls implemented, as well as the AddingNewDataItem method, which is modified to automatically scroll to the last page and select the last added row.

private void gridView_AddingNewDataItem(object sender, Telerik.Windows.Controls.GridView.GridViewAddingNewEventArgs e)
{
    e.Cancel = true;

    Dispatcher.BeginInvoke(new Action(() => {
        ((RadGridView)sender).Items.MoveToLastPage();

        var gridView = (RadGridView)sender;

        var table = (DataView)gridView.ItemsSource;
        var newRow = table.AddNew();
        newRow["FirstName"] = "John";
        newRow["LastName"] = "Doe";
        e.NewObject = newRow;

        gridView.SelectedItem = e.NewObject;
    }));
}

That said, could you give the attached project a try?

Regards,
Stenly
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Daniel
Top achievements
Rank 1
commented on 17 Nov 2021, 04:40 PM

Thank you, the problem was that I was binding the grid's ItemsSource to the pager's PagedSource property, and not the pager's Source to the grid's Items as in your sample. One note regarding your code sample though: I had to set e.NewObject in the event handler, not in the action.
Tags
GridView
Asked by
Daniel
Top achievements
Rank 1
Answers by
Stenly
Telerik team
Share this question
or