datagrid and observablecollection propblem

essamce 621 Reputation points
2020-11-07T07:53:08.307+00:00

hi,
i have a datagrid in my view bound to observablecollection<myClass>, the propblem occurs when i set datagrid.CanUserAddRows to "True" , it adds a null object to the end of my observablecollection,
and this observablecollection bound to another itemscontrol control as ItemsSource in another view.

Q1: how to prevent my datagrid from adding null object to my collection ?
Q2: how to prevent my itemscontrol from seeing the null element (the one that created automatically by datagrid) ?

thanks in advance.
i'm using VS2019 last version , wpr.core app.

Developer technologies Windows Presentation Foundation
0 comments No comments
{count} votes

Accepted answer
  1. Emon Haque 3,176 Reputation points
    2020-11-07T08:28:43.077+00:00

    For my purpose I don't use DataGrid at all for anything. For reporting ItemsControl, ListBox, etc. are more than enough. For adding objects into collection I create small input form with validation and when is valid I let it to get added into collection.

    To hide the null in your ItemsControl, create a DataTrigger and check for x:Null. If it is null (last item) which in fact always will be, keep it collapsed.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,341 Reputation points
    2020-11-07T10:55:37.527+00:00

    Hi,
    if you use a second View with the same Source you don't see null elements. Try following demo with

    XAML

    <Window x:Class="WpfApp1.Window07"  
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
            xmlns:local="clr-namespace:WpfApp07"  
            mc:Ignorable="d"  
            Title="Window07" Height="450" Width="800">  
      <Window.DataContext>  
        <local:ViewModel/>  
      </Window.DataContext>  
      <Grid>  
        <Grid.ColumnDefinitions>  
          <ColumnDefinition/>  
          <ColumnDefinition/>  
        </Grid.ColumnDefinitions>  
        <DataGrid ItemsSource="{Binding View1}"/>  
        <ListBox Grid.Column="1" ItemsSource="{Binding View2}">  
          <ListBox.ItemTemplate>  
            <DataTemplate>  
              <Label Content="{Binding Info}"/>  
            </DataTemplate>  
          </ListBox.ItemTemplate>  
        </ListBox>  
      </Grid>  
    </Window>  
    

    And classes:

    using System.Collections.ObjectModel;  
    using System.ComponentModel;  
    using System.Windows;  
    using System.Windows.Data;  
      
    namespace WpfApp07  
    {  
      public class ViewModel  
      {  
        ObservableCollection<Data> col = new ObservableCollection<Data>();  
        CollectionViewSource cvs = new CollectionViewSource();  
        public ViewModel() => cvs.Source = col;  
        public ICollectionView View1 { get => cvs.View; }  
        public ICollectionView View2 { get => cvs.View; }  
      }  
      
      public class Data  
      {  
        public int ID { get; set; }  
        public string Info { get; set; }  
      }  
    }  
    

    Result:

    38137-x.gif

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.