Populating a DataGridView with a DataTable

The DataGridView control is a simple way to display data in a tabular format in a Windows Forms application. This example populates a DataGridView control with data from an SQL query using the DataTable class.

Getting Started

Create a new Visual C# Windows Forms Application project in Visual Studio.
– Drag a DataGridView object from the Toolbox to the blank form in the form designer.
– Drag a Button from the Toolbox to the form.

Right-click on Properties for the Form, DataGridView and Button to set basic properties, e.g.:
– Give the Form a title by setting the Text property for Form1.
– Set the Text property of the button to “Close”.
– Go through the properties of the DataGridView and choose the properties to change. For example I changed the Name to DataGridView1, set AllowUserToAddColumns to True.

Add code to close the form

Not strictly necessary since you can exit the program by clicking on the X, but double-click on the Close button in the form designer and add some code to close the form:

private void closeForm_Click(object sender, EventArgs e)
{
// close the form
Close();
}

Add code to populate the DataGridView

 

The DataGridView has an option to choose Data Source – this allows you to create a CRUD application without writing any code. In this case I’ll choose to use the SQLDataAdapter class to fill the grid instead:

Add:

using System.Data;
using System.Data.SqlClient;

Add a call to a new LoadData function to the form constructor:

public Form1()
{
InitializeComponent();
LoadData();
}

Create the LoadData() function. Note how the Fill function from the SqlDataAdapter class is used to fill a DataTable object, which is the then associated with the DataSource property of the DataGridView:

private void LoadData()
{
SqlConnection conn = new SqlConnection();
try
{
// construct a connection string to the database
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = “.\\MYINSTANCE“;
builder.InitialCatalog = “msnotes”;
builder.IntegratedSecurity = true;
conn.ConnectionString = builder.ConnectionString;

// Connect
conn.Open();

using (SqlDataAdapter myAdapter = new SqlDataAdapter(
“SELECT category Category, note_data Note FROM categories, notes ” +
“WHERE notes.note_cat = categories.catidx”, conn))
{
// Use DataAdapter to fill DataTable
DataTable myTable = new DataTable();
myAdapter.Fill(myTable);

            // Render data onto the screen
dataGridView1.DataSource = myTable;
}
}
catch (SqlException e)
{
MessageBox.Show(“Error accessing database: {0}”, e.Message);
}
finally
{
conn.Close()’;
}

Resources

My new favorite site for programming tips, and a reference source for this example is Dot Net Perls. I like the way it provides small self-contained tutorials demonstrating how to solve a specific problem or use a particular language feature or control.

Advertisements
This entry was posted in Databases. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s