I have a Silverlight 4 application that uses a DataGrid. This DataGrid is bound to a List of approximately 1,000 records. One of the columns on this grid is dynamic in the sense that it should update once every thirty seconds.
I have a DispatcherTimer setup that is triggered every 30 seconds. My question is, what is the best way for me to update the values of just that one column?
Thank you!
Make each dataitem representing the row implementing INotifyPropertyChanged, and raise the event when you update the column value. This should work by displayng the canges as soon as you update the variable.
Related
I have a listview that contains a list of 'rows', which in term, contains a list of 'columns'. For now, I use List for both rows and columns (i.e. I have a List and it the Rows objet, I have a List, but as the number gets bigger, the time it takes to update the GUI and overall performance of the program gets pretty bad. Plus, the GUI isn't notified when the items (columns within the rows) change, so I have to do an Item.Refresh, which seems to refresh the whole thing each time, adding to the slow performance
Is there any other type of object that I could use to accelerate the whole thing. Meaning it would need to satisfied the following points :
- Faster that List
- When I add a new rows, the listview is updated. Is possible, only the new rows is added instead of the whole listview.
- When I change a Columns object, the GUI is updated. If possible, only the modified columns objet is affected so the whole Listview is not changed.
Note that there are quite a number of actual calculations behind the scene for displaying the columns object. I have currently working on optimizing this end, but I'm pretty sure using a better collection would help quite a lot here.
Thanks
I believe what you are looking for is an Observable Collection
This collection type will fire a CollectionChanged event when items are added. You will not need to refresh the collection because WPF controls support this out of the box.
Note
The CollectionChanged event will not be fired when indiviual elements are updated. If you want these updates to show on the UI, the object within the collection should implement INotifyPropertyChanged
I was wondering what the best way was to create an empty datagrid.
For example after you have hit new in excel, You have a grid with empty rows and columns.
I am using c# with WPF and .net 4.0.
Thank you.
As the comments have suggested, a datagrid is not a spreadsheet, but a method to display / edit existing data. That said if you want something similar, feel free to populate a collection with default / 'empty' objects and bind that to your grid. It just means that after working with the data, you will have to define a method to capture only the edited rows. This still means that the column-bound properties of your class need to be known ahead of time.
A DataGrid is used to display a collection. If you want to create an individual row DataGrid is not really the right tool. You could but a single empty row in the DataGrid using a collection of only on row. There is a lot of guidance on Master Detail on MSDN. If you don't know how many columns at design time you could used a DataGrid to turn the row vertical with column 1 as name and column as value so now you have one record but with a collection of fields.
How would I display a running total for a particular column of each record that is displayed in a DataGrid? I'd like to keep the computation logic in the view model.
Desired Output:
Value | Running Total
25 25
10 35
-2 33
Thank you,
Ben
Edit: Keep in mind that when a user-triggered DataGrid sort occurs, the running totals must be recalculated because the record display order has changed.
I'd really like to put the logic for this in the view model, not in WPF code (e.g. not using an IValueConverter).
It is only idea: actually, DataGrid uses ICollectionView interface ( http://msdn.microsoft.com/en-us/library/system.componentmodel.icollectionview.aspx) to implement sorting (as described in http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid(VS.95).aspx at Grouping, Sorting, and Filtering section.
So, you can create class that implements ICollectionView interface, and observe sorting with corresponding updating of running total.
I don't know there is a recommended / supported / simple way to do this. The choices I can think of are:
1) Have some ui element outside of the data grid to hold the total. This is easy to bind to your VM but a bit of a pain to work out the alignment issues that will come up, as you likely want the total right under the column. This is what I would recommend.
2) Have a dummy last row in the grid; this solves the alignment problem but adds hacky logic to the VM, and probably doesn't look so great on top of it.
HTH,
Berryl
If you don't mind the answer in VB or C# then this tutorial shows how to do what you want in VB and this tutorial shows it in C#. Both use a dummy column in the grid.
I ended up solving this problem by extending DataGrid to expose a property which could be bound to a "resort has occured" callback on the view model.
Details: http://bengribaudo.com/blog/2010/07/14/3/datagrid-per-row-running-totals
Check out this answer for how to add a row count column to a ListBox and populate using a value converter. Instead of counting item, add up the sum of items. If you need the total sum in your VM this is of course not a good solution.
Numbered listbox
long cumulative = 0;
List<int> Values = GetValues();
Values.ForEach(v => v.RunningTotal = cumulative = cumulative + v.Value);
You can just run foreach for the whole list and get your running total by the formula
(RunningTotal = cumulative = cumulative + Value);
We need to continously save the previous value.
I got a DataGrid with 6 column, each column is a RadioButton so that user can select 1 in 6 option. There are about 100 row. I make a custom class with 6 bool datamember, then bind the datagrid with a list of about 100 custom class object. There are 2 problem:
The binding time is very slow
When I scroll the data grid, sometime the check value of some row disappear.
Are there any known issues with datagrid when bind to a large amount of data like this? And how to improve the performance. I'm using the June Release of WPF Toolkit
Thanks
P/S: You can download my sample app to understand exactly the problem
http://www.mediafire.com/file/jrmft4m4jwm/WpfApplication3.zip
Have you looked into using a VirtualizingStackPanel? This will improve performance for large collection of data for rendering on screen.
If you can, switch to the .NET 4.0 version of DataGrid.
For me it reduced binding time from 80 secs to <0.5secs on 10000 rows, 10 columns.
Also, make sure to use UI Virtualization (turned on by default, but sometimes it gets disabled)
WPF Toolkit DataGrid scrolling performance problems - why?
First off I have a datagrid on the main form which is populated with about 7000 rows. It is bound (to ItemsSource) in the forms constructor. Everything runs extremely fast and smooth when the app loads (less than 1 sec). However when I want to load that same datagrid in another window it takes > 1 min. There is no difference in the xaml between pages. What could be causing this. I am using Virtualizing Stack panel as well as turning on Cell and Row Virtualization on the DataGrid. I have profile my Sql Server and the query runs in the same time for both calls so it's not a database issue.
I got curious and set up a listview and bound it to the same source on the second form and the same scenario happens as well.
What could be the issue?
Thanks Andrew. I figured it out but I think it's a bug. In my main form the datagrid is in row 2 column 2. In my calling form it was in row 0, column 0. When I made another row and moved the datagrid to it it loaded just like it did in the main form. The datagrid obviously didn't like being in row 0. It works now so I won't be using reflector to look at it.