How to properly manage a dynamic table of data for gui - wpf

So I need to display a DataGrid in WPF. I'm using RadGridView from Telerik and Caliburn.Micro framework using the MVVM strategy.
In my presentation there are differing number of columns, X+2 to be exact as there is always a Label column and a Total column and then X number of others that hold a decimal number.
I do know the number at runtime before I create the classes I need so that could help.
So I'm currently considering a few methods of doing this but some of them have drawbacks so I would love if someone could point me towards a solution.
Create a ViewModel class for each row and for each Cell. The X number will be a collection and binding to it is problematic. I would need to customize the RadGridView itsefl to actually manage this.
Create a DataTable an the run and bind to it. I haven't managed to make this work and not sure if RadGridView actually supports DataTable. This will also not allow me to keep metadata for each cell, something I need to be easily able to update the cells because column + row aren't the only distinguishing keys I need.
Somehow dynamically create a object with x+2 properties to bind to.
Are there any solutions, guides or something out there to help me here?

Related

Dynamic Databinding in WPF

I am filling up a Stackpanel with Textboxes. Each textbox, Should display the Value Property of a Class i have written.
The number of Textboxes is always different so i can't create a databinding in the xaml file.
Are there tutorials, that show how can i archive my goal?
Edit: Thanks for the replies so far. After reading the articles i still not clear how to implement my Problem.
Here is what I'm trying to achieve:
I have an SQL Server DB and there a Table with an ID and a Value.
I now load all Values and IDs i want to display into my application. (number of loaded rows is always differnt).
I need to display a textbox for each row and display the value there and after the value is changed i write the value back to the database.
I don't know how I should query the data and than bind it to the textboxes.
I don't need an implementations, I am happy with every piece of advice I can get.
yes there are a number of tutorials out there. It sound like you are interested in datatemplating.
I would suggest looking Here on MSDN. A few tutorials on databinding in general may be useful DataBinding on MSDN
If your class, classValue, has a public property Value then just create a List and bind that to a ListBox with the item DisplayValuePath = Value. Repeater controls are used to bind to collections.

Creating an Empty DataGrid

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.

Retrieving Cells within a WPF DataGrid Column

I need to get all the cells within a certain column.
there is no property called "Cells" in the GridViewColumn class.
Is there any other way of doing this?
Thanks.
May I ask you the purpose of getting those cells? And when you say you want a cell, do you want the visual of that cell or the data associated with it?
If it is data, then getting it directly from your ViewModel will be a preferred approach. So if you are following proper MVVM while implementing this, you can have a method in the ViewModel which can easily expose you a specific collection of cell-data corresponds to a column
Have a look at http://techiethings.blogspot.com/2010/05/get-wpf-datagrid-row-and-cell.html

Dynamic row and column span in WPF DataGrid

I'm working on a new project where the design calls for dynamic rows and columns displayed in some form of grid. I think I can accomplish this very hackishly using the plain ol' Grid, but it seems like it should be possible using a "real" grid.
In my mockup, I circled the tricky bit in red. Under column 1, I have effectively 2 columns of data. The problem is the fact that my data in column 1 can span multiple rows. And the fact that column one is really two pseudo columns and each one can span a different number of rows independently. Note: the data is fake and inconsequential. I'm open to ideas for a good implementation. I can fall back to the Grid and do this all in code behind, I'm open to purchasing a datagrid that may have more power than the DataGrid and my absolute last resort/preference is to restructure this format to something a little more doable.
Thank you for any ideas you can provide.
Most commercial WPF DataGrid implementations handle hierarchical data like this. For example, Telerik's DataGrid (part of RadControls for WPF) supports hierarchical data in multiple formats.
Typically, the default styling is a bit different than yours above, but can be adapted to that format via styling.

Array binding Xaml by Position on Datagrid

I have a list of string array as input. The array dimension are static for all the list but I can't know the array size until the list is retrieved.
I need to bind the list in a datagrid in Silverlight.
I tried to create columns at runtime, binding each column to a particular array position but cannot find a way.
Have you any idea how to do it?
I could eventually "convert" my list of array in anything else that could be binded in a datagrid?
The idea is to have a sort of 2d Matrix showed inside the datagrid in Silverlight (I think that the problem is similar).
List
column_1 column_2 column_3 .. column_m
string[1,1] string[1,2] string[1,3] .. string[1,m]
string[2,1] string[2,2] string[2,3] .. string[2,m]
string[3,1] string[3,2] string[3,3] .. string[3,m]
....
string[n,1] string[n,2] string[n,3] .. string[n,m]
n is list lenght, m is list column max number.
Any idea is appreciated
I've found two solution to the problem that use the schema in the Denis's answer:
the first one is to use reflection to generate a class at runtime for the binding as suggested in this article (thanks to Vladimir Bodurov). I've tested this solution and I'll try to use it on my project. The only problem right now is that for large collection, the performance are poor. But I hope that someone will fix it in next relese (Silverlight 3 seems to not have fixed this problem, yet)
the second solution will be using some dynamic language for generate data. I don't know if this could be faster or not (probably not) but eventually could help. I will try in the future and use ironpython or ruby to generate classes that will be binded in datagrid.
If anyone have tryed using the second solution or any performance related information about creating classes at runtime, it will be appreciated.
Giorgio
Unfortunately that's not gonna be easy.
Do you have any valid constraints, like the maximum number of columns that is allowed or anything like that?
If you do (let's say you have N column maximum), you might be able to do something by
Having a class that exposes N
properties (named Col1...ColN for
example) that map the content
of the array for one line at column
X
Generating a list of that class,
one instance for each line
Generating the correct number of
column on the fly, binding each
column to property ColX
Binding your DataGrid to that list
That's kind of ugly, but it would work.
If you do not have to rely on the DataGrid, there is a possibility using a UniformGrid. A UniformGrid is a panel that layouts its children in a regular grid (every item has the same width, every item has the same height). You can indicate the number of columns at runtime, and the panel will fit children one after the other up to the number of columns and then continue on another line.
You could bind an ItemsControl to your array, indicating it should use a UniformGrid as its layout panel and indicating a suitable ItemTemplate to render each string.
The second option is much easier, but you will not have the capabilities of the DataGrid like sorting, moving columns, row selection, edition events per row etc.

Resources