Results 1 to 6 of 6

Thread: Problems adding rows to a DataGrid

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    258

    Question Problems adding rows to a DataGrid

    To start with, I am using VS2012 and .NET 4.5.

    I am binding a DataGrid to an ObservableCollection<DataItem> and want to allow in-grid editing. I have CanUserAddRows and CanUserDeleteRows set to true. When I bind the DataGrid to the collection, the data displays perfectly (only one column of the class visible, the rest can be filled in programmatically), along with a {NewItemPlaceholder} row. I can edit existing rows and it works fine, but when I attempt to add a new row by moving to the placeholder, entering a value, and then hitting enter, nothing happens. That is, the new value is displayed, but the AddingNewItem event never fires, the new row isn't added to the underlying collection, and no new {NewItemPlaceholder} row is added to the DataGrid.

    I'm probably doing (or, more accurately, NOT doing) something stupid, but I haven't done a lot of work with DataGrids, so I'm looking for a hand.

    Do I have to manually implement the add functionality? If so, is there a built-in event to tell me when to do that, or do I have to do something kludgy like trap the enter key and use that to fire the manual add functionality? Or am I just missing something fairly simple?

  2. #2
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: Problems adding rows to a DataGrid

    Given that your DataItem class implements INotifyPropertyChanged, you need to add Mode and UpdateSourceTrigger
    in your data grid columns.
    Code:
       <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding}" 
                      Margin="12,12,12,41" Name="dataGrid1" 
                      RowEditEnding="dataGrid1_RowEditEnding">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding UserName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="User name" Width="230">                    
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding FirstName,  Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="First Name" Width="250">
                    </DataGridTextColumn>
                </DataGrid.Columns>
            </DataGrid>
    where UserName and FirstName are properties defined in DataItem class.

    KGC
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    258

    Re: Problems adding rows to a DataGrid

    Already got that... as I said, it works fine updating existing data, which means the columns are bound properly. It's the adding of new rows that doesn't work as it should...

    Here's the XAML for the DataGrid:

    Code:
    <DataGrid x:Name="grdAddls" Grid.Row="3" Grid.Column="4" Margin="5,2,5,2" AutoGenerateColumns="False" 
                        IsEnabled="True" IsReadOnly="False" RowHeaderWidth="30" AlternatingRowBackground="LightGray" GridLinesVisibility="All" BorderThickness="2" MouseRightButtonUp="grdAddls_MouseRightButtonUp" AddingNewItem="grdAddls_AddingNewItem" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserResizeRows="False" CanUserSortColumns="False">
        <DataGrid.RowHeaderStyle>
            <Style TargetType="{x:Type DataGridRowHeader}">
                <Setter Property="Content" Value="{Binding Converter={StaticResource IndexConverter}, ConverterParameter=grdAddls}" />    <!-- Shows a row # -->
            </Style>
        </DataGrid.RowHeaderStyle>
        <DataGrid.Columns>
            <DataGridTextColumn Header="Proc ID" Binding="{Binding Path=ProcID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Visibility="Hidden"></DataGridTextColumn>
            <DataGridTextColumn Header="ProcAddlID" Binding="{Binding Path=ProcAddlID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Visibility="Hidden"></DataGridTextColumn>
            <DataGridTemplateColumn Header="Code">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox CharacterCasing="Upper" Text="{Binding Path=ProcCodeDisplay, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
    It works fine when updating existing rows... the values are placed in the underlying observable collection when they are modified. It's adding a new row where it messes up... entering a code and hitting return does nothing, not creating the object in the underlying collection, not creating a new "add row" and moving the cursor to it, nothing.

  4. #4
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: Problems adding rows to a DataGrid

    Try adding a RowEditEnding with Commit checking to trace any changes to your observable collection object. Given that you are entering new item to a new item placeholder.

    Code:
    private void grdAddls_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
    {
        if (e.EditAction == DataGridEditAction.Commit)
        {
                //add logic to check your observable collection
               //......
        }  
    }
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    258

    Re: Problems adding rows to a DataGrid

    I have verified that the underlying collection isn't being modified two ways. First, by placing a breakpoint in the AddingNewItem handler, which is never fired. Second, by putting a button on the form to display the underlying collection in a messagebox. The collection isn't being updated at any point with the data I entered into the NewItemPlacholder row, and I've tried moving focus to another control, moving back to other cells in the same DataGrid, and every other GUI action I could think of. At no point was the underlying collection updated.

    However, I added the RowEditEnding handler and tried again, but it never fired no matter how much I moved through the DataGrid rows or clicked on other edit fields on the form. And the underlying collection never changed.

  6. #6
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: Problems adding rows to a DataGrid

    In my case, I created a property AllDataItems of type ObservableCollection<DataItems>, then in the code below,
    I added a collection changed event to monitor any changes to collection like Add/Delete and etc..

    Constructor
    Code:
    dataGrid1.ItemsSource = AllDataItems;
    AllDataItems.CollectionChanged += AllDataItems_CollectionChanged;
    Code:
    // monitor any collection changes
    private void AllDataItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
       //logic to monitor AllDataItems    
    }
    Did you implement INotifyPropertyChanged in DataItem class?

    KGC
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width