Tuesday 5 May 2009

Simple WPF Datagrid Example

Simple WPF Datagrid Example

Really love to use WPF data grid was looking for such control for long time still working on but just implemented a small example which might others to help out in their projects

First of all need to define namespace for Datagrid

After that just write XAML code for Datagrid

<dg:DataGrid x:Name="PersonsDataGrid" AutoGenerateColumns="True"

AlternationCount="2"

ClipboardCopyMode="IncludeHeader"

IsReadOnly="True"

AreRowDetailsFrozen="True"

AlternatingRowBackground="AliceBlue"

Margin="5”>

dg:DataGrid>

Now the design is ready if you run your WPF application you will the grid but without any data it’s time to bind this grid with data.

Define list of Person object

public List<Person> result = new List<Person>();

In Window_Load fetch the data :

result = GetAllPersons();

Now bind with Datagrid:

PersonsDataGrid.ItemsSource = result.ToList();