Note that there are some explanatory texts on larger screens.

plurals
  1. PODataGrid throws InvalidOperationException by Scrolling
    primarykey
    data
    text
    <p>I want to show in a DataGrid the contents of a file. (The file contains more than 200,000 lines)</p> <p>To show the Grid with the Data is fast.</p> <p>But when I use the Scrollbar (for down scrolling) I've got the following exception:</p> <pre><code>System.InvalidOperationException: {"An ItemsControl is inconsistent with its items source.\n See the inner exception for more information."} </code></pre> <p>InnerException:</p> <pre><code>Information for developers (use Text Visualizer to read this): This exception was thrown because the generator for control 'System.Windows.Controls.DataGrid Items.Count:0' with name '(unnamed)' has received sequence of CollectionChanged events that do not agree with the current state of the Items collection. The following differences were detected: Accumulated count 0 is different from actual count 200000. [Accumulated count is (Count at last Reset + #Adds - #Removes since last Reset).] One or more of the following sources may have raised the wrong events: System.Windows.Controls.ItemContainerGenerator System.Windows.Controls.ItemCollection System.Windows.Data.ListCollectionView System.Collections.Generic.List`1[[WpfApplication3.Entry, WpfApplication3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] (The starred sources are considered more likely to be the cause of the problem.) The most common causes are (a) changing the collection or its Count without raising a corresponding event, and (b) raising an event with an incorrect index or item parameter. The exception's stack trace describes how the inconsistencies were detected, not how they occurred. To get a more timely exception, set the attached property 'PresentationTraceSources.TraceLevel' on the generator to value 'High' and rerun the scenario. One way to do this is to run a command similar to the following: System.Diagnostics.PresentationTraceSources.SetTraceLevel(myItemsControl.ItemContainerGenerator, System.Diagnostics.PresentationTraceLevel.High) from the Immediate window. This causes the detection logic to run after every CollectionChanged event, so it will slow down the application. </code></pre> <p>The Exception tells that it: "has received sequence of CollectionChanged events that do not agree with the current state of the Items collection." </p> <p>Thats the code:</p> <p><strong>MainWindow.xaml</strong></p> <pre><code>&lt;Window x:Class="WpfApplication3.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:WpfApplication3="clr-namespace:WpfApplication3" Title="MainWindow" Height="350" Width="525"&gt; &lt;Grid Name="Test"&gt; &lt;WpfApplication3:Viewer x:Name="LogUC" /&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p><strong>MainWindow.xaml.cs</strong></p> <pre><code>public partial class MainWindow { public MainWindow() { InitializeComponent(); Test.DataContext = this; LogUC.Loaded += LogUcOnLoaded; } private void LogUcOnLoaded(object sender, RoutedEventArgs routedEventArgs) { LogUC.Test(); } } </code></pre> <p><strong>Viewer.xaml</strong></p> <pre><code>&lt;UserControl x:Class="WpfApplication3.Viewer" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="300" Width="300"&gt; &lt;Grid Name="Container"&gt; &lt;DataGrid ItemsSource="{Binding Path=EntryCollection, Mode=OneTime}" AutoGenerateColumns="False" CanUserResizeColumns="True" CanUserResizeRows="True" CanUserAddRows="False" CanUserDeleteRows="False" IsReadOnly="True"&gt; &lt;DataGrid.Columns&gt; &lt;DataGridTextColumn Binding="{Binding ErrorCode}" Header="" /&gt; &lt;DataGridTextColumn Binding="{Binding Time}" Header="Time" /&gt; &lt;DataGridTextColumn Binding="{Binding Content}" Header="Content" /&gt; &lt;/DataGrid.Columns&gt; &lt;/DataGrid&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p><strong>Viewer.xaml.cs</strong></p> <pre><code>public partial class Viewer : INotifyPropertyChanged { public Viewer() { EntryCollection = new List&lt;Entry&gt;(); InitializeComponent(); Container.DataContext = this; } public List&lt;Entry&gt; EntryCollection { get; set; } internal void Test() { List&lt;Entry&gt; test = new List&lt;Entry&gt;(); for (int i = 0; i &lt; 200000; i++) { Entry entry = new Entry(){ ErrorCode = 0, Time = DateTime.Now.ToString(), Content = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet." }; test.Add(entry); } EntryCollection.AddRange(test); OnPropertyChanged("EntryCollection"); } #region Implementation of INotifyPropertyChanged public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } #endregion Implementation of INotifyPropertyChanged } </code></pre> <p><strong>Entry.cs</strong></p> <pre><code>public class Entry { public int ErrorCode { get; set; } public string Time { get; set; } public string Content { get; set; } } </code></pre> <p>What's the problem?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload