15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

1128 ❘ ChaPTer 39 windOws fOrms<br />

If the values in the list are wide, you can change the width of the drop-down portion of the control with<br />

the DropDownWidth property. The MaxDropDownItems property sets the number of items to show when the<br />

drop-down portion of the list is displayed.<br />

The FindString <strong>and</strong> FindStringExact methods are two other useful methods of the list<br />

controls. FindString finds the first string in the list that starts with the passed-in string. FindString<br />

Exact finds the first string that matches the passed-in string. Both return the index of the value that<br />

is found or -1 if the value is not found. They can also take an integer that is the starting index to search<br />

from.<br />

Lastly, the CheckedListBox control is similar to the ListBox; however, the DisplayMember <strong>and</strong><br />

ValueMember properties do not show up in IntelliSense, nor do they display in the property grid.<br />

They are available on the actual control, but an attribute has been added to them to indicate that they<br />

are not used. This is not the case — you can use DisplayMember to determine what will show up on the<br />

user interface, or as an alternative, you can simply override the ToString() method on the objects<br />

you are adding into this list to display whatever you like onscreen.<br />

The datagridView Control<br />

The DataGrid control that has been available from the initial release of .<strong>NET</strong> was functional, but it had<br />

many aspects that made it unsuitable for use in a commercial application — such as an inability to display<br />

images, drop-down controls, or lock columns, to name but a few. The control always felt half-completed,<br />

so many control vendors provided custom grid controls that overcame these deficiencies <strong>and</strong> also provided<br />

much more functionality.<br />

.<strong>NET</strong> 2.0 introduced an additional Grid control — the DataGridView. This addressed many of the<br />

deficiencies of the original control, <strong>and</strong> added significant functionality that previously was available only<br />

with add-on products.<br />

The DataGridView control has binding capabilities similar to the old DataGrid, so it can bind to an Array,<br />

DataTable, DataView, or DataSet class, or a component that implements either the IListSource or IList<br />

interface. It gives you a variety of views of the same data. In its simplest guise, data can be displayed (as in<br />

a DataSet class) by setting the DataSource <strong>and</strong> DataMember properties — note that this control is not a<br />

plugin replacement for the DataGrid, so the programmatic interface to it is entirely different from that of<br />

the DataGrid. This control also provides more complex capabilities, which are discussed in the course<br />

of this chapter.<br />

In this section we use the Northwind sample database. If you don’t already have it installed, please<br />

download the installer from the Web <strong>and</strong> create a database called Northwind in your SQL Server or SQL<br />

Express instance. You can find the installation scripts for Northwind at http://msdn.microsoft.com/<br />

enus/library/ms143221.aspx.<br />

Displaying Tabular Data<br />

Chapter 20, “Threads, Tasks, <strong>and</strong> Synchronization,” introduced numerous ways of selecting data <strong>and</strong><br />

reading it into a data table, although the data was displayed in a very basic fashion using Console<br />

.WriteLine().<br />

The following example demonstrates how to retrieve some data <strong>and</strong> display it in a DataGridView<br />

control. For this purpose, you will build a new application, DisplayTabularData, shown<br />

in Figure 39-2.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!