ARTICLE

View Relational Data in a DataGrid

Posted by Mahesh Chand Articles | ADO.NET in C# March 15, 2001
This article explains you how easy is reading a database and displaying in a grid using DataSet.
Reader Level:
Download Files:
 

This article explains you how easy is reading a database and displaying in a grid using DataSet.

In this sample example, I have used access 2000 database, Northwind.mdb. We access Customers table of northwind database and display records in a datagrid control.

Starting: Create a Windows Application type project and add a DataGrid control to the form. Leave DataGrid name as DataGrid1.

Add Reference to the Namespace

First thing you need to do is add reference to System.Data.OleDb namespace since I will use OldDb data providers. if System.Data namespace is not present, you might want to add this too.

using System.Data.OleDb;

Create OleDbDataAdapter Object

Now you create a OleDbDataAdapter object and connect to a database table.

// create a connection string
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Northwind.mdb";
OleDbConnection myConnection = new OleDbConnection();
myConnection.ConnectionString = connString;// create a data adapter OleDbDataAdapter da = new OleDbDataAdapter("Select * from Customers", myConnection);

Create a DataSet Object and Fill with the data

You use Fill method of OleDbDataAdpater to fill data to a DataSet object.

// create a new dataset
DataSet ds = new DataSet();
// fill dataset
da.Fill(ds, "Customers");// write dataset contents to an xml file by calling WriteXml method

Attach DataSet to DataGrid

Now you use DataSource method of DataGrid to attached the DataSet data to the data grid.

// Attach DataSet to DataGrid
dataGrid1.DataSource = ds.DefaultViewManager;

Here is entire source code written on the form load method.

private void Form1_Load(object sender, System.EventArgs e)
{
// create a connection string
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Northwind.mdb";
OleDbConnection myConnection = new OleDbConnection(); myConnection.ConnectionString = connString;// create a data adapter OleDbDataAdapter da = new OleDbDataAdapter("Select * from Customers", myConnection);
// create a new dataset
DataSet ds = new DataSet();
// fill dataset
da.Fill(ds, "Customers");// write dataset contents to an xml file by calling WriteXml method
// Attach DataSet to DataGrid
dataGrid1.DataSource = ds.DefaultViewManager;
}

The output of the program looks like following figure.

data_v1.gif

Login to add your contents and source code to this article
post comment
     

Iam trying to do something similar to the example given here. The only difference is I want to add a checkbox to the grid as the very first column which is not there in the database table I use.

 

So I changed the select query to 'Select 'Y', a1, a2 from t1' ; then added gridtable styles and column styles but whatever I do, it just displays the Y as such as a text column and the a1 and a2. When I built my column styles, I also set the readonly property to true for all columns except the bool column, but it stll allows me to edit the columns.

 

Can you please let me know what you would do if you had the requirement of adding a checkbox in the above example where the checkbox column is not part of the customers table?

 

Thanks a lot for your help. I have been trying out things from various examples, but none seem to work.

Regards!

 

Posted by New ToCS Dec 15, 2005
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.