Hi tutovavera,
Check this example. Now please take its reference and correct your code.
For this example I am making use of the Microsoft's Northwind Database. You can download from the below link.
Install Microsoft Northwind and Pubs Sample databases in SQL Server Management Studio
HTML
<div align="center">
Order ID:
<asp:TextBox ID="txtOrderId" OnTextChanged="OnTextChanged" AutoPostBack="true" runat="server" />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true"
PageSize="2" OnRowDataBound="OnDataBound" OnPageIndexChanging="OnPageIndexChanging"
ShowFooter="true">
<Columns>
<asp:BoundField DataField="OrderID" HeaderText="Order ID" />
<asp:BoundField DataField="ProductName" HeaderText="Product Name" />
<asp:BoundField DataField="Price" HeaderText="Price" DataFormatString="{0:N2}" />
</Columns>
</asp:GridView>
<br />
<b>Grand Total:</b> <asp:Label ID="txtGrandTotal" runat="server" />
</div>
Namespaces
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid(string orderId = null)
{
string query = "SELECT TOP 5 OrderID,";
query += "(SELECT ProductName FROM Products WHERE ProductID = details.ProductId) ProductName,";
query += "(Quantity * UnitPrice) Price";
query += " FROM [Order Details] details";
if (!string.IsNullOrEmpty(orderId))
{
query += " WHERE OrderID = " + orderId;
}
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();
txtGrandTotal.Text = dt.AsEnumerable().Sum(row => row.Field<decimal>("Price")).ToString("N2");
}
}
}
}
}
protected void OnTextChanged(object sender, EventArgs e)
{
BindGrid(txtOrderId.Text);
}
decimal total = 0;
protected void OnDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
total = total + Convert.ToDecimal(e.Row.Cells[2].Text);
}
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].Text = "<b>Sub Total:</b>";
e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Right;
e.Row.Cells[2].Text = total.ToString("N2");
}
}
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindGrid(txtOrderId.Text);
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGrid()
End If
End Sub
Private Sub BindGrid(Optional ByVal orderId As String = Nothing)
Dim query As String = "SELECT TOP 5 OrderID,"
query += "(SELECT ProductName FROM Products WHERE ProductID = details.ProductId) ProductName,"
query += "(Quantity * UnitPrice) Price"
query += " FROM [Order Details] details"
If Not String.IsNullOrEmpty(orderId) Then
query += " WHERE OrderID = " & orderId
End If
query += " ORDER BY OrderID"
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(query)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
txtGrandTotal.Text = dt.AsEnumerable().Sum(Function(row) row.Field(Of Decimal)("Price")).ToString("N2")
End Using
End Using
End Using
End Using
End Sub
Protected Sub OnTextChanged(ByVal sender As Object, ByVal e As EventArgs)
BindGrid(txtOrderId.Text)
End Sub
Private total As Decimal = 0
Protected Sub OnDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
total = total + Convert.ToDecimal(e.Row.Cells(2).Text)
End If
If e.Row.RowType = DataControlRowType.Footer Then
e.Row.Cells(1).Text = "<b>Sub Total:</b>"
e.Row.Cells(1).HorizontalAlign = HorizontalAlign.Right
e.Row.Cells(2).Text = total.ToString("N2")
End If
End Sub
Protected Sub OnPageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
GridView1.PageIndex = e.NewPageIndex
BindGrid(txtOrderId.Text)
End Sub
Screenshot