tmutton
tmutton

Reputation: 1101

WPF - Display db records into a list

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

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.

Upvotes: 2

Views: 1845

Answers (1)

Kolky
Kolky

Reputation: 2977

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

Upvotes: 2

Related Questions