Solved

c# datagrid does not refresh (wpf)

Posted on 2011-09-09
8
765 Views
Last Modified: 2012-05-12
I have a wpf datagrid with the itemsource set:
            dataGrid1.ItemsSource = (mb.dataA);

Open in new window

dataA is an array of a class dataAddress:
        public dataAddress[] dataA;

Open in new window

How do I make the datagrid refresh immediately when I update the array?

The dataGrid only updates when i scroll up or down so the rows that have updated dissapear off the control, and when I scroll them back into view the data has changed.

After I update the array I have tried to set the itemsSource again, but the display doesn't refresh.  All the other examples I can find on the internet relate to databases which I am not using.
0
Comment
Question by:dazzler77
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 

Author Comment

by:dazzler77
ID: 36514814
I think I have solved this myself.

I changed the array "dataA" to a list of type "BindingList"

then I call "mb.dataA.ResetBindings();" to refresh the list.

Not sure if this is the best solution but it seems to work.
0
 
LVL 4

Expert Comment

by:Friman001
ID: 36515073
You can do something along the lines for dataGrid1.DataBind(); and it should rebind.
0
 
LVL 4

Expert Comment

by:Friman001
ID: 36515078
Actually, this is more confusing that I first read.  Do you update the array by clicking a button to submit new data?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:dazzler77
ID: 36516108
Thanks for your feedback Friman001.
unfortunately, dataGrid1 does not have a method called DataBind().

The data is coming in via the serial port, and I have an event that I raise when all the data has arrived.

The dataGrid is supplied in System.Collections namespace.  is there another dataGrid I can use?
0
 
LVL 4

Expert Comment

by:Friman001
ID: 36516356
Yes there is.  DataGrid is supplied through System.Web.UI.WebControls.  Here is an example of one created in the code behind file:

DataGrid dg = new DataGrid();
            dg.DataBind();

Open in new window

0
 

Author Comment

by:dazzler77
ID: 36517218
hmm I don't seem to have that namespace.  I have System.Web but it finishes there.  Maybe thats only for silverlight.
0
 

Accepted Solution

by:
dazzler77 earned 0 total points
ID: 36518733
I think I found the proper way to do it now.

using INotifyPropertyChanged in the class.  Then you don't need to refresh at all.
This way I can also use a IList or other, and I am not limited to using the BindingList.

    public class dataAddress:INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

Open in new window

Then, in the set properties, use OnPropertyChanged
            set 
            { 
                data = value;
                this.OnPropertyChanged("Data");

Open in new window

0
 

Author Closing Comment

by:dazzler77
ID: 36547859
Not sure why the other namespaces/classes suggested weren't available, but this solution that I came accross seems to be correct.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question