Solved

c# datagrid does not refresh (wpf)

Posted on 2011-09-09
8
736 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
  • 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
 

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now