Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
View Relational Data in a DataGrid
WhatsApp
Mahesh Chand
4y
33.3k
0
2
100
Article
ViewDtInDtGridMCB.zip
This article explains to you how easy is to read a database and displaying in a grid using DataSet.
In this sample example, I have used the access 2000 database,
Northwind.mdb
. We access the Customers table of the 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 a 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 the following figure.
ADO.NET
Attach DataSet to DataGrid
Create OleDbDataAdapter Object
DataGrid
Up Next
Ebook Download
View all
Printing in C# Made Easy
Read by 22.3k people
Download Now!
Learn
View all
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.
Membership not found