1
Reply

pageindexchanging issue

bani marc

bani marc

Oct 7 2017 6:28 AM
226
Hi everybody. 
I have a gridview with pagination which is displaying filtered data from sql server. I have also a button. When this one is clicked, it's displaying all the data from sql server. My problem is that when I want to click on page 2, only the filtered data are displaying and not the second page of the total data.
 
Here is my code aspx:
  1. <asp:Button ID="btnTteLivraisons" CssClass="btn btn-success" runat="server" Text="Toutes les livraisons" OnClick="btnTteLivraisons_Click" Visible="False" />

  2. <asp:GridView ID="GridView1" runat="server" CssClass="mydatagrid" PagerStyle-CssClass="pager"  
  3.                                 HeaderStyle-CssClass="header" RowStyle-CssClass="rows" AllowPaging="True"    
  4.                                 OnPageIndexChanging="GridView1_PageIndexChanging" OnSorting="GridView1_Sorting"  
  5.                                 AutoGenerateColumns="False"  CellPadding="4"   
  6.                                 ForeColor="#333333" GridLines="None" Width="100%">  
  7.                                 <SelectedRowStyle BackColor="#C5BBAF" />  
  8.                                 <AlternatingRowStyle BackColor="White" />  
  9.   
  10.                                 <HeaderStyle HorizontalAlign="center" />  
  11.                                 <Columns>  
  12.                                     <asp:BoundField DataField="Nom" HeaderText="Nom" ItemStyle-HorizontalAlign="center" />  
  13.                                     <asp:BoundField DataField="Prenom" HeaderText="Prénom" ItemStyle-HorizontalAlign="center" />  
  14.                                     <asp:BoundField DataField="CP" HeaderText="Code Postal" ItemStyle-HorizontalAlign="center" />  
  15.                                     <asp:BoundField DataField="ville" HeaderText="Ville" ItemStyle-HorizontalAlign="center" />  
  16.                                     <asp:BoundField DataField="Telephone" HeaderText="Téléphone" ItemStyle-HorizontalAlign="center" />  
  17.                                     <asp:BoundField DataField="date_command2" HeaderText="Date Commande" DataFormatString="<%$AppSettings:FormatDateGrille%>" ItemStyle-HorizontalAlign="center" />  
  18.                                     <asp:BoundField DataField="heure_command" HeaderText="Heure Commande" ItemStyle-HorizontalAlign="center" />  
  19.                                     <asp:BoundField DataField="montantCommande" HeaderText="Montant de la Commande" ItemStyle-HorizontalAlign="center" />  
  20.                                     <asp:TemplateField HeaderText="">  
  21.                                         <ItemTemplate>  
  22.                                             <asp:Button id="btnCommande" runat="server" Text="Détail Commande" OnClick="btnCommande_Click"  CommandArgument='<%# Eval("ID") %>' class="btn btn-info" />  
  23.                                         </ItemTemplate>  
  24.                                     </asp:TemplateField>  
  25.                                     <asp:TemplateField HeaderText="">  
  26.                                         <ItemTemplate>  
  27.                                             <asp:Button id="btnEdit" runat="server" Text="Editer" OnClick="Edit" CommandArgument='<%# Eval("ID") %>' class="btn btn-primary" />  
  28.                                         </ItemTemplate>  
  29.                                     </asp:TemplateField>  
  30.                                     <asp:TemplateField HeaderText="">  
  31.                                         <ItemTemplate>  
  32.                                             <asp:Button ID="btnSupprimer" runat="server" Text="Supprimer" OnClick="Delete" CommandArgument='<%# Eval("ID") %>' CssClass="btn btn-danger" />  
  33.                                         </ItemTemplate>  
  34.                                     </asp:TemplateField>  
  35.                                 </Columns>   
  36.                             </asp:GridView>               
 my code behind :
  1. protected void Page_Load(object sender, EventArgs e)  
  2.         {  
  3.             if (!IsPostBack)  
  4.             {  
  5.                 BindInfoLivraison();  
  6.                 btnTteLivraisons.Visible = true;  
  7.             }  
  8.         }  
  9.   
  10. public static DataTable ToDataTable<T>(List<T> items)  
  11.         {  
  12.             DataTable dataTable = new DataTable(typeof(T).Name);  
  13.   
  14.             //Get all the properties  
  15.             PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);  
  16.             foreach (PropertyInfo prop in Props)  
  17.             {  
  18.                 //Setting column names as Property names  
  19.                 dataTable.Columns.Add(prop.Name);  
  20.             }  
  21.             foreach (T item in items)  
  22.             {  
  23.                 var values = new object[Props.Length];  
  24.                 for (int i = 0; i < Props.Length; i++)  
  25.                 {  
  26.                     //inserting property values to datatable rows  
  27.                     values[i] = Props[i].GetValue(item, null);  
  28.                 }  
  29.                 dataTable.Rows.Add(values);  
  30.             }  
  31.             //put a breakpoint here and check datatable  
  32.             return dataTable;  
  33.         }  
  34.   
  35.         private void BindInfoLivraison()  
  36.         {  
  37.             using (RestauDBEntities1 db = new RestauDBEntities1())  
  38.             {  
  39.                 if (db.InfoLivraison.Count() > 0)  
  40.                 {  
  41.                     DateTime today = Convert.ToDateTime(DateTime.Now.ToString("dd/MM/yyyy"));  
  42.   
  43.                     List<InfoLivraison> list = (from infoLiv in db.InfoLivraison where infoLiv.date_command2 == today select infoLiv).ToList();  
  44.   
  45.                     DataTable dt = ToDataTable<InfoLivraison>(list);  
  46.   
  47.                     ViewState["livraison"] = dt;  
  48.   
  49.                     GridView1.DataSource = dt;  
  50.                     GridView1.DataBind();  
  51.   
  52.                 }  
  53.                 else  
  54.                 {  
  55.                     GridView1.DataSource = null;  
  56.                     GridView1.DataBind();  
  57.                 }  
  58.             }  
  59.         }  
  60.   
  61.         private void BindInfoLivraisonTotal()  
  62.         {  
  63.             DataTable dt = new DataTable();  
  64.             using (RestauDBEntities1 db = new RestauDBEntities1())  
  65.             {  
  66.                 if (db.InfoLivraison.Count() > 0)  
  67.                 {  
  68.                     //string today = DateTime.Now.ToString("d");  
  69.                     GridView1.DataSource = (from infoLiv in db.InfoLivraison select infoLiv).ToList();  
  70.                     GridView1.DataBind();  
  71.                 }  
  72.                 else  
  73.                 {  
  74.                     GridView1.DataSource = null;  
  75.                     GridView1.DataBind();  
  76.                 }  
  77.             }  
  78.         }  
  79.   
  80. protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)  
  81.         {  
  82.             if (ViewState["livraison"] != null)  
  83.             {  
  84.                 GridView1.PageIndex = e.NewPageIndex;  
  85.                 GridView1.DataSource = ViewState["livraison"];  
  86.                 GridView1.DataBind();  
  87.   }
  88.           }  


Answers (1)