Skip to main content

Answered Question Silverlight datagrid periodically (sporadically) does not update datagrid display contentsRSS Feed

(0)

craigbowers
craigbowers

Member

Member

0 points

2 Posts

Silverlight datagrid periodically (sporadically) does not update datagrid display contents

Problem description:

We've set up a very simple test page that contains a datagrid with 100 rows and 6 columns. Once the page is rendered, I systematically double-click into each cell (starting in the top-left corner and proceeding top-to-bottom, then left-to-right) and type a new value.  Most of the time, the cell displays the new value.  However, occasionally, the cell displays the old value.  After this happens, if I then double-click on that errored cell to re-enter edit mode, it will show the new value.

This error can take some time to reproduce.  Most of the time, the cell displays the new value as it should.  However, within a couple minutes of clicking and changing values, the error occurs (a cell will show the old value).  I did some further testing and noted that after the initial edit (when the cell is displaying the old value, the datasource in the code behind actually captured the new value).  In order to use this Silverlight control in our application, we need a fix or a workaround as the current behaviour makes the datagrid unusable.

 

Code Sample

Xaml

<UserControl x:Class="GridTest"
    xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
    <Grid x:Name="LayoutRoot" Background="White">
        <data:DataGrid x:Name="dataGrid" AutoGenerateColumns="true" >
        </data:DataGrid>
    </Grid>
</UserControl>

C#

    public class XClass
    {
        public string Index { get; set; }
        public string Col1 { get; set; }
        public string Col2 { get; set; }
        public string Col3 { get; set; }
        public string Col4 { get; set; }
        public string Col5 { get; set; }
    }

    public partial class GridTest : UserControl
    {
        List<XClass> tc = new List<XClass>();
        public GridTest()
        {
            InitializeComponent();
            for (int i = 0; i < 100; i++)
            {
                tc.Add(new XClass()
                {
                    Index = i.ToString(),
                    Col1 = "1",
                    Col2 = "1",
                    Col3 = "1",
                    Col4 = "1",
                    Col5 = "1",
                });
            }
            dataGrid.ItemsSource = tc;
        }
    }

Notes:

This item has been cross-posted to:  https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=381548

 

ken tucker
ken tucker

All-Star

All-Star

18166 points

2,749 Posts

Answered Question

Re: Silverlight datagrid periodically (sporadically) does not update datagrid display contents

When databinding to a class you should have the class implement the INotifyPropertyChanged interface.

 

http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(VS.95).aspx

You should also use an observablecollection to store your data instead of a list.  The observablecollection is set up for data binding

http://msdn.microsoft.com/en-us/library/ms668604(VS.95).aspx

craigbowers
craigbowers

Member

Member

0 points

2 Posts

Re: Re: Silverlight datagrid periodically (sporadically) does not update datagrid display contents

Ken, thank you for your response.  Your suggestion works.  Implementing both ObservableCollection and INotifyPropertyChanged, the cells no longer display the old values.

  • Unanswered Question
  • Answered Question
  • Announcement