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

Initializing data on AddingNewDataItem

4 Answers 404 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Steve
Top achievements
Rank 1
Steve asked on 21 Dec 2010, 05:33 PM

Hello

I'm trying to set default values for a DataRow when it is newly inserted into a RadGridView. See the cut down sample below.
If I use AddingNewDataItem to create the row, then I get an error.
If instead I let the grid create the row, and then use BeginningEdit to set the values, then the values don't show.

What's the best way to set default values with a new DataRow?

Many Thanks



public
partial class MainWindow : Window

{

       private RadGridView _grid;

       private DataTable _table;

 


       public MainWindow()

       {

              InitializeComponent();

 


              _grid = new RadGridView { AutoGenerateColumns = false, CanUserInsertRows = true, ShowInsertRow = true };

              Content = _grid;

      

              _grid.AddingNewDataItem += new EventHandler<GridViewAddingNewEventArgs>( _grid_AddingNewDataItem );

              _grid.BeginningEdit += new EventHandler<GridViewBeginningEditRoutedEventArgs>( _grid_BeginningEdit );

 


              _grid.Columns.Add( new GridViewDataColumn { DataMemberBinding = new Binding { Path = new PropertyPath( "Column1" ), Mode = BindingMode.TwoWay } } );

              _grid.Columns.Add( new GridViewDataColumn { DataMemberBinding = new Binding { Path = new PropertyPath( "Column2" ), Mode = BindingMode.TwoWay } } );

 


              _table = new DataTable();

              _table.Columns.Add( "Column1", typeof( string ) );

              _table.Columns.Add( "Column2", typeof( string ) );

              _table.Rows.Add( "row1-col1", "row1-col2" );

              _grid.ItemsSource = _table.DefaultView;

       }

 


       private void _grid_BeginningEdit( object sender, GridViewBeginningEditRoutedEventArgs e )

       {

              // This won't show "INIT" until Column2 is focused

              //DataRowView dataRowView = e.Row.Item as DataRowView;

              //if( dataRowView.Row.RowState == DataRowState.Detached )

              //{

              //    dataRowView[ "Column2" ] = "INIT";

              //}

       }

 


       private void _grid_AddingNewDataItem( object sender, GridViewAddingNewEventArgs e )

       {

              // This will cause an error: Cannot add external objects to this list

              //e.NewObject = _table.DefaultView.AddNew();

       }

}

4 Answers, 1 is accepted

Sort by
0
Vanya Pavlova
Telerik team
answered on 22 Dec 2010, 03:03 PM
Hi Steve,

 
You may use the following forum thread for further reference.

All the best,
Vanya Pavlova
the Telerik team
Browse the videos here>> to help you get started with RadControls for WPF
0
Steve
Top achievements
Rank 1
answered on 23 Dec 2010, 12:37 AM
Thanks for the reply Vanya.  I couldn't find anywhere in the attached code on that thread where a new data record is initialized prior to showing though.

I'm basically trying to do:

TheNewDataRow[ "Column1" ] = "some data"

before showing the new record.

Many Thanks
0
Accepted
Nedyalko Nikolov
Telerik team
answered on 24 Dec 2010, 04:28 PM
Hello Steve,

Generally AddingNewDataItem event is the right way to achieve this task. With the specific of the DataTable.DefaultView and that its AddNew method used to create a new DataRowView actually adds this row into table. This sounds great by DefaultView implements INotifyCollectionChanged and this item is inserted immediately in RadGridView, so we have to cancel adding new row from RadGridView again. This could be accomplished by e.Cancel = true within AddingNewDataItem event handler. Another part of the task is to put this particular row into edit mode. This could be done by RadGridView.ScrollIntoViewAsync method. I hope that following code snippet will be helpful.

void radGridView_AddingNewDataItem(object sender, GridViewAddingNewEventArgs e)
        {
            e.Cancel = true;
            var newRow = this.dataSource.DefaultView.AddNew();
            newRow["FirstName"] = "John";
            newRow["LastName"] = "Doe";
            this.radGridView.ScrollIntoViewAsync(newRow, (f) =>
            {
                GridViewRow row = f as GridViewRow;
                if (row != null)
                {
                    ((GridViewCell) row.Cells[0]).BeginEdit();
                }
            });
        }

Let me know how it works on your end.

Regards,
Nedyalko Nikolov
the Telerik team
Browse the videos here>> to help you get started with RadControls for WPF
0
Steve
Top achievements
Rank 1
answered on 26 Dec 2010, 12:54 PM
Perfect, that works well. Thanks Nedyalko.
Tags
GridView
Asked by
Steve
Top achievements
Rank 1
Answers by
Vanya Pavlova
Telerik team
Steve
Top achievements
Rank 1
Nedyalko Nikolov
Telerik team
Share this question
or