WPF DataGrid: Blank Row Missing

19,746

Solution 1

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.

Solution 2

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.

Solution 3

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

Solution 4

This happned to me , i forgot to new up the instance and it was nightmare for me . once i created an instance of the collection in onviewloaded it was solved.

`observablecollection<T> _newvariable = new observablecollection<T>();`

this solved my problem. hope it may help others

Solution 5

Add an empty item to your ItemsSource and then remove it. You may have to set CanUserAddRows back to true after doing this. I read this solution here: (Posts by Jarrey and Rick Roen)

I had this problem when I set the ItemsSource to a DataTable's DefaultView and the view was empty. The columns were defined though so it should have been able to get them. Heh.

Share:
19,746

Related videos on Youtube

David Veeneman
Author by

David Veeneman

Delete Me

Updated on June 04, 2022

Comments

  • David Veeneman
    David Veeneman almost 2 years

    I am creating a WPF window with a DataGrid, and I want to show the blank "new item" row at the bottom of the grid that allows me to add a new item to the grid. For some reason, the blank row is not shown on the grid on my window. Here is the markup I used to create the DataGrid:

    <toolkit:DataGrid  x:Name="ProjectTasksDataGrid" 
                       DockPanel.Dock="Top" 
                       Style="{DynamicResource {x:Static res:SharedResources.FsBlueGridKey}}"
                       AutoGenerateColumns="False" 
                       ItemsSource="{Binding SelectedProject.Tasks}" 
                       RowHeaderWidth="0" 
                       MouseMove="OnStartDrag" 
                       DragEnter="OnCheckDropTarget" 
                       DragOver="OnCheckDropTarget" 
                       DragLeave="OnCheckDropTarget" 
                       Drop="OnDrop" 
                       InitializingNewItem="ProjectTasksDataGrid_InitializingNewItem">
        <toolkit:DataGrid.Columns>
            <toolkit:DataGridCheckBoxColumn HeaderTemplate="{DynamicResource {x:Static res:SharedResources.CheckmarkHeaderKey}}" Width="25" Binding="{Binding Completed}" IsReadOnly="false"/>
            <toolkit:DataGridTextColumn Header="Days" Width="75" Binding="{Binding NumDays}" IsReadOnly="false"/>
            <toolkit:DataGridTextColumn Header="Due Date" Width="75" Binding="{Binding DueDate, Converter={StaticResource standardDateConverter}}" IsReadOnly="false"/>
            <toolkit:DataGridTextColumn Header="Description" Width="*" Binding="{Binding Description}" IsReadOnly="false"/>
        </toolkit:DataGrid.Columns>
    </toolkit:DataGrid>
    

    I can't figure out why the blank row isn't showing. I have tried the obvious stuff (IsReadOnly="false", CanUserAddRows="True"), with no luck. Any idea why the blank row is disabled? Thanks for your help.

  • David Veeneman
    David Veeneman over 14 years
    Actually, Tasks is an ObservableCollection<T>. I did a test project binding a data grid to the same sort of collection, and the blank row is present at the bottom of the grid. Vincent's blog post is good, but he makes it sound like you have to implement IEditableObject, which is not the case. A plain vanilla DataGrid, bound to an ObservableCollection<T>, should display the blank row. See codeproject.com/KB/WPF/MVVM_DataGrid.aspx.
  • Brett
    Brett over 12 years
    Ugh. Thanks. This has been driving me absolutely crazy. I finally gave up on entities and moved to typed datasets and even that failed. The trick is to assign the collection first and then manipulate it by adding and removing an object.
  • Alaa Jabre
    Alaa Jabre almost 11 years
    Thank you very much that was helpful. Sincerely.