Hi makumbi,
Please verify the DataType of "Qty" column with DataBase you might be saving Integer DataType in database and making calculation with Decimal DataType so you are facing that problem.
Please refer below sample.
Note: For this sample i have used temporary DataTable. For more details refer How to create Temporary Table in ASP.Net using C# and VB.Net.
HTML
<asp:GridView ID="Gvgridclient" runat="server" AutoGenerateColumns="False" ShowFooter="True">
<Columns>
<asp:BoundField DataField="itemcode" HeaderText="Itemcode" />
<asp:BoundField DataField="stockitem" HeaderText="Item Description" />
<asp:BoundField DataField="itemsize" HeaderText="itemsize" />
<asp:TemplateField HeaderText="Qty">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Qty") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="Fqty" runat="server" Width="46px"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Qty") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="saleprice" HeaderText="OrderPrice" />
<asp:BoundField DataField="saleprice" HeaderText="SalePrice" />
<asp:BoundField HeaderText="Subtotal" />
<asp:BoundField DataField="account" HeaderText="Admno" />
<asp:BoundField DataField="clientname" HeaderText="Names" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("itemcode", typeof(int)),
new DataColumn("stockitem", typeof(string)),
new DataColumn("itemsize", typeof(int)),
new DataColumn("Qty", typeof(int)),
new DataColumn("OrderPrice", typeof(int)),
new DataColumn("SalePrice", typeof(int)),
new DataColumn("account", typeof(int)),
new DataColumn("clientname", typeof(string))
});
dt.Rows.Add(1, "A", 100, 10, 20, 30, 123, "John Hammond");
dt.Rows.Add(2, "B", 200, 20, 30, 40, 456, "Mudassar Khan");
this.Gvgridclient.DataSource = dt;
this.Gvgridclient.DataBind();
int total =dt.AsEnumerable().Sum(row => row.Field<int>("Qty"));
Gvgridclient.FooterRow.Cells[1].Text = "Total";
Gvgridclient.FooterRow.Cells[1].HorizontalAlign = HorizontalAlign.Right;
Gvgridclient.FooterRow.Cells[2].Text = total.ToString("N2");
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {
New DataColumn("itemcode", GetType(Integer)),
New DataColumn("stockitem", GetType(String)),
New DataColumn("itemsize", GetType(Integer)),
New DataColumn("Qty", GetType(Integer)),
New DataColumn("OrderPrice", GetType(Integer)),
New DataColumn("SalePrice", GetType(Integer)),
New DataColumn("account", GetType(Integer)),
New DataColumn("clientname", GetType(String))
})
dt.Rows.Add(1, "A", 100, 10, 20, 30, 123, "John Hammond")
dt.Rows.Add(2, "B", 200, 20, 30, 40, 456, "Mudassar Khan")
Me.Gvgridclient.DataSource = dt
Me.Gvgridclient.DataBind()
Dim total As Integer = dt.AsEnumerable().Sum(Function(row) row.Field(Of Integer)("Qty"))
Gvgridclient.FooterRow.Cells(1).Text = "Total"
Gvgridclient.FooterRow.Cells(1).HorizontalAlign = HorizontalAlign.Right
Gvgridclient.FooterRow.Cells(2).Text = total.ToString("N2")
End If
End Sub
Screenshot