Write code below in code behind file
ProductsData() functions is written to get data from Products table of NORTHWIND database in a DataSet object. In Page Load event, GridView in Bound to ProductsData() function. Two variables are declared before the RowDataBound Event of GridView to show total of UnitPrice and UnitsInStock. In RowDataBound Event, if the RowType is DataRow then UnitPrice and UnitInStock of a page is added and if the RowType is Footer then these values are displayed on Label controls. At the end, PageIndexChanging event is handled.
Visual Basic
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
GridView1.DataSource = ProductsData()
GridView1.DataBind()
End Sub
Public Function ProductsData() As DataSet
Dim text As String = "SELECT ProductID, ProductName, UnitPrice, UnitsInStock FROM Products"
Dim connString As String = "Data Source=YourServer;Initial Catalog=NORTHWIND;Integrated Security=True"
Dim conn As New SqlConnection(connString)
Dim cmd As New SqlCommand(text, conn)
conn.Open()
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds)
conn.Close()
Return ds
End Function
Private totalUnitPrice As Decimal = 0
Private totalUnitInstock As Integer = 0
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
totalUnitPrice += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "UnitPrice"))
totalUnitInstock += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "UnitsInStock"))
End If
If e.Row.RowType = DataControlRowType.Footer Then
Dim lblGTUnitPrice As Label = DirectCast(e.Row.FindControl("lblTotalUnitPrice"), Label)
Dim lblGTUnitInStock As Label = DirectCast(e.Row.FindControl("lblTotalUnitsInStock"), Label)
lblGTUnitPrice.Text = totalUnitPrice.ToString()
lblGTUnitInStock.Text = totalUnitInstock.ToString()
End If
End Sub
Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs) Handles GridView1.PageIndexChanging
GridView1.PageIndex = e.NewPageIndex
GridView1.DataSource = ProductsData()
GridView1.DataBind()
End Sub
Visual C#
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = ProductsData();
GridView1.DataBind();
}
public DataSet ProductsData()
{
string text = "SELECT ProductID, ProductName, UnitPrice, UnitsInStock FROM Products";
string connString = "Data Source=YourServer;Initial Catalog=NORTHWIND;Integrated Security=True";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(text, conn);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
return ds;
}
decimal totalUnitPrice = 0;
int totalUnitInstock = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
totalUnitPrice += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "UnitPrice"));
totalUnitInstock += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "UnitsInStock"));
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lblGTUnitPrice = (Label)e.Row.FindControl("lblTotalUnitPrice");
Label lblGTUnitInStock = (Label)e.Row.FindControl("lblTotalUnitsInStock");
lblGTUnitPrice.Text = totalUnitPrice.ToString();
lblGTUnitInStock.Text = totalUnitInstock.ToString();
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = ProductsData();
GridView1.DataBind();
}