Solved

c# datagrid does not refresh (wpf)

Posted on 2011-09-09
8
775 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

734 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