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();
}
}