WPF MVVM Creating rows and columns during runtime - c#

I create application, where user creates alternatives and criteria. My alternative should have list of criteria, so user can give value for particular criteria in alternative. I would like to present this as a matrix, where rows are alternatives and columns are criteria. User during runtime can create new alternatives and criteria. Both are created using dialogbox, additionally presented in ListView. I store them in ObservableCollection.
My problem is, that DataGrid control doesn't offer easy way to add columns. I look for a solution to my problem, where I could create during runtime both columns and rows. Maybe DataGrid is wrong control to use in this scenario. I found some solution, but it doesn't feel right to me. All data, that user inserts, will be later used in solver. Maybe DataTable is way to go?
example: https://svitla.com/blog/grid-with-dynamic-number-of-rows-and-columns-part-1

Related

Dynamically creating Tab+DataGrid for binding with multiple different classes?

I apologize for the crappy title, I wasn't quite sure how to summarize what I'm trying to do.
My scenario:
I am refactoring an existing WinForms application to WPF and MVVM. I'm currently working on a module that provides search functionality for a database comprised of many different tables such as Contact, User, Case, Product, etc. In code-behind there are classes which provide an Object for each. I have written wrapper classes for each of the searchable table Objects that expose only the properties a user would want/need to see in the search results for each type of Object, for binding to a DataGrid.
Once these search results exist, they need to be displayed in a combination of Tab Controls and Data Grids, like so:
Because of some use cases, I need to be able to create an individual display Tab+DataGrid for the results of every single search that is performed. I cannot have a single tab for each type of search that is shown/hidden as needed.
I'm not sure how to proceed from where I currently am to the goal pictured and described above. Thanks for any help anyone can provide.
Not sure I entirely understand your question, but it looks to me that it might be a candidate for datatemplateselector.
Basically, you use an ItemsControl bound to your result collection and then - using a datatemplateselector - you swap in the appropriate template to display the item based upon a code inspection.
That will let you present all the results in a single list.
If you want to display your results in a tabs, I would present a collection of each result type in your viewmodel. So you have a Users collection and a seperate Products collection. Then you can bind individual data grids to each template.
If you want to then hide the tabs when no results are present, add a data trigger using the expression.interactivity namespace to trigger the visibility of each tab page based on its respective collection count.
One more thing, if you want to create tab items dynamically, i.e. One tab for each search - the tab control has an ItemSource property. If you group each search result into an object an expose an observable collection of that object, you can bind that to your tab control and have it create tab items for each search result. Just make that object contain a collection of actual results and you should be able to create a itemscontrol as mentioned here already.
Sorry if this appears a bit of a mind dump, but like I said - not sure if I entirely get the question :)
Rather then chuck a load of code snippets in, there a plenty of samples just a google away if anything sounds helpful.

WPF window that looks like excel

I am new to WPF and c#, and I am trying trying to create an excel like table with a fixed number of columns and a varying number of rows according to the user's needs.People advise to use a datagrid, but I am very confused, some say that it is used mostly to display data source content (which is data contained in database if I understood). I read also about listviews, binding things to itemsource...etc. This is really a lot of information to work with! What I simply need is a way to create a table with fixed columns, and adding rows automatically when the user clicks on a button, that's it! No binding, or anything of this sort.But how to achieve that? Also, if you have good websites tutorials for working with datagrids, I would be very grateful (most of those that I found are too much complex, or don't explain well).
Thanks a lot!
You can create datatemplates that will style your data to look however you like - in this case an Excel row. Then you can display this data as the ItemsSource in an items control. Since you want the number of rows to vary based on some criteria, your data should be in an ObservableCollection. I'm not sure how you'd set-up the header, but I think you could style-up some containers and bind their width properties to the datatemplate controls.
If you wish to do more cell level customizations, then you can try Grid
http://www.syncfusion.com/products/user-interface-edition/wpf/grid/grid-control

Generating results table with custom rows (including pictures)

This is more of a design question. I am working on a website and want to create a table for displaying search results. The table will display various items that are for sale. Each row will have basic information about the item (name, various specs, and an image). Each row will be clickable so that the item's detailed page can be displayed.
I know of a few ways I could do this, but I was wondering if there is a "best practice" to handle this sort of thing.
I have an "item" object that contains all the info I want to display. Is there an easy way to create a graphical object that I populate with info from the result set and then bind these rows to my table?
Sorry if this is too broad, but I am not particularly skilled with HTML yet and want to make sure I learn the proper way to do things like this.
I would look up the Repeater control which lets you bind data to custom HTML in a rather flexible way. There are higher-level controls like the DataGrid which can be very efficient to use in terms of programming time if your problem matches its structure, but in general I prefer the more light-weight approach of the Repeater.

How to approach data-binding a BindingList of Lists to a DataGridView in C#?

I'm writing a program that functions as an Excel-style dictionary. Basically, it allows the user to add rows, edit rows, search through them, and so on. I use it for storing and studying vocabulary for foreign languages.
I've gotten a version up and running that I'm quite happy with. It uses a BindingList as a data source for a DataGridView in order to track changes and record them back to the BindingList that I use to store all the vocabulary, and the list itself is made up of a custom class I named "Term", that has properties for "English Word", "Spanish Word", "Examples", ect. What it doesn't do is let the user customize the fields, and that's where my problem comes in. It's pretty much "hard-coded" in that even if I'm studying Spanish or French, the Term class is going to be using the property for "Kanji" from Japanese.
I want to be able to have the user type in what fields they want the dictionary to display and keep track of--basically, they should be able to rename and add/remove columns from the DataGridView. My first thought was to implement this as a List, which stores the names of the fields (and accordingly the number of them, by using the List's length). Then, I would have a Word class that has a List property, and each string in the list represents one of the fields. Then I create a BindingList of this Word class, which leaves me with a BindingList of Lists.
When I try to databind my List list to my DataGridView, the grid comes up empty--it apparently has no idea how I want the data to be displayed and I'm having great difficulty figuring out how to tell it to. I'm not even sure if my approach of having a List of Lists is a good way to implement customizable fields, but it's the best I could think of. In any event, can anyone recommend a way to approach this that lets me add the fields to the table, but also tracks changes and pastes them back to the original source? I need the grid to be used as an editing tool for the user to not only add new elements, but also change existing ones.
It's a personal project, but it's driving me a bit crazy. I was up until 5AM last night trying to figure it out and came up empty-handed. Thanks very much for reading!
I've read your post a couple of times. I'm not sure I understand completly. If I don't, please give some details and I'll try to help.
If I had to do a Excel-like DataGridView, I think I'd use an Array. I would create an array of, say, 256 by 256 and put it as DataSource. Then after the user edits, you read the whole DataGrid and rewrite if it differs from the array you originally had.
I think you might be interested in this class:
http://www.codeproject.com/KB/grid/DGVColumnSelector.aspx
It allows the user to dynamically display which columns are shown in the DataGridView

Nested Data Tables: They said it can not be done?

I have been trying unsuccessfully to do something that I would have thought in WPF should be quite easy.
We wanted to display a table in a cell of another table recursively (that can be expanded / collapsed). (Not Master / Detail, but true tree like recursion into the sub-tables)
We thought maybe a DataGrid or a GridView might be able to achive this but we've been unsucessful in our attempts, so I wanted to check with the world at large that we're trying to do something possible.
If it helps, it's readonly data, but it's all data-driven so there's a dynamic number of rows and columns.
Look into the DataGrid's RowDetailsTemplate. You should be able to add whatever you want in there including another Grid.
You need a hierarchical data grid, evaluate Infragistics

Categories