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.

st<strong>and</strong>ard Controls <strong>and</strong> Components ❘ 1129<br />

figure 39-2<br />

This simple application selects every record from the Customer table in the Northwind database <strong>and</strong><br />

displays these records to the user in the DataGridView control. The following snippet shows the code for<br />

this example (excluding the form <strong>and</strong> control definition code):<br />

using System;<br />

using System.Configuration;<br />

using System.Data;<br />

using System.Data.Common;<br />

using System.Data.SqlClient;<br />

using System.Windows.Forms;<br />

namespace DisplayTabularData<br />

{<br />

partial class Form1: Form<br />

{<br />

public Form1()<br />

{<br />

InitializeComponent();<br />

}<br />

private void getData_Click(object sender, EventArgs e)<br />

{<br />

string customers = "SELECT * FROM Customers";<br />

using (SqlConnection con =<br />

new SqlConnection (ConfigurationManager.<br />

ConnectionStrings["northwind"].ConnectionString))<br />

{<br />

DataSet ds = new DataSet();<br />

SqlDataAdapter da = new SqlDataAdapter(customers, con);<br />

da.Fill(ds, "Customers");<br />

dataGridView.AutoGenerateColumns = true;<br />

dataGridView.DataSource = ds;<br />

dataGridView.DataMember = "Customers";<br />

}<br />

}<br />

}<br />

}<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!