<%@ Import Namespace="System.Data"%> <%@ Page Language="C#" Debug="true" %> <%@ import Namespace="System.Data.OleDb" %> <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 DataGrid ***// myDataGrid.DataSource = ds; myDataGrid.DataBind(); dtAdapter = null; objConn.Close(); objConn = null; } void ShowPageCommand(Object sender, DataGridPageChangedEventArgs e) { myDataGrid.CurrentPageIndex = e.NewPageIndex; BindData(); } </script> <html> <head> <title>ThaiCreate.Com ASP.NET - DataGrid</title> </head> <body> <form id="form1" runat="server" > <asp:DataGrid id="myDataGrid" PageSize="2" OnPageIndexChanged="ShowPageCommand" AllowPaging="True" runat="server"> <HeaderStyle BackColor="#cccccc"></HeaderStyle> <AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle> </asp:DataGrid> </form> </body> </html>
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท