Solved

c# datagrid does not refresh (wpf)

Posted on 2011-09-09
8
755 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

820 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