BLOGS BY TOPIC▼
BLOGS BY AUTHOR▼
BLOGS BY YEAR▼
We think - cautiously - that WPF is a better platform for client tool development than Windows Forms. This blog gives 10 reasons why.
- 10 reasons why WPF is better than Windows Forms
- WPF Forms are Quicker to Create
- Flow Layout Trumps Absolute Positioning
- WPF Works for Websites
- Styles are Much Better in WPF
- WPF is (I think) the Way of the Future
- Better Data Binding (this blog)
- Selecting Controls in the Document Outline Window
- Triggers
- StoryBoards and Animations
- Drawing!
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:

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)
cn.Open()
'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
cn.Close()
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);
try
{
// 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)
cn.Open();
// 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)
{
MessageBox.Show(ex.Message);
}
finally
{
// close the connection whether went wrong or not
cn.Close();
}
}
In both cases, you set the data context for the gridview. The gridview itself can then pick up on this bound table:

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.
- 10 reasons why WPF is better than Windows Forms
- WPF Forms are Quicker to Create
- Flow Layout Trumps Absolute Positioning
- WPF Works for Websites
- Styles are Much Better in WPF
- WPF is (I think) the Way of the Future
- Better Data Binding (this blog)
- Selecting Controls in the Document Outline Window
- Triggers
- StoryBoards and Animations
- Drawing!