<%@ Import Namespace="System.Data"%> <%@ Import Namespace="System.Data.OleDb"%> <%@ Page Language="C#" Debug="true" %> <script runat="server"> void Page_Load(object sender,EventArgs e) { if(!Page.IsPostBack) { BindData(); } } void BindData() { OleDbConnection objConn = new OleDbConnection(); OleDbCommand objCmd = new OleDbCommand(); OleDbDataAdapter dtAdapter = new OleDbDataAdapter(); DataSet ds = new DataSet(); String strConnString,strSQL; strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("database/mydatabase.mdb") + ";"; strSQL = "SELECT * FROM customer"; objConn.ConnectionString = strConnString; objCmd.Connection = objConn; objCmd.CommandText = strSQL; objCmd.CommandType = CommandType.Text; dtAdapter.SelectCommand = objCmd; dtAdapter.Fill(ds); //*** BindData to GridView ***// myGridView.DataSource = ds; myGridView.DataBind(); dtAdapter = null; objConn.Close(); objConn = null; } void ShowPageCommand(Object sender, GridViewPageEventArgs e) { myGridView.PageIndex = e.NewPageIndex; BindData(); } </script> <html> <head> <title>ThaiCreate.Com ASP.NET - GridView</title> </head> <body> <form id="form1" runat="server" > <asp:GridView id="myGridView" PageSize="2" OnPageIndexChanging="ShowPageCommand" AllowPaging="True" runat="server"> <HeaderStyle BackColor="#cccccc"></HeaderStyle> <AlternatingRowStyle BackColor="Gainsboro"></AlternatingRowStyle> </asp:GridView> </form> </body> </html>
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท