This is a migrated thread and some comments may be shown as answers.

Adding new item

2 Answers 178 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Dave
Top achievements
Rank 1
Dave asked on 22 Jun 2010, 02:23 AM
I am trying to add a new item to the GridView. When the user tabs past the last column of the last row I'm inserting a new row and placing it in edit mode. I do this by handling the KeyDown event like this:

        private void RadGridView_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Tab)
            {
                var grid = sender as RadGridView;
                var currentItem = grid.CurrentItem as OrderDetail;
                if ((currentItem != null) && (currentItem.IsValid))
                {
                    grid.BeginInsert();
                    grid.CurrentColumn = grid.Columns[0];
                }
                else
                {
                    e.Handled = true;
                }
            }
        }

I also handle the AddingNewDataItem event to add a new object:

        private void RadGridView_AddingNewDataItem(object sender, Telerik.Windows.Controls.GridView.GridViewAddingNewEventArgs e)
        {
            e.NewObject = OrderDetail.NewOrderDetail();
        }

The problem is that I need the new object to be added to the underlying collection that is bound to the ItemsSource property of the GridView so that a number of event handlers can be hooked up to events on the OrderDetail object. (These events bubble up list / property change notifications to the parent Order object for such things as calculating the Order total, sales tax, etc.).

The new row is being added and it gets set into edit mode but when I modify data in the columns for Quantity and UnitPrice no events are being published because the OrderDetail object is not inserted into the collection (which does the job of hooking up the events).

I tried to add the object to the collection but then I get two new rows added to the GridView. What I would like to do is add the OrderDetail to the underlying collection so that event handlers get hooked up but then set the row into edit mode on the first column. Is this possible or is there some other way to approach this?

Thanks,
Dave

2 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 24 Jun 2010, 12:07 PM
Hi Dave Kehring,

 
You can submit your changes on the RowEditEnded event of the grid:

private void gridView_RowEditEnded(object sender, GridViewRowEditEndedEventArgs e)
        {                      
            this.gridView.CommitEdit();
        }

Furthermore, a good approach when calculating columns may be found in this blog post. Defining your columns in the way described in the article will result in instant change of the value of the calculated column as soon as you modify some of its components. 


Sincerely yours,
Maya
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Dave
Top achievements
Rank 1
answered on 28 Jun 2010, 05:47 PM
I'm not sure your suggestion is related to my problem. The bottom line is I do not want the grid to control the creation of new data items. I control them in my business objects. Here's what I ended up doing to make this work:

        private void RadGridView_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Tab)
            {
                var grid = sender as RadGridView;
                var currentItem = grid.CurrentItem as OrderDetail;
                if ((currentItem != null) && (currentItem.IsValid))
                {
                    var vm = this.DataContext as OrderEditViewModel;
                    vm.AddOrderDetail();
                    grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[grid.Items.Count - 1], grid.Columns[0]);
                    grid.BeginEdit();
                }
                else
                {
                    e.Handled = true;
                }
            }
        }

I simply capture the KeyDown event and if it's the last row (and the row is valid) I get a reference to the underlying ViewModel via the DataContext, call the method AddOrderDetail on the VM to add a new object to the collection which in turn makes it show up in the grid (the grid is bound to an ObservableCollection). I then set the current cell to the first column of the new row and call BeginEdit.

By the way, your suggestion for handling calculated columns is not desirable in my situation (and probably most) because it pushes domain logic into the UI which is not a good idea. My calculations are performed in my business objects and the results are reflected in the UI using databinding.

Thanks for your response anyway.

Regards,
Dave
Tags
GridView
Asked by
Dave
Top achievements
Rank 1
Answers by
Maya
Telerik team
Dave
Top achievements
Rank 1
Share this question
or