Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the content of a new row Datagrid

Tags:

database

wpf

I have a DataGrid showing some databases having quite some columns.
I would like that, when the user edit a new row, some values are set automatically.
With the windows form DataGrid that would be easy, since there's RowsAdded event handler. But how could i handle this with the wpf DataGrid ??
Edit : my DataGrid is bound in Xaml to a public property which is an ITable. When user select a table in a ComboBox, the property is updated with corresponding table. Yes there's autogenerating column, and the way the user can enter a new row is to edit the last blank row (default behaviour).

like image 495
GameAlchemist Avatar asked Oct 26 '25 15:10

GameAlchemist


2 Answers

You can do this in the LoadingRow event. Try something like this:

private void myDataGrid_LoadingRow(object sender, System.Windows.Controls.DataGridRowEventArgs e)
{
    MyObject myObject = e.Row.Item as MyObject;
    if (myObject != null)
    {
        myObject.PropertyOne = "test";
        myObject.PropertyTwo = 2;
    }
}
like image 190
Eirik Avatar answered Oct 29 '25 07:10

Eirik


Ok i think i got it.
When a DataTable is bound to a DataGrid, a CollectionView is created in order to see it. You can get it by using the (static/shared) CollectionView.GetDefaultView(ThePropertyThatIsBound) method.
Since it implements ICollectionChanged, you can add an event handler to the CollectionChangedEvent.

In the CollectionChanged event handler, if you have a new item (e.NewItems.Count>0) you must check it against System.Windows.Data.CollectionView.NewItemPlaceholder and if it is not a place holder, then it is a brand new item, for wich i can set all default values.

like image 41
GameAlchemist Avatar answered Oct 29 '25 07:10

GameAlchemist



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!