Windows Search (WDS 4)
Windows Search (WDS 4)
Written by Mike James   
Monday, 22 February 2010
Article Index
Windows Search (WDS 4)
Getting Started
Making the connection
Binding to a grid
Binding in WPF
More properties
Scope and content

Binding to a grid - Windows Forms
(Visual Studio 2008)

We can display the data returned by the query most simply by using data binding and a data grid. While the general principles are the same the details differ for Windows Forms and for WPF. This section explains how to use the Windows Form's DataGridView and the next section deals with WPF's new DataGrid.

The joy of ADO .NET is that it is a complete system designed to allow you to work with databases without having to reinvent everything from scratch.

For example, now that we have a DataSet we are most likely going to transfer all or some of the data to some sort of grid control so that the user can view it and perhaps modify it. Transferring data to and from a grid isn't difficult programming, but it is very tedious programming.

The DataGridView does away with all of the tedious programming by automating the transfer. If you open the Form Designer and place a DataGridView on the Form you can "bind" it to a data source. If you click on the control's "Task List", the small arrow icon in the top right-hand corner you can select a data source from within Project Data Sources and in this case select Table within SearchResults.

This causes the DataGridView to display a single column and a dummy row corresponding to the definition of Table within SearchResults.

You should also notice that a DataSet control called searchResults and a BindingSource control have been automatically added to the form – don't worry about them they are just part of the automatic plumbing that transfers the data. The only minor complication is that now the DataSet object that you have to use in the program is called searchResults and you don't have to instantiate one manually. So to make the binding work remove:

 SearchResults SearchData = 
new SearchResults();

and remove any references to SearchData in the program.


 SearchAdpt.Fill(SearchData) ;

to read:

 SearchAdpt.Fill(searchResults) ;

Now when you run the program and click the button you will see the grid populate with the result of the search without you having to do anything extra – this is how programming should be!

Binding a DataGridView to a data source is as easy as picking from a list 

Binding to a Grid - Windows Forms
(Visual Studio 2010)

The only change needed to create a Windows Forms version of this program using Visual Studio 2010 or Express is that instead of dragging a DataGridView to the form from the Toolbox you can use the Data Source window to set a default control to be bound to the dataset and simply drag the table to the form. This works in the exactly the same way as described for a WPF project in the next section.

The code given for the Windows Forms project in the previous section works as before - only the method of creating the controls has changed.





Last Updated ( Sunday, 21 February 2010 )

RSS feed of all content
I Programmer - full contents
Copyright © 2018 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.