WPF - Display db records into a list - c#

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

Related

Can I format one listBox to show some columns

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.

Share listbox across controls/pivot items on WP7

I was wondering if there was a way to share a listbox on the same page.
Basically, I have a listbox that is bound to a collectionviewsource. I also have 5-6 pivot controls that will use the same listbox, but will filter the collectionviewsource differently.
I was wondering if it was possible to say have a listbox as a resource and display it on all hte pivots then when the pivot changes, I can apply a different filter to the collectionviewsource.
Can anyone steer me the right way?
Thanks!
You could create an ItemTemplate for the Pivot which includes the listbox.
I find it easier to maintain this scenario by explicilty creating each pivotItem in XAML but having all teh listboxes share the same ItemTemplate though. I find it easier to see what's going on by just looking and it also makes it easier if you ever need to change the template of one of the listboxes. (I find this seems to happen quite often.)

How do I display two tables in a data grid/tree view showing columns for both the parent and child tables (when child expanded)

I'm trying to use a TreeView/DataGrid to display some data. I need to display columns for the top level items, but also display a header for the expanded level.
I have a dataset with two tables, e.g. Orders and Items, items has a foreign key to Orders.
I'm trying to bind the dataset to the datagrid/tree view so that I show the list of orders.
The WinFrom DataGrid can show multiple tables from the DataSet:
Which I set with:
dataGrid.DataSource = dataSet;
dataGrid.DataMember = "Orders";
Clicking the [+] expands the row to show the link:
Following that link brings up the Items Table:
What I'm after is a mix of both but in WPF:
I've used a dataset for this example but I can massage my data into collections etc that WPF can use to display the data in the way I'm trying to achieve.
I've looked at the TreeListView which gives me the top level item headers, but I'm scratching my head trying to get the expanded items header to be shown. Could someone point me in the right direction, thanks.
Update I've come back to looking at this issue. As per #offapps-cory's answer I tried using a ListView with expander with a listview. 1st attempt:
As you can see, it expanded in the cell :) I'm thinking maybe I can throw a treeview into the mix...
Could you instead use a ListBox with ListViews nested in the items? While I have not had ListViews nested, I have nested ListBoxes several levels deep to achieve some functionality that I needed.
I originally thought that one might do as you are asking by customizing the TreeViewItem template and the ItemsPresenter, but ItemsPresenter is expecting a Panel, and ListView is a control.
I would try creating a DataTemplate with a ListView/GridView in it and an expander. You may need to do some funny databinding to get it working, but I don't see why it wouldn't work.

How do I access the controls in a WPF DataGrid

In good old (well!!) WinForms days the datagrids row used to the be the actual control and you could then access the DataItem.
In WPF its all flipped and dataGrid.Items is just the source data.
I am probably doing this the wrong way round as im a bit of a WPF newb but how can I iterate through the rows of my gridview grabbing the values from certain labels, textboxes etc?
Yes, you are doing this the wrong way round. What you should be doing is iterating through the items in your data source - it's where all the values are, after all.
It's possible to iterate through the WPF objects, but it's not trivial. And there's a significant problem that you'll run into if you try.
You can use the VisualTreeHelper class to search the visual tree and find the DataGrid's descendant objects. If you play with this long enough, eventually you'll figure out how to find the specific controls you're looking for. But the DataGrid (actually, the VirtualizingStackPanel in its control template) virtualizes its visual children. If an item hasn't appeared on the screen yet, its WPF objects haven't been created yet, and you won't find them in the visual tree. You may not be able to find what you're looking for, not because you don't have a way of finding it, but because it doesn't exist.
If you're using value converters and formatting in your bindings (which is the only reason I can think of that you'd want to look at the WPF objects and not the underlying data items), I'm afraid the answer is: don't do that. Do the value conversion and formatting in your data source, and expose the results as properties that can be bound to directly.
It's certainly possible to use WPF without using the MVVM pattern. But this is the kind of brick wall that you can run into if you don't.
You can use this
public DataGridRow TryFindRow(object item, DataGrid grid)
{
// Does not de-virtualize cells
DataGridRow row = (DataGridRow)(grid as ItemsControl).ItemContainerGenerator.ContainerFromItem(item);
return row;
}
where item represent the data displayed on the row.
Hope this helps.

Setting the datasource of a WPF Grid

I'm very new to WPF, and am trying to set the datasource (which the WPF Grid doesn't have as a property) of my grid to take a List. Does anyone have any code examples of how to do this. I have googled it, but can't find any really good examples.
(Oh, and can anyone suggest a good site for all round WPF Code examples?)
Thanks
If you're referring to a WPF Grid, you can't bind it to data; it's meant for layout purposes only; you might want to look into one of the controls that inherit from ItemsControl, such as ListView.
The property you'd bind your list to, is called ItemsSource.
The other control you might be thinking of is the GridView
There's also the DataGrid (Note old link) in the WPF Toolkit which implements a lot of the same functionality as the WinForms DataGridView

Categories

Resources