Sunday, July 22, 2007

First add a data grid view to the form

then go to the code file

on a buttons click event add the following code

U need to add the header named

using System.Data.OleDb;

private void button1_Click(object sender, EventArgs e)

OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\test.mdb;Jet OLEDB:Database Password=password");;

String queryString ="select UserID from UserTable where USerID > 2";

OleDbDataAdapter da = new OleDbDataAdapter(queryString, conn);
DataSet ds = new DataSet();
da.Fill(ds, "UserTable");
dataGridView1.DataSource = ds.Tables["UserTable"].DefaultView;


that's it. U r done. U can now add your custom query and use the data grid to show your results.


Enough for today. :D

Some links


  1. Mert Man said...
    Hey, I searched all over the place for a straight forward way to load a datagrid with a query.

    Your example was the only one that got me going! I am SO THANKFUL!!

    -Mert Man
    Shimul said...
    Mert i am glad that it came to your help
    Anonymous said...
    Write this at the end of the code and it will execute properly:

    Dev Singh said...
    i like this post
    Dev Singh said...
    i like this
    Jignesh said...
    How to display data in datagridview starting from the record whic we pass at runtime? In C#
    For example, there is 10 rows in a table. When i run the app i'll pass 5th record's value so it shld show all data of tbl starting from 5th row.

Post a Comment