Enter the data, programmatically, from an Access database table in a ASP.NET GridView
Suppose we have the Northwind.mdb Access database stored in the App_Data folder, and we want to display some data in a GridView.
Code:
<div>
<asp:GridView ID="GridViewEmployees" runat="server"
CellPadding="4"
ForeColor="#333333"
GridLines="None"
Width="500">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle HorizontalAlign="Left" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333"/>
</asp:GridView>
</div>
In the code-behind file that you can write code like this.
Code:
AccessDataSource ads = new AccessDataSource();
string dataFile = Server.MapPath("~/App_Data/Northwind.mdb");
string selectCommand = "SELECT Employees.TitleOfCourtesy AS Title, Employees.LastName AS Surname, Employees.FirstName AS Name, Employees.City " +
"FROM Employees " +
"ORDER BY Employees.LastName";
ads.DataFile = dataFile;
ads.SelectCommand = selectCommand;
GridViewEmployees.DataSource = ads;
GridViewEmployees.DataBind();
Here is what displays the GridView: