Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
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
Display data in a Data Grid
WhatsApp
Mahesh Chand
4y
220.8k
0
5
100
Article
After writing programs for 5 years, you have ever try to look at your first program when you start learning simple programs in Pascal or C++?? I wrote this simple program in the early days of C# and ADO.NET.
In Visual C#, the DataGrid is one of the ToolBox controls. DataGrid can be used to display data from a database by writing only a few lines of code. In this article, I have used a database called "C:\\mcb.mdb" which has a table called 'Student'.
Create a Windows Application and add DataGrid to the Form
Add a DataGrid control to the form and set properties according to your needs.
Adding Source Code
Now you can add this few lines of code anywhere you want to load the data from the database. It may be mouse button click or the Form load event handler.
private
void
Form1_Load(
object
sender, System.EventArgs e) {
OleDbDataAdapter da =
new
OleDbDataAdapter(
"SELECT * FROM Student"
,
"Provider=Microsoft.JET.OLEDB.4.0;data source=C:\\mcb.mdb"
);
DataSet ds =
new
DataSet();
da.Fill(ds,
"Student"
);
dataGrid1.DataSource = ds.Tables[
"Student"
].DefaultView;
}
Simple. I create an OleDb DataAdapter and a DataSet object and call the Fill method of data adapter, which fills the data set. After that, I bind the data set with the DataGrid using the DataSource property. The contents of DataGrid looks like the following figure.
How to Run?
Download the database zip files and unzip them.
Add using System.Data.OleDb; namespace in your project. Change the database path in this string. "Provider=Microsoft.JET.OLEDB.4.0;data source=C:\\mcb.mdb");
Run the application.
ADO.NET
bind datagrid
DataGrid using DataSource property
DataSource
Display data in a Data Grid
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