2013-07-19 89 views
0

我想允許分頁爲我的gridview。我已經允許在我的gridview中進行封裝,並且還添加了頁面大小。不幸的是,它不起作用。我研究過,我看到人們不需要添加任何代碼。無法使用分頁儘管啓用分頁

這是我的gridview的

<asp:GridView ID="GVVerify" runat="server" AllowPaging="True" AutoGenerateSelectButton="True" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" CssClass="gridviewadminverify" ForeColor="Black" OnSelectedIndexChanged="GVVerify_SelectedIndexChanged" Width="100%"> 
       <FooterStyle BackColor="#CCCCCC" /> 
       <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" /> 
       <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" /> 
       <RowStyle BackColor="White" /> 
       <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" /> 
       <SortedAscendingCellStyle BackColor="#F1F1F1" /> 
       <SortedAscendingHeaderStyle BackColor="#808080" /> 
       <SortedDescendingCellStyle BackColor="#CAC9C9" /> 
       <SortedDescendingHeaderStyle BackColor="#383838" /> 
      </asp:GridView> 

我的源代碼這是我如何通過數據綁定是使用不同的數據源連接我的SQL。

SqlConnection conn = new SqlConnection(); 
conn.ConnectionString = "Data Source = localhost; Initial Catalog = project; Integrated Security= SSPI"; 
conn.Open(); 

DataSet ds = new DataSet(); 

SqlDataAdapter da = new SqlDataAdapter("SELECT policeid as [Police ID], password as [Password], email as [Email], nric as [NRIC], fullname as [Full Name], contact as [Contact], address as [Address], location as [Location] From LoginRegisterPolice where pending='pending'", conn); 
da.Fill(ds); 

GVVerify.DataSource = ds;   
GVVerify.DataBind(); 
conn.Close(); 

回答

1

您沒有使用PageIndexChanging事件,你必須綁定PageIndexChanging事件,並使用當前頁重新綁定你的網格。

的Html

<asp:GridView ID="GVVerify" runat="server" OnPageIndexChanging="GridViewPageEventHandler" AllowPaging="True" AutoGenerateSelectButton="True" BackColor="#CCCCCC" 
BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" 
CssClass="gridviewadminverify" ForeColor="Black" OnSelectedIndexChanged="GVVerify_SelectedIndexChanged" 
Width="100%"> 

代碼背後

protected void grdView_PageIndexChanging(object sender, GridViewPageEventArgs e) 
{ 
    GVVerify.PageIndex = e.NewPageIndex; 
    GVVerify.DataSource = GetGridData(); 
    GVVerify.DataBind(); 
} 
0

您需要處理GridView控件的PageIndexChanging事件。

例如:

​​

其中bindGrid方法將包含代碼結合電網。 對於實施例

private void bindGrid() 
    { 
     SqlConnection conn = new SqlConnection(); 
     conn.ConnectionString = "Data Source = localhost; Initial Catalog = project; Integrated Security= SSPI"; 
     conn.Open(); 
     DataSet ds = new DataSet(); 

     SqlDataAdapter da = new SqlDataAdapter("SELECT policeid as [Police ID], password as [Password], email as [Email], nric as [NRIC], fullname as [Full Name], contact as [Contact], address as [Address], location as [Location] From LoginRegisterPolice where pending='pending'", conn); 
     da.Fill(ds); 

     GVVerify.DataSource = ds;   
     GVVerify.DataBind(); 
     conn.Close(); 
    }