WPF DataGrid: Blank Row Missing

Vincent Sibal posted an article describing what is required for adding new rows to a DataGrid. There are quite a few possibilities, and most of this depends on the type of collection you're using for SelectedProject.Tasks.

I would recommend making sure that "Tasks" is not a read only collection, and that it supports one of the required interfaces (mentioned in the previous link) to allow new items to be added correctly with DataGrid.


You must also have to have a default constructor on the type in the collection.


Finally got back to this one. I am not going to change the accepted answer (green checkmark), but here is the cause of the problem:

My View Model wraps domain classes to provide infrastructure needed by WPF. I wrote a CodeProject article on the wrap method I use, which includes a collection class that has two type parameters:

VmCollection<VM, DM>

where DM is a wrapped domain class, and DM is the WPF class that wraps it.

It truns out that, for some weird reason, having the second type parameter in the collection class causes the WPF DataGrid to become uneditable. The fix is to eliminate the second type parameter.

Can't say why this works, only that it does. Hope it helps somebody else down the road.


In my opinion this is a bug in the DataGrid. Mike Blandford's link helped me to finally realize what the problem is: The DataGrid does not recognize the type of the rows until it has a real object bound. The edit row does not appear b/c the data grid doesn't know the column types. You would think that binding a strongly typed collection would work, but it does not.

To expand upon Mike Blandford's answer, you must first assign the empty collection and then add and remove a row. For example,

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        // data binding
        dataGridUsers.ItemsSource = GetMembershipUsers();
        EntRefUserDataSet.EntRefUserDataTable dt = (EntRefUserDataSet.EntRefUserDataTable)dataGridUsers.ItemsSource;
        // hack to force edit row to appear for empty collections
        if (dt.Rows.Count == 0)
        {
            dt.AddEntRefUserRow("", "", false, false);
            dt.Rows[0].Delete();
        }
    }