15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1130 ❘ ChaPTer 39 windOws fOrms<br />

The form consists of the getData button, which when clicked calls the getData_Click() method shown in<br />

the example code.<br />

This constructs a SqlConnection object, using the ConnectionStrings property of the<br />

ConfigurationManager class. Subsequently, a data set is constructed <strong>and</strong> filled from the database table,<br />

using a DataAdapter object. The data is then displayed by the DataGridView control by setting the<br />

DataSource <strong>and</strong> DataMember properties. Note that the AutoGenerateColumns property is also set to true<br />

because this ensures that something is displayed to the user. If this flag is not specified, you need to create all<br />

the columns yourself.<br />

Data sources<br />

The DataGridView control provides a flexible way to display data; in addition to setting the DataSource to<br />

a DataSet <strong>and</strong> the DataMember to the name of the table to display, the DataSource property can be set to<br />

any of the following sources:<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

An array (the grid can bind to any one-dimensional array)<br />

DataTable<br />

DataView<br />

DataSet or DataViewManager<br />

Components that implement the IListSource interface<br />

Components that implement the IList interface<br />

Any generic collection class or object derived from a generic collection class<br />

The following sections give an example of each of these data sources.<br />

Displaying Data from an array<br />

At first glance, displaying data from an array seems to be easy. Create<br />

an array, fill it with some data, <strong>and</strong> set the DataSource property on the<br />

DataGridView control. Here’s some example code:<br />

string[] stuff = new string[] {"One", "Two", "Three"};<br />

dataGridView.DataSource = stuff;<br />

If the data source contains multiple possible c<strong>and</strong>idate tables (such as<br />

when using a DataSet or DataViewManager), you need to also set the<br />

DataMember property.<br />

You could replace the code in the previous example’s getData_Click<br />

event h<strong>and</strong>ler with the preceding array code. The problem with this<br />

code is the resulting display (see Figure 39-3).<br />

Instead of displaying the strings defined within the array, the grid displays the length of those strings. That’s<br />

because when using an array as the source of data for a DataGridView control, the grid looks for the first<br />

public property of the object within the array <strong>and</strong> displays this value rather than the string value. The first<br />

(<strong>and</strong> only) public property of a string is its length, so that is what is displayed. The list of properties for<br />

any class can be obtained by using the GetProperties method of the TypeDescriptor class. This returns<br />

a collection of PropertyDescriptor objects, which can then be used when displaying data. The .<strong>NET</strong><br />

PropertyGrid control uses this method when displaying arbitrary objects.<br />

One way to rectify the problem with displaying strings in the DataGridView is to create a wrapper class:<br />

protected class Item<br />

{<br />

public Item(string text)<br />

{<br />

_text = text;<br />

}<br />

figure 39-3<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!