我有搜索按钮,从数据库中选择数据我试图使用以下代码获取gridview之外的列数量和借方和贷方的总和:
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();
}
但是在sumCredit + = Convert.ToDouble(GridView1.Rows [i] .Cells [2] .Text);它的返回输入字符串格式错误
这是我对gridview的html:
<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>
并计算行数据绑定量
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();
}
}