COVID-19: Choose between our familiar (but now socially distanced) classroom training courses and our excellent new live online courses.
Why you should develop systems in WPF, not WinForms
Part seven of a eleven-part series of blogs

Posted by Andy Brown on 25 September 2012

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

6 - Better Data Binding

The thing I disliked most about WinForms was getting data to display, particularly in DataGridViews.  By contrast, WPF is much easier (although still not trouble-free).

Binding Data to a DataGrid in WPF

WPF for the .NET 4.0 framework (the latest version at the time of writing) includes a DataGrid control, which allows you to list records:

DataGrid listing films

This datagrid is listing films from an underlying SQL Server table.


WinForms has an equivalent DataGridView control, but the binding seems much easier in WPF.  All you need to do is to set a binding context for the window when you open it.  Here's how to do this in VB:

Private Sub winGrid_Loaded(sender As Object, _

e As System.Windows.RoutedEventArgs) Handles Me.Loaded


'create a connection to database

Dim cs As String = ConfigurationManager.ConnectionStrings("csMovies").ConnectionString

Dim cn As SqlConnection = New SqlConnection(cs)


'create a new dataset

Dim ds As DataSet = New DataSet()


'open the connection (not strictly necessary, as the

'data adapter will do this when you use it anyway)



'fill data tables

Dim daMovies As SqlDataAdapter = New SqlDataAdapter()

daMovies.SelectCommand = New SqlCommand("SELECT * FROM tblFilm", cn)

daMovies.Fill(ds, "Films")


'now set the data context for the entire window

Me.DataContext = ds



End Sub

If you must use C#, here's the equivalent code:

private void Window_Loaded(object sender, RoutedEventArgs e)


// create a connection to Top Trumps database

String cs = ConfigurationManager.ConnectionStrings["csMovies"].ConnectionString;

SqlConnection cn = new SqlConnection(cs);




// create a new dataset

DataSet ds = new DataSet();


// open the connection (not strictly necessary, as the

// data adapter will do this when you use it anyway)



// fill data tables

SqlDataAdapter daMoviess = new SqlDataAdapter();

daMoviess.SelectCommand =

new SqlCommand("SELECT * FROM tblMovies WHERE PackId=1", cn);

daMoviess.Fill(ds, "Movies");


// now set the data context for the entire window

this.DataContext = ds;


catch (Exception ex)






// close the connection whether went wrong or not




In both cases, you set the data context for the gridview.  The gridview itself can then pick up on this bound table:

Gridview in XAML with binding

The gridview's XAML - the item source is the window's Films table.

Two-Way Data Binding

Although reacting to gridview events is still complicated in WPF, it's made easier by the fact that you can get at the bound object.  For example, here's some code to determine which row a user double-clicked on (in C# only, this time):

private void DataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)


// find out the row clicked on

DataRowView dr = dg.SelectedItem as DataRowView;


// get the field values for this row

string FilmName = (int)dr["FilmName"];

int FilmOscarWins = (int)dr["FilmOscarWins"];


// display choice made

MessageBox.Show(FilmName + ' ' + FilmOscarWins.ToString());


When a user double-clicks on a gridview, this turns the selected row into the source data row, and then gets two fields from it.

This blog has 0 threads Add post