Do you know if this is possible and if so how to do it?
I have a set of data which will be created at runtime in my view model. Now I want to present these data to the user in a grid which automatically creates columns and rows depending on the number of data items in my data source.
When the window is created it should not generate it all over again - just let it be static as I think that is easiest.
my data source is a class containing a filepath, file name and an image.
Is it possible?
Yes it is possible. You can use Microsoft DataGrid. It has a lot of feature that can be helpful (automatic columns generation, virtualization, columns sorting and reordering...)
Alternatively, you can use a simple ItemsControl and set its ItemsPanel as a Grid, and then implement a custom logic to generate Rows/Columns when ItemsSource changes.
Related
What is the best way to create an empty chart (like a spreadsheet) so that the user can fill the cells with some data (strings, numbers)? The number of rows and columns is not known, it's determined at runtime. The data given by the user should then be easily loaded to, say, 2D array. Is using DataGrid a good solution?
You might want to check
RadSpreadSheet
Infragistic WPF SpreadSheet
DevExpress Spreadsheet
SynFunction Spreadsheet
These are third party control and not free.
This one is open source control.
Creating your own is of course lots of work and you may want to refer to these for a starter.
If you want to make a simple editable DataGrid follow this link.
Also look at IEditableCollectionView
IEditableCollectionView is a new collection view that you can use to supports adding and removing new items, as well as editing items in a transactional way. It is implemented by ListCollectionView (the default view for ObservableCollection) and BindingListCollectionView (the default view for DataTable). I will go through an example to further describe it and summarize afterwards.
In the following link, the author shows how to create a binding source and add sorting function to a grid view. It should work similarly for combobox datasource. However does it have any benefit to do it for simpler controls like combobox or listbox? I can just assign a list of type List<AClass> and then assign the DisplayMember and Value for the controls.
http://aviadezra.blogspot.com/2008/06/binding-domain-objects-in-visual-studio.html
Even for data grid view, does it work well for complex situation like, for example, sorting on a grid view with paging? Looks the class PropertyComparerCollection in the example only works the loaded data.
The article is focused on binding complex business objects to the grid view. This has been a intensively debated subject since .Net Framework 2.0. If the properties of your bound type are no longer primitive, then you have to get your hands dirty to support sorting, filtering or searching.
There are plenty generic classes for this goal, but I would recommend this implementation, with a nice video demo here.
However, if you're not aiming at a data grid view, but rather at a combo box or list box, and wish not to modify the underlying store while sorting or filtering, you could still use a custom class derived from IBindingListView and BindingList(of T), like the one suggested above and manually call ApplySort or ApplyFilter. The implementation in the article linked in your post and the one I suggested use temporary copies of the original data source for sorting/filtering.
Furthermore, for paging instead of taking slices out of the original data source, you'd be taking slices out of the wrapped sortable view of that data source.
I am quite new to WPF and XAML but my background is of ASP.NET and C# so I have a vague idea of how it works.
In .net, I can use the repeater, datalist, gridview, bind to them a DataTable and output from that DataTable. I am now wanting to do the same with WPF.
Basically, I want to display simple records from a database (preferably using a DataTable as I usually work with those). The list might be something like this with two columns
1) Grey TV
2) Red car
3) Blue motorbike
I have looked around but I can't get a definitive answer on what control to use. Some people say ItemsControl and some people say DataGrid. Can anyone help me here?
Thanks in advance.
A DataGrid is used for displaying Table-like data (Per record multiple columns). An ItemsControl is used to display data using your own ItemTemplate, in which you are unlimited in how to represent the items and in what directions or alignments.
Another good useable control for you might be a ListView, which works just as a ListBox except it doesn't have any selection logic. And you can choose between four different ways of displaying your items using the View property (http://msdn.microsoft.com/en-us/library/system.windows.forms.view.aspx).
In your case I would suggest using a ListView.
To bind any items to the control, you have to set the DataContext on the UserControl or the Control itself. And then bind the ItemsSource property to a local List or Collection using the Binding markup extension (http://msdn.microsoft.com/en-us/library/ms750413.aspx). To learn more about data-binding go here:
http://msdn.microsoft.com/en-us/library/ms752347.aspx
I use SQLite3 to read data from a database and then display the results in a listBox, I would to ask if there is possibility to display the results in a listBox as columns in nice show without using DataGrid.
It is better to use ListView instead of listBox.
Also you can still use listbox for multicolumn purpose
Listbox is not designed to work with multiple columns. Any reason why you do not want to use a DataGrid?
In general a grid control is the most appropriate when dealing with items or records having multiple properties or fields to be shown in different columns, some people also use a ListView with the report/details style and several sub items one per each column.
In my experience when the final result is really a table/grid it is easier and simpler to use a grid instead of "forcing" a ListView to behave like a grid. ListView is not designed for that even if it can be used in a similar way.
In one of windows application in C# , I am using Dev Express Grid view control to bind some data and display it to user. I have custom business objects with properties defined for this purpose.Then I simple set the DataSource of the grid to the list of my custom business objects.
A while ago , there came a requirement which means that the columns to be displayed on to the grid will be dynamic. This means I cannot know at design time which fields I will need to display.
I was thinking of abandoning setting the DataSource and populating the grid manually by code. But I think this will cause many of the grid's features not to work properly, for example , grouping the data by drag n drop of fields to the header area etc. Is there any way to tell a grid at runtime to skip certain fields from a list of BO's when databinding to the grid ?
This is pretty simple, we do it all the time. You simply need to bind the grid to your datasource and it will do the rest for you.
Hiding fields is easy also, just set its VisibleIndex to -1
You could do something like this
C#
grid.FocusedView.Columns["Col1"].VisibleIndex = -1;
VB
grid.FocusedView.Columns("Col1").VisibleIndex = -1;