.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Sandeep Singh
Melody Anderson
Eminent IT

Home >> Code Snippets >> DataGrid >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Showing the data in a Datagrid from the database

Posted By:Abhisek Panda       Posted Date: October 26, 2009    Points: 10    Category: DataGrid    URL: http://www.dotnetspark.com  

In the code it is assumed that there is a table(Student) in MS Access database with two fields(sName & sRoll) containing some values or data.

Now design a form with a DataGrid control and a button(Show). Change the name of the button to btnShow.

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Data.OleDb;

namespace ShowData


public partial class Form1 : Form


Dataset ds;

OleDbCommand cmd;

OleDbConnection con;

OleDbDataAdapter da;

OleDbCommandBuilder cb;

public Form1()




private void btnShow_Click(Object Sender,EventArgs e)


//filling the dataadapter with data(Student is only the nameofthe

//datagrid at runtime) using the dataset



private void Form1_Load(Object Sender,EventArgs e)


con = new OleDbConnection("DataSource = d:\\Student.mdb;Provider = Microsoft.Jet.OleDb4.0");

cmd = new OleDbCommand("select*from student",con);

da = new OleDbDataAdapter();

da.SelectCommand = cmd;

cb = new OleDbCommandBuilder();

cb.DataAdapter = da;

ds = new Dataset();

//binding the datagrid to the dataset

this.dataGrid1.DataSource = ds;





Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend