views:

45

answers:

1

I want to add a new row in the datagrid when click on a button in the outside of a datagrid. The datagrid is bound with SQL Server database, and it have some data at runtime I want to add new data to the database through the database

I tried a lot, but it was unsuccessful

Anyone reply me it will be very helpful for me...

Advance thanks..

+1  A: 

If the collection of data bound to the grid implements INotifyCollectionChanged, adding a new item to the collection will add the row to the datagrid.

When you read the data from the DB, store it in an ObservableCollection (which implements this interface), then bind the data to the grid.

Example:

public class ViewModel {

   public ObservableCollection<Data> Items { get; set; }

   ...

}

In View.xaml:

...
<DataGrid ItemsSource={Binding Path=Items}" ... />
...

And you have to set the DataContext property of the view to an instance of ViewModel.

From now on, adding/removing items from the observable collection will automatically trigger the same operation on the data grid.

Timores