Solved

c# datagrid does not refresh (wpf)

Posted on 2011-09-09
8
782 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

Industry Leaders: 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!

Question has a verified solution.

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

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.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

623 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