ListView or DataGridView - c#

Being new to WPF I am looking for guidance on which control would be best to use for the following scenario.
I need to create an application which displays two lists side by side. The list on the left displays a list of users. The list on the right displays a list of permissions.
I want to be able to dynamically add/remove a column of check boxes indicating the user permissions to the right hand permission list by clicking/dragging users from the left hand list. The number of user permission columns will be from 0 - total number of users. The check box should bind to the underlying data i.e. update the users permission.
Which control should I use to achieve the right hand permission list, a ListView or DataGrid?
I have something working by using a ListView and creating an attached property using the following answer as a guide WPF MVVM: how to bind GridViewColumn to ViewModel-Collection? but this doesn't render a checkbox only true/false for the permission status. Looking into how to dynamically add a CellDataTemplate to render a checkbox has made me question whether I should be using a DataGrid.
If I was to use a DataGrid how would the dynamic addition of columns work?
I would like to keep the code behind to a minimum if at all possible.

At first glance on your question, I thought a Datagrid would be a better choice. I'm a vb.net guy but I think the syntax would be very similar
To add columns at runtime the code behind looks like this
DataGridView1.Columns.Add("Column Name", "Column Heading")
You could always just do a google search on "Adding columns to a DataGrid at runtime in C#" and that should return a bunch of examples.

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 C# - Complex Data Binding

I have a very complex databinding I want to accomplish here using the below:
2 SQL CE tables named mainTable and secondaryTable
1 Fluidkit ElementFlow control named myElmntFlow
2 UserControls named myUsrCtrl and otherUsrCtrl
All of the above are already created and implemented but the UserControls are populated into the myElmntFlow control's items list programmatically through lenghty backgroundworker code that does take a good bit of time to run when the number of items to enter is > 20.
This is how they get created as of now:
The backgroundworker loops through each row of mainTable and adds the myUsrCtrl control to the list of items in myElmntFlow if the value of the row in column "Selected" = "'Yes'".
Then, it modifies the content of the newly added myUsrCtrl as such: it adds a otherUsrCtrl into the myUsrCtrl's stackpanel (named stckPanel) for each row in secondaryTable where the value of the column "FullName " = the value of the same column of the mainTable row we used to created the myUsrCtrl control.
And then populates the otherUsrCtrl's sevaral labels with the value of the secondaryTable row looked at at the moment.
Very confusing but it is a complex scenario. Let's use an example:
In mainTable, the row #4 has the FullName value of "Chad Jones" and
also has the Selected value of "Yes".
A new instance of the myUsrCtrl control is added to the
myElmntFlow's list of items as such:
myElmntFlow.Items.Add(myUsrCtrl);
The newly added myUsrCtrl control has a stackpanel (stckPanel)
We filter the secondaryTable where the FullName = "Chad Jones"
For each row in the now filtered secondaryTable, we add a new
instance of otherUsrCtrl to the previously created myUsrCtrl's
stckPanel control
The different labels in the otherUsrCtrl are populated with the values of the
row in secondaryTable
Can this possibly be converted into a DataBinding within the XAML of the controls as I want to implement several features later on (such as a nice SearchBox with autocomplete) that would be quite poor if they were to be coded behind by writing hundreds of line to tell which data to filter, sort , take , compare etc...
I wrote this as clearly as I could, just hope it's understandable.
PS: I would like to keep my SQL structure as the data is going to become quite consequent over time and I believe that the SQL is the way to go when manipulating thousands of lines.
It's not a very confusing scenario, it's just made confusing by the complex handling that goes on there. It can indeed be made much easier using bindings and MVVM (Model-View-ViewModel) so please take some time to read about the basics of that. There are a ton of tutorials and introductory materials on the web, a simple search will give you more than enough to go on.
When you're comfortable with the concepts, all you need is to transform the data into a sequence of objects (no matter how you go about it), then use an ItemsControl to represent the UI for a list of items. Use DataTemplates to specify how each item should be displayed, binding elements in the DataTemplate to the properties of each item. These things can be nested so you can have ItemsControls in your DataTemplates which use other DataTemplates etc.
In order to represent a collection of items bound to an ItemsControl look at using an ICollectionView which will help tremendously with filtering/sorting/etc.
Sorry about the very broad strokes but it is a pretty broad question. If you need more specific help I'll gladly add more.

Cloning the WPF ComboBox control: Ideas or suggestions

If have a control that acts like a record selector from a database, say, for example, customers.
The control must act in certain ways allowing the user to type the name, the alias or the code of the customer, and the control will select the correct one, or offer a list of possible candidates, and other behaviors.
I have tried to inherit from ComboBox, but there are some ComboBox behaviors that make it difficult or impossible to do what I want, so I'm better starting from scratch, with a TextBox and Button.
The questions are:
Do you know some open-source component so I don't have to start from zero?
Have you already done something like this and want to share methodologies or tips?
Am I good with a TextBox, a Button and a PopUp control?
Try this control from CodeProject: A Reusable WPF Autocomplete TextBox
It allows you to specify a source list, and a custom filter. In your custom filter you can select to show only those records that have a name, alias or code that fit the text entered, and the user will have a list of filtered options displayed to them.

Paged Custom ComboBox

I would like to have some sample code on how to do a paginated ComboBox.
My data consist on 1300+ items. When the user clicks the combo box arrow, the combobox will display display 25 (page size configurable) items at the time with arrows up/down (depending on page location) so that the user can request the previous/next page.
The data is coming from a generic list.(List)
Thus, the idea is to display only a subset of the data at the time.The user can scroll and select from the list as per normal combobox. At the top and bottom of the list should be a new button to request the previous or next page of navigator values.
Note: All data is read only. For legacy issues I can only use Winforms (.net 2.0) and C# but VB.net code will do as well
you might want to consider using a treeview as a dropdown control for your combobox, smth like is done here:
you can get source code for this control here: ComboBox control with a TreeView I guess it should give an idea on how to proceed with your task
regards

Simple question - no results (ListView Columns)

I'm trying to accomplish what should be a very simple task using the ListView Control. All I am trying to do is add two Items to a ListView Control. One Item goes under the "Title" Column and the other Item goes under the "Status" Column. I have seen many examples for the ListView Control and none of them cover this particular need.
I've read the MSDN documentation on the ListView Control and find it rather odd they don't mention this... Or maybe I've overlooked it?
In ListView parlance, these are not separate items. It sounds like you want to add one item, and then what ListView calls a subitem. Assuming that Title is your first column and Status your second, you want:
ListViewItem myItem = listView.Items.Add("My Item's Title");
myItem.SubItems.Add("My Item's Status");
If you are just getting started with a ListView, you can save yourself a lot of time, headaches and frustration by using ObjectListView -- an open source wrapper around .NET WinForms ListView.
It solves many problems that you are going to find while trying to use a ListView, as well as just making it much easier to use. Just one example: it automatically handles sorting rows by clicking on column headers.

Categories