Hi kavithav,
Check this example. Now please take its reference and correct your code. You need to check if GridView1.PageCount - 1 == GridView1.PageIndex on PageIndexChanging event then make the footer visible true.
For this example i have referred the below article.
Display sum of Columns total in GridView Footer in ASP.Net using C# and VB.Net
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true"
OnPageIndexChanging="OnPageIndexChanging" ShowFooter="false">
<Columns>
<asp:BoundField DataField="OrderID" HeaderText="Order ID" ItemStyle-Width="60" />
<asp:BoundField DataField="ProductName" HeaderText="Product Name" ItemStyle-Width="210" />
<asp:BoundField DataField="Price" HeaderText="Price" ItemStyle-Width="60" DataFormatString="{0:N2}"
ItemStyle-HorizontalAlign="Right" />
</Columns>
</asp:GridView>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string query = "SELECT TOP 30 OrderID,";
query += "(SELECT ProductName FROM Products WHERE ProductID = details.ProductId) ProductName,";
query += "(Quantity * UnitPrice) Price";
query += " FROM [Order Details] details";
query += " ORDER BY OrderID";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
//Calculate Sum and display in Footer Row
decimal total = dt.AsEnumerable().Sum(row => row.Field<decimal>("Price"));
GridView1.FooterRow.Cells[1].Text = "Total";
GridView1.FooterRow.Cells[1].HorizontalAlign = HorizontalAlign.Right;
GridView1.FooterRow.Cells[2].Text = total.ToString("N2");
}
}
}
}
}
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
if (GridView1.PageCount - 1 == GridView1.PageIndex)
{
GridView1.ShowFooter = true;
}
else
{
GridView1.ShowFooter = false;
}
this.BindGrid();
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGrid()
End If
End Sub
Private Sub BindGrid()
Dim query As String = "SELECT TOP 30 OrderID,"
query += "(SELECT ProductName FROM Products WHERE ProductID = details.ProductId) ProductName,"
query += "(Quantity * UnitPrice) Price"
query += " FROM [Order Details] details"
query += " ORDER BY OrderID"
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand(query)
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As New DataTable()
sda.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
'Calculate Sum and display in Footer Row
Dim total As Decimal = dt.AsEnumerable().Sum(Function(row) row.Field(Of Decimal)("Price"))
GridView1.FooterRow.Cells(1).Text = "Total"
GridView1.FooterRow.Cells(1).HorizontalAlign = HorizontalAlign.Right
GridView1.FooterRow.Cells(2).Text = total.ToString("N2")
End Using
End Using
End Using
End Using
End Sub
Protected Sub OnPageIndexChanging(sender As Object, e As GridViewPageEventArgs)
GridView1.PageIndex = e.NewPageIndex
If GridView1.PageCount - 1 = GridView1.PageIndex Then
GridView1.ShowFooter = True
Else
GridView1.ShowFooter = False
End If
Me.BindGrid()
End Sub
Screenshot