Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF datagrid allow user to add rows?

I would like my WPF Datagrid that is bound to my observable collection to have the blank row at the bottom so that the user can add more info. I've successfully bound the data, i.e. I can see it.

Why is the 'new' blank row not showing? Here is my xaml declaration:

<UserControl.Resources>   <CollectionViewSource x:Key="MyItems" Source="{Binding Path=AllItems}">   </CollectionViewSource> </UserControl.Resource>  <my:DataGrid HorizontalAlignment="Stretch"   AutoGenerateColumns="True"   SelectionUnit="FullRow"   CanUserAddRows="True"   CanUserDeleteRows="True"   DataContext="{StaticResource MyItems}"   ItemsSource="{Binding}"> 

PS: I'm using Josh Smith's MVVM implementation. I have also read some SO posts on the issue and they have not helped.

Thanks in advance.

Update 2010-01-14:

When the usercontrol load event occurs, "CanUserAdddRows" is false. I suspect is has something to do with the conditions listed here.

like image 943
JP Richardson Avatar asked Jan 13 '10 20:01

JP Richardson


People also ask

Can user add rows DataGrid WPF?

WPF DataGrid (SfDataGrid) provides built-in row called AddNewRow. It allows user to add a new row to underlying collection. You can enable or disable by setting SfDataGrid.

What is the difference between DataGrid and DataGridView?

The DataGrid control is limited to displaying data from an external data source. The DataGridView control, however, can display unbound data stored in the control, data from a bound data source, or bound and unbound data together.

What is DataGrid WPF?

A DataGrid is a control that displays data in a customizable grid. It provides a flexible way to display a collection of data in rows and columns. The hierarchical inheritance of DataGrid class is as follows −


2 Answers

Found the problem. My constructor in the object that's part of my ObservableCollection wasn't declared public.

*Hits head*

Thanks for your time.

like image 182
JP Richardson Avatar answered Sep 21 '22 22:09

JP Richardson


Make sure your objects in the ObeservableCollection have a default parameterless constructor.

like image 39
bartonm Avatar answered Sep 21 '22 22:09

bartonm