在asp.net c#中,当flag ='D'为sumDebt时,我试图返回总和 当flag ='C'作为sumcredit时的总和 我的代码是:
SqlParameter[] para = new SqlParameter[4];
para[0] = new SqlParameter("@stat_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 sum(stat_amount) as sumDebt from PostedVoucher where stat_flag ='D' and stat_leger =@stat_leger and branch=@branch and stat_date between @from and @to UNION Select sum(stat_amount) as sumcredit from PostedVoucher where stat_flag ='C' and stat_leger =@stat_leger and branch=@branch and stat_date between @from and @to ", para);
GridView1.DataSource = dtreport;
GridView1.DataBind();
在html网格视图中:
<asp:TemplateField HeaderText="sumDebt">
<ItemTemplate>
<asp:Label ID="sumDebt" runat="server" Text='<%#string.Format("{0:0.000}",float.Parse(Eval("sumDebt").ToString()))%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="sumcredit">
<ItemTemplate>
<asp:Label ID="sumcredit" runat="server" Text='<%#string.Format("{0:0.000}",float.Parse(Eval("sumcredit").ToString()))%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
在gridview中它返回错误的问题(DataBinding:'System.Data.DataRowView'不包含名为'sumcredit'的属性。)
答案 0 :(得分:0)
您可以使用Eval的第二个参数来格式化结果字符串:
<%# Eval("sumDebt", "{0:c}")%>
答案 1 :(得分:0)
要合并表,请尝试这样: -
SqlConnection conn = new SqlConnection("connectionString");
SqlCommand cmd = new SqlCommand("Select sum(stat_amount) as sumDebt from PostedVoucher where stat_flag ='D' and stat_leger =@stat_leger and branch=@branch and stat_date between @from and @to UNION Select sum(stat_amount) as sumcredit from PostedVoucher where stat_flag ='C' and stat_leger =@stat_leger and branch=@branch and stat_date between @from and @to", conn);
cmd.Parameters.AddWithValue("@stat_leger", ddlACCcode.SelectedValue);
cmd.Parameters.AddWithValue("@branch", DDLBranch.SelectedValue);
cmd.Parameters.AddWithValue("@from", db.getDate(txtFrom.Text));
cmd.Parameters.AddWithValue("@to", db.getDate(txtTo.Text));
conn.Open();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet);
DataTable tbl1 = dataSet.Tables[0];
DataTable tbl2 = dataSet.Tables[1];
tbl1.Columns.Add("sumcredit");
for (int i = 0; i < tbl2.Rows.Count; i++)
{
DataRow dr = tbl2.Rows[i];
DataRow dr1 = tbl1.Rows[i];
foreach (DataColumn v_Column in dr.Table.Columns)
{
if (dr1.Table.Columns.Contains("sumcredit"))
{
dr1["sumcredit"] = dr["sumcredit"];
}
}
}