- You have
AllowPaging="True"
set on the GridView
; - You're binding the grid from the code-behind, rather than from a data-source control;
- You haven't handled the
PageIndexChanging
event;
When you don't use a data-source control, the grid doesn't know where to get its data, so you have to handle various events to re-bind the grid.
The simplest option would be to add a handler for the event:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindGrid();
}
private void BindGrid()
{
GridView1.DataSource = GetData("SELECT ReplyDoubtID, Name, Subject, ReplyDoubt, Date, Files, FileName, Topic, ISNULL((SELECT AVG(Rating) FROM Rates WHERE ReplyDoubtID = ReplyDoubt.ReplyDoubtID), 0) Rating FROM ReplyDoubt");
GridView1.DataBind();
}
Another option would be to use
model binding[
^] to bind the grid:
<asp:GridView ID="GridView1" runat="server"
...
SelectMethod="GridView1_GetData"
>
protected DataView GridView1_GetData()
{
DataTable dt = GetData("SELECT ReplyDoubtID, Name, Subject, ReplyDoubt, Date, Files, FileName, Topic, ISNULL((SELECT AVG(Rating) FROM Rates WHERE ReplyDoubtID = ReplyDoubt.ReplyDoubtID), 0) Rating FROM ReplyDoubt");
return dt.DefaultView;
}
Or
the SqlDataSource
control[
^]:
<asp:GridView ID="GridView1" runat="server"
...
DataSourceID="GridView1DataSource"
>
...
</asp:GridView>
<asp:SqlDataSource id="GridView1DataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:VCRConnectionString %>"
SelectCommand="SELECT ReplyDoubtID, Name, Subject, ReplyDoubt, Date, Files, FileName, Topic, ISNULL((SELECT AVG(Rating) FROM Rates WHERE ReplyDoubtID = ReplyDoubt.ReplyDoubtID), 0) Rating FROM ReplyDoubt"
/>