DJ메탈짱™의 Free Style

[ASP.NET] GridView 페이징 (SqlDataSource 컨트롤 사용안함) 본문

일(job)/MS(Microsoft)

[ASP.NET] GridView 페이징 (SqlDataSource 컨트롤 사용안함)

뽀&쏭 2015. 11. 15. 14:22

 

1. ASPX

<asp:GridView ID="gvOrders" runat="server"  AllowPaging="True" AutoGenerateColumns="False" EnableViewState="False" OnPageIndexChanging="gvOrders_PageIndexChanging"  PageSize="20">
 <Columns>                              
  <asp:TemplateField  HeaderText="No">
   <ItemStyle Width="10px" CssClass="tabletit2" HorizontalAlign="Center" /> 
   <ItemTemplate><%# (Container.DataItemIndex + 1).ToString() %></ItemTemplate>
  </asp:TemplateField>      
 </Columns>

<PagerSettings FirstPageText="&lt;&lt; First" LastPageText="Last &gt;&gt;" NextPageText="Next &gt;" PreviousPageText="&lt; Previous" Mode="NumericFirstLast" />
<asp:GridView>

 

2. ASPX.CS

private void GridViewBind()
{
    SqlCommand cmdOrders = new SqlCommand();
    cmdOrders.Connection = Con;
    cmdOrders.CommandText = "SELECT * FROM Orders";


    cmdOrders.Parameters.Add("@OrderID", SqlDbType.VarChar, 10);
    cmdOrders.Parameters["@OrderID"].Value = strOrderID.Text ;

    DataSet dsMain = new DataSet();
    SqlDataAdapter daOrders = new SqlDataAdapter(cmdOrders);
    daOrders.Fill(dsMain, "Orders");


    gvOrders.DataSource = dsMain.Tables[0];
    gvOrders.DataBind();

}

protected void gvOrders_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    gvRollBook.PageIndex = e.NewPageIndex;
    GridViewBind() ;
}