Search code examples
c#asp.netms-accessgridviewoledb

GridView Access Data Source


I have a GridView control that I would like to display the rows of a table (cartTable), based on the field 'orderNo' which is stored in a integer 'intOrderNo', how can i configure my data source to do as such?


Solution

  • try this:
    -connect to the access file
    -set your query and your WHERE clause
    -execute it
    -then bind it to your grid

    OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\YOUR_ACCESS_FILE_PATH");
    conn.Open();
    OleDbCommand cmd = new OleDbCommand("SELECT * FROM cartTable WHERE orderNo = " + intOrderNo , conn);
    OleDbDataReader reader = cmd.ExecuteReader();
    DataTable dt = new DataTable();
    dt.Load(reader);
    
    //Bind your grid
    this.gridView1.DataSource = dt;
    this.gridView1.DataBind();