Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to display a Table from Database in GridView Control on Run time using .NET 2.0

Author: DevAsp Team
Download Source Code : 663_Access_Grid.zip

: In this simple article you will learn how can Attach Access database Table with Data Grid View on Run time.

Simply just create New Project of console application in C#.

 

Drag one Data Grid View on the form and create a database in access with same database which is in Source folder.

 

In this sample I am using MS Access database and getting values from access on run time. This is a good example for beginners.

 

 

Now declare global variables

 

System.Data.OleDb.OleDbConnection cn = new System.Data.OleDb.OleDbConnection();

        System.Data.OleDb.OleDbCommand cm = new System.Data.OleDb.OleDbCommand();

        System.Data.OleDb.OleDbDataReader  rd;

        System.Data.OleDb.OleDbDataAdapter  adp;

 

Now just write code on Form Load Event

 

private void Form1_Load(object sender, EventArgs e)

        {

           

            cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb";

            try

            {

                cn.Open();

            }

            catch (Exception Ex)

            {

                MessageBox.Show(Ex.Message);

            }

            cm.Connection = cn;

            cm.CommandText = "select * from tbl1";

            try

            {

                adp = new System.Data.OleDb.OleDbDataAdapter();

                DataSet ds = new DataSet();

                adp.SelectCommand = cm;

                adp.Fill(ds, "dataset");

                dataGridView1.DataSource = ds;

                dataGridView1.DataSource = ds.Tables[0];

                dataGridView1.Refresh();

            }

            catch (Exception Ex)

            {

 

                MessageBox.Show(Ex.Message);

            }

            finally

            {

                cn.Close();

            }

       

        }

 

Here data Adaptor fill command fill the Dataset ds. Then we assign Data grid view that Data set and we are able to see database values on grid.

Article Comments
i want to search hiding the column of data grid view that i cant find

Posted on 9/28/2007 10:21:31 AM by vishal

how to add data in database at run time.
theory and code.

Posted on 10/1/2007 5:58:07 AM by aniket kulkarni

how to add data in database during run time.(theory and code)

Posted on 10/1/2007 5:59:10 AM by aniket kulkarni

how we can show a table in gridview as 2 *1 =2 or 2*2=4

Posted on 3/8/2010 5:00:14 AM by ishu sharma

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Creating Multi Browser Windows Application in .NET 2.0.

Disclaimer - Privacy
© 2002-2017 DevASP.net