0
votes

I have a data grid which I binded to a ObservableCollection object. On an event, I clear the ObservableCollection and add new items to it. When finished, I try to update the DataGrid, but it still shows the old rows. What am I doing wrong? This is my XAML:

<DataGrid 
   ItemsSource="{Binding }" 
   AutoGenerateColumns="False" 
   Name="dgvCurrentFaults" 
   TabIndex="0" 
   Background="Transparent" 
   RowBackground="#B4CDCD" 
   Foreground="#314E54" >
   <DataGrid.Columns>
      <DataGridTemplateColumn Header="Icon" Width="70" IsReadOnly="True">
         <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
               <Image Source="{Binding Icon}" Width="20" Height="20"/>
            </DataTemplate>
         </DataGridTemplateColumn.CellTemplate>
      </DataGridTemplateColumn>
      <DataGridHyperlinkColumn  Header="Display" Binding="{Binding Display}" ContentBinding="{Binding Display}" IsReadOnly="True">
         <DataGridHyperlinkColumn.ElementStyle>
            <Style>
               <EventSetter Event="Hyperlink.Click" Handler="dgvCurrentFaults_CellContentClick"/>
            </Style>
         </DataGridHyperlinkColumn.ElementStyle>
      </DataGridHyperlinkColumn>
      <DataGridTextColumn Header="Fault Name" Binding="{Binding Falut_Name}" Width="150" IsReadOnly="True">
         <DataGridTextColumn.ElementStyle>
            <Style TargetType="TextBlock">
               <Setter Property="TextWrapping" Value="Wrap"/>
            </Style>
         </DataGridTextColumn.ElementStyle>
      </DataGridTextColumn>
      <DataGridTextColumn Header="Description" Binding="{Binding Fault_Description}" Width="240" IsReadOnly="True">
         <DataGridTextColumn.ElementStyle>
            <Style TargetType="TextBlock">
               <Setter Property="TextWrapping" Value="Wrap"/>
            </Style>
         </DataGridTextColumn.ElementStyle>
      </DataGridTextColumn>
      <DataGridTextColumn Header="Action Required" Binding="{Binding ActionRequired}" Width="200" IsReadOnly="True">
         <DataGridTextColumn.ElementStyle>
            <Style TargetType="TextBlock">
               <Setter Property="TextWrapping" Value="Wrap"/>
            </Style>
         </DataGridTextColumn.ElementStyle>
      </DataGridTextColumn>
      <DataGridTextColumn Header="ID Fault" Binding="{Binding IDFault}" Visibility="Hidden" IsReadOnly="True"/>
   </DataGrid.Columns>
</DataGrid>

and this is my code

public ObservableCollection<FaultsInfo> infoFaultList { get; set; }

private void UpdateTable()
{
    infoFaultList.Clear();
    infoFaultList.Add(new infoFault(1));
    infoFaultList.Add(new infoFault(2));

    dgvCurrentFaults.ItemsSource = null;
    dgvCurrentFaults.ItemsSource = infoFaultList;
    dgvCurrentFaults.UpdateLayout();
    dgvCurrentFaults.Items.Refresh();
}

Edit:

After having many more looks on the subject, I see that the first time I update the DataGrid is on the Loaded event of the UserControl. In this case, the DataGrid update fine. Later, the DataGrid updates on an event that is launched by some communication. In that case, it dose not udate. I thought that maybe the problem is that I try to update it from another thread, although I use Invoke.

2
you will need to show the code where you are setting the datacontext of datagrid and updating your Observable collectionNitin

2 Answers

0
votes

I the source property is an observable collection, you doesn't need to set it again. You doesn't need this code:

dgvCurrentFaults.ItemsSource = null;
dgvCurrentFaults.ItemsSource = infoFaultList;

Also, the code above also doesn't works because the class must implement the INotifyPropertyChanged interface:

ObservableCollection<FaultsInfo> _infoFaultList;
public ObservableCollection<FaultsInfo> infoFaultList
{
    get
    {
        return _infoFaultList;        
    }
    set
    {
        _infoFaultList = value;
        NotifyPropertyChanged("infoFaultList");
    }
}

Also you need to check if the binding is working. If your xaml code is right, then the data grid's data context should be the collection itself before the binding working like you are using it: ItemsSource="{Binding }". If the collection is not the data context, then you should fix the binding and make some one like this ItemsSource="{Binding InfoFaultList}".

Hope this helps.

0
votes
 public ObservableCollection<FaultsInfo> infoFaultList { get; set; }

        public MainWindow2()
        {
            InitializeComponent();
            infoFaultList = new ObservableCollection<FaultsInfo>();
            infoFaultList.Add(new FaultsInfo(5));
            infoFaultList.Add(new FaultsInfo(6));
            dgvCurrentFaults.ItemsSource = infoFaultList;
            dgvCurrentFaults.UpdateLayout();
        }

        private void UpdateTable()
        {
            infoFaultList.Clear();
            infoFaultList.Add(new FaultsInfo(1));
            infoFaultList.Add(new FaultsInfo(2));
            dgvCurrentFaults.ItemsSource = null;
            dgvCurrentFaults.ItemsSource = infoFaultList;
            dgvCurrentFaults.UpdateLayout();
            dgvCurrentFaults.Items.Refresh();
        }

        private void btnName_Click_1(object sender, RoutedEventArgs e)
        {
            UpdateTable();
        }

XAML Code

 <StackPanel Orientation="Vertical">
    <Grid>
        <DataGrid 
   ItemsSource="{Binding }" 
   AutoGenerateColumns="False" 
   Name="dgvCurrentFaults" 
   TabIndex="0" 
   Background="Transparent" 
   RowBackground="#B4CDCD" 
   Foreground="#314E54" >
            <DataGrid.Columns>
                <!--<DataGridTemplateColumn Header="Icon" Width="70" IsReadOnly="True">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Image Source="{Binding Icon}" Width="20" Height="20"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>-->
                <DataGridHyperlinkColumn  Header="Display" Binding="{Binding Display}" ContentBinding="{Binding Display}" IsReadOnly="True">
                </DataGridHyperlinkColumn>
                <DataGridTextColumn Header="Fault Name" Binding="{Binding Falut_Name}" Width="150" IsReadOnly="True">
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="TextBlock">
                            <Setter Property="TextWrapping" Value="Wrap"/>
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>
                <DataGridTextColumn Header="Description" Binding="{Binding Fault_Description}" Width="240" IsReadOnly="True">
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="TextBlock">
                            <Setter Property="TextWrapping" Value="Wrap"/>
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>
                <DataGridTextColumn Header="Action Required" Binding="{Binding ActionRequired}" Width="200" IsReadOnly="True">
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="TextBlock">
                            <Setter Property="TextWrapping" Value="Wrap"/>
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>
                <DataGridTextColumn Header="ID Fault" Binding="{Binding IDFault}" Visibility="Hidden" IsReadOnly="True"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
        <Button x:Name="btnName"  Width="100" Height="100" Content="Click Me" Click="btnName_Click_1"/>
    </StackPanel>

Above is my code and Its working fine there is no issue u can follow the provided syntax. During testing above code i came to know that whenever I clear infoFaultList or set it to null we had to reinitialize like this.

infoFaultList=new ObservableCollection<FaultsInfo>();