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

http://www.codeproject.com/csharp/UsingDataGrid.asp

http://www.google.com/search?q=c%23+show+data+in+a+datagrid&btnG=Search&hl=en

http://www.c-sharpcorner.com/UploadFile/mahesh/DataGrid11282005234057PM/DataGrid.aspx

6 Comments:

  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:

    GridView1.DataBind();
    Dev Singh said...
    i like this post


    www.richonet.com
    www.bwtrp.com
    www.oracledba.in
    Dev Singh said...
    i like this

    www.richonet.com
    www.bwtrp.com
    www.oracledba.in
    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