I have search button that select data from data base am trying to get the sum of the column amount and debit and credit outside the gridview using this code :

protected void Button1_Click(object sender, EventArgs e)
{

    SqlParameter[] para = new SqlParameter[4];
    para[0] = new SqlParameter("@stat_sub_leger", ddlACCcode.SelectedValue);
          para[1] = new SqlParameter("@branch", DDLBranch.SelectedValue);
          para[2] = new SqlParameter("@from", db.getDate(txtFrom.Text));
          para[3] = new SqlParameter("@to", db.getDate(txtTo.Text));

          DataTable dtreport = db.SelectCmdText("Select * from PostedVoucher where stat_sub_leger = @stat_sub_leger and branch=@branch and stat_date between @from and @to ORDER BY stat_date ", para);

          GridView1.DataSource = dtreport;
          GridView1.DataBind();
            double sumCredit = 0;
    double sumDebit = 0;
    double sumAmount = 0;
    TxtCredit.Text = "0";
    TxtDebit.Text = "0";
    for (int i = 0; i < GridView1.Rows.Count; ++i)
    {
        sumCredit += Convert.ToDouble(GridView1.Rows[i].Cells[2].Text);
        TxtCredit.Text = sumCredit.ToString();

        sumDebit += Convert.ToDouble(GridView1.Rows[i].Cells[1].Text);
        TxtDebit.Text = sumDebit.ToString();

        sumAmount += Convert.ToDouble(GridView1.Rows[i].Cells[0].Text);
        TxtAmount.Text = sumAmount.ToString();

    }

but in sumCredit += Convert.ToDouble(GridView1.Rows[i].Cells[2].Text); it's return Input string was not in a correct format error

and this my html for the gridview :

        <asp:TemplateField HeaderText="amount">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblTotal" Text=""></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>


         <asp:TemplateField HeaderText="debit">
            <ItemTemplate>
                <asp:Label ID="debit" runat="server" Text='<%# (Eval("stat_flag").ToString() =="d" ) ? Eval("stat_amount"): "0" %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

              <asp:TemplateField HeaderText="credit">
            <ItemTemplate>
                <asp:Label ID="credit" runat="server" Text='<%# (Eval("stat_flag").ToString() =="c" ) ? Eval("stat_amount"): "0" %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

                           </Columns>
                       </asp:GridView>

and to calculate the amount in row data bound

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string deb = ((Label)e.Row.FindControl("credit")).Text;
        string cred = ((Label)e.Row.FindControl("debit")).Text;

        decimal totalvalue = Convert.ToDecimal(deb) + Convert.ToDecimal(cred);

        amount += totalvalue;

        // Dim lblRunningTotal As Label = e.Row.FindControl("lblRunningTotal")
        //lblRunningTotal.Text = m_runningTotal.ToString

        Label lbl = (Label)e.Row.FindControl("lblTotal");
        lbl.Text = amount.ToString();
    }
}

Related posts

Recent Viewed