C#中datagridview合计行问题

yingbo833 2015-08-05 04:58:49
在C#中要实现 按条件查询,并把结果显示在datagridview中去,并实现数据和金额的汇总

以下是代码:
private void button1_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox2.Text)
&& string.IsNullOrEmpty(textBox3.Text) && string.IsNullOrEmpty(textBox4.Text)
&& string.IsNullOrEmpty(textBox5.Text) && string.IsNullOrEmpty(comboBox1.Text))
{
MessageBox.Show("请输入查询条件!");

return;
}


string sql = @"
select t.fnumber as 物料编号,t.fname as 物料名称 ,fmodel as 规格型号 ,fbillno as 单据编号,ts.fname as 仓库,
fdate as 日期,FQtyMust as 应发数量,Fqty as 实发数量 from ICStockBillEntry ICE
left join t_item t on t.FItemID=ICE.FItemID left join t_icitem tm on t.FItemID=tm.FItemID
LEFT JOIN ICStockBill ICS ON ICE.Finterid=ICS.Finterid
left join t_stock ts on ICE.FDCStockID=ts.fitemid where ftrantype=21";



if (!string.IsNullOrEmpty(textBox1.Text))
{
var fstCondition = textBox1.Text.Trim();

if (fstCondition.Length > 0)
sql = sql + "and t.fnumber LIKE '%" + textBox1.Text + "%' ";
}

if (!string.IsNullOrEmpty(textBox2.Text))
{
var secCondition = textBox2.Text.Trim();

if (secCondition.Length > 0)
sql = sql + "and t.fname LIKE '%" + textBox2.Text + "%' ";
}

if (!string.IsNullOrEmpty(textBox3.Text))
{
var secCondition1 = textBox3.Text.Trim();

if (secCondition1.Length > 0)
sql = sql + "and fmodel LIKE '%" + textBox3.Text + "%' ";
}
if (!string.IsNullOrEmpty(textBox4.Text))
{
var secCondition2 = textBox4.Text.Trim();

if (secCondition2.Length > 0)
sql = sql + "and convert(varchar,fdate,120) LIKE '%" + textBox4.Text.Trim() + "%' ";
}
if (!string.IsNullOrEmpty(textBox5.Text))
{
var secCondition3 = textBox5.Text.Trim();

if (secCondition3.Length > 0)
sql = sql + "and fbillno LIKE '%" + textBox5.Text + "%' ";
}

if (!string.IsNullOrEmpty(comboBox1.Text))
{
var secCondition4 = comboBox1.Text.Trim();

if (secCondition4.Length > 0)
sql = sql + "and ts.fname LIKE '%" + comboBox1.Text + "%' ";
}

dateClass del5 = new dateClass();

del5.BindDataGridView(dataGridView1, sql);


}
上面的SQL语句在数据库里运行时是 没有问题的,但是运行时不能按条件查询
如图:


跪求大师指点

...全文
458 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
二毛 2015-08-10
  • 打赏
  • 举报
回复
好复杂…………
sunylf 2015-08-07
  • 打赏
  • 举报
回复
数据和金额的汇总有没有其他比较好的办法?
阿苗1 2015-08-07
  • 打赏
  • 举报
回复
引用 20 楼 yingbo833 的回复:
[quote=引用 19 楼 mhy8946 的回复:] 在if判断之前,你的sql语句是一个整体字符串,if判断之后,只是把符合条件的字符串拼接到之前的字符串后面,不能进行指定位置拼接,比如: sql="select语句1 union all select语句2" if判断之后,如果符合条件,那就把“条件”拼接在后面,最后sql变成“select语句1 union all select语句2 条件”,它不可能变成“select语句1 条件 union all select语句2”的。 如果想把条件加在第一个select后面,你可以将从union all开始到最后的字符串写在所有if之后,即 string sql = @" select tp.fname as 供应商名称,tm.fnumber as 物料编码,tre.fmodel as 规格型号,tre.fname as 物料名称, pd.Fbillno as 单据编号,pd.FCheckDate as 审核日期 ,pd.fdate as 单据日期, pod.Fdate as 交货日期,pod.FPrice as 单价,pod.fqty as 数量, pod.FAmount as 总金额,pod.FTaxAmount as 税额,pod.FAllAmount as 价税合计,(case pd.FStatus when '0' then '保存' when '1' then '审核' when '3' then '关闭' else null end)as 单据状态 from POOrderEntry pod left join POOrder pd on pod.FInterID=pd.FInterID left join t_icitemcore tre on tre.fitemid=pod.fitemid left join t_item tm on tm.fitemid=tre.fitemid left join t_user tu on tu.FuserID=pd.FBillerID left join t_Supplier tp on tp.fitemid=pd.fsupplyid where 1=1"; 。。。 这里是if判断条件。。。 。。。 sql += " union all select '合计','','','', '',null,null, null,null,sum(pod.fqty)as 数量, sum(pod.FAmount) as 总金额,sum(pod.FTaxAmount) as 税额, sum(pod.FAllAmount) as 价税合计,''from POOrderEntry pod left join POOrder pd on pod.FInterID=pd.FInterID left join t_icitemcore tre on tre.fitemid=pod.fitemid left join t_item tm on tm.fitemid=tre.fitemid left join t_user tu on tu.FuserID=pd.FBillerID left join t_Supplier tp on tp.fitemid=pd.fsupplyid where 1=1"; 这样就可以了
现在的问题是where 后面两边都要加条件 正确的SQL应该是 select tp.fname as 供应商名称,tm.fnumber as 物料编码,tre.fmodel as 规格型号,tre.fname as 物料名称, pd.Fbillno as 单据编号,pd.FCheckDate as 审核日期 ,pd.fdate as 单据日期, pod.Fdate as 交货日期,pod.FPrice as 单价,pod.fqty as 数量, pod.FAmount as 总金额,pod.FTaxAmount as 税额,pod.FAllAmount as 价税合计,(case pd.FStatus when '0' then '保存' when '1' then '审核' when '3' then '关闭' else null end)as 单据状态 from POOrderEntry pod left join POOrder pd on pod.FInterID=pd.FInterID left join t_icitemcore tre on tre.fitemid=pod.fitemid left join t_item tm on tm.fitemid=tre.fitemid left join t_user tu on tu.FuserID=pd.FBillerID left join t_Supplier tp on tp.fitemid=pd.fsupplyid where 1=1 and tm.fnumber LIKE '%3.1001.2015%' union all select '合计','','','', '',null,null, null,null,sum(pod.fqty)as 数量, sum(pod.FAmount) as 总金额,sum(pod.FTaxAmount) as 税额,sum(pod.FAllAmount) as 价税合计,'' from POOrderEntry pod left join POOrder pd on pod.FInterID=pd.FInterID left join t_icitemcore tre on tre.fitemid=pod.fitemid left join t_item tm on tm.fitemid=tre.fitemid left join t_user tu on tu.FuserID=pd.FBillerID left join t_Supplier tp on tp.fitemid=pd.fsupplyid where 1=1 and tm.fnumber LIKE '%3.1001.2015%'[/quote] 如果你两个where后加的条件都是一样的话,那你可以写两个sql语句,在if判断里分别进行拼接,最后在把两个sql语句拼接为一个就行了
yingbo833 2015-08-07
  • 打赏
  • 举报
回复
调试了下正确的代码是: private void button3_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox2.Text) && string.IsNullOrEmpty(textBox3.Text) && string.IsNullOrEmpty(textBox4.Text) && string.IsNullOrEmpty(textBox5.Text) && string.IsNullOrEmpty(comboBox1.Text) && string.IsNullOrEmpty(comboBox2.Text)&&string.IsNullOrEmpty(textBox6.Text) && string.IsNullOrEmpty(comboBox3.Text)) { MessageBox.Show("请输入查询条件!"); return; } string sql = @" select tp.fname as 供应商名称,tm.fnumber as 物料编码,tre.fmodel as 规格型号,tre.fname as 物料名称, pd.Fbillno as 单据编号,pd.FCheckDate as 审核日期 ,pd.fdate as 单据日期, pod.Fdate as 交货日期,pod.FPrice as 单价,pod.fqty as 数量, pod.FAmount as 总金额,pod.FTaxAmount as 税额,pod.FAllAmount as 价税合计,(case pd.FStatus when '0' then '保存' when '1' then '审核' when '3' then '关闭' else null end)as 单据状态 from POOrderEntry pod left join POOrder pd on pod.FInterID=pd.FInterID left join t_icitemcore tre on tre.fitemid=pod.fitemid left join t_item tm on tm.fitemid=tre.fitemid left join t_user tu on tu.FuserID=pd.FBillerID left join t_Supplier tp on tp.fitemid=pd.fsupplyid where 1=1"; String p = comboBox1.Text; if (!string.IsNullOrEmpty(textBox1.Text)) { var fstCondition = textBox1.Text.Trim(); if (fstCondition.Length > 0) sql= sql + "and tm.fnumber LIKE '%" + textBox1.Text + "%'"; } if (!string.IsNullOrEmpty(textBox2.Text)) { var secCondition = textBox2.Text.Trim(); if (secCondition.Length > 0) sql = sql + "and tre.fname LIKE '%" + textBox2.Text + "%' "; } if (!string.IsNullOrEmpty(textBox3.Text)) { var secCondition1 = textBox3.Text.Trim(); if (secCondition1.Length > 0) sql = sql + "and tre.fmodel LIKE '%" + textBox3.Text + "%' "; } if (!string.IsNullOrEmpty(textBox4.Text)) { var secCondition2 = textBox4.Text.Trim(); if (secCondition2.Length > 0) sql = sql + "and convert(varchar,pd.fdate,120) LIKE '%" + textBox4.Text.Trim() + "%' "; } if (!string.IsNullOrEmpty(textBox5.Text)) { var secCondition3 = textBox5.Text.Trim(); if (secCondition3.Length > 0) sql = sql + "and convert(varchar,pod.fdate,120) LIKE '%" + textBox5.Text + "%' "; } if (!string.IsNullOrEmpty(textBox6.Text)) { var secCondition7 = textBox6.Text.Trim(); if (secCondition7.Length > 0) sql = sql + "and Fbillno LIKE '%" + textBox6.Text + "%' "; } if (!string.IsNullOrEmpty(comboBox1.Text)) { var secCondition4 = comboBox1.Text.Trim(); if (secCondition4.Length > 0) sql = sql + "and pd.FStatus LIKE '" + p.Substring(0, 1) + "%' "; } if (!string.IsNullOrEmpty(comboBox2.Text)) { var secCondition9 = comboBox2.Text.Trim(); if (secCondition9.Length > 0) sql = sql + "and tu.fname LIKE '%" + comboBox2.Text + "%' "; } if (!string.IsNullOrEmpty(comboBox3.Text)) { var secCondition10 = comboBox3.Text.Trim(); if (secCondition10.Length > 0) sql = sql + "and tp.fname LIKE '%" + comboBox3.Text + "%' "; } sql+="union all select '合计','','','','',null,null, null,null,sum(pod.fqty)as 数量, sum(pod.FAmount) as 总金额,sum(pod.FTaxAmount) as 税额,"; sql+="sum(pod.FAllAmount) as 价税合计,''from POOrderEntry pod left join"; sql += " POOrder pd on pod.FInterID=pd.FInterID left join t_icitemcore tre on tre.fitemid=pod.fitemid left join"; sql += " t_item tm on tm.fitemid=tre.fitemid left join t_user tu on tu.FuserID=pd.FBillerID left join"; sql += " t_Supplier tp on tp.fitemid=pd.fsupplyid where 1=1 and tm.fnumber LIKE '%" + textBox1.Text + "%'"; sql +="and tre.fname LIKE '%" + textBox2.Text + "%'and tre.fmodel LIKE '%" + textBox3.Text + "%' "; sql += " and convert(varchar,pd.fdate,120) LIKE '%" + textBox4.Text.Trim() + "%'"; sql += "and convert(varchar,pod.fdate,120) LIKE '%" + textBox5.Text + "%' "; sql += "and Fbillno LIKE '%" + textBox6.Text + "%' and pd.FStatus LIKE '" + p.Substring(0, 1) + "%'"; sql += "and tu.fname LIKE '%" + comboBox2.Text + "%' and tp.fname LIKE '%" + comboBox3.Text + "%'"; dateClass del10 = new dateClass(); del10.BindDataGridView(dataGridView1, sql); }
yingbo833 2015-08-07
  • 打赏
  • 举报
回复
在C#中怎么去实现
yingbo833 2015-08-07
  • 打赏
  • 举报
回复
引用 19 楼 mhy8946 的回复:
在if判断之前,你的sql语句是一个整体字符串,if判断之后,只是把符合条件的字符串拼接到之前的字符串后面,不能进行指定位置拼接,比如:
sql="select语句1 union all select语句2"
if判断之后,如果符合条件,那就把“条件”拼接在后面,最后sql变成“select语句1 union all select语句2 条件”,它不可能变成“select语句1 条件 union all select语句2”的。

如果想把条件加在第一个select后面,你可以将从union all开始到最后的字符串写在所有if之后,即
string sql = @"
select tp.fname as 供应商名称,tm.fnumber as 物料编码,tre.fmodel as 规格型号,tre.fname as 物料名称,
pd.Fbillno as 单据编号,pd.FCheckDate as 审核日期 ,pd.fdate as 单据日期,
pod.Fdate as 交货日期,pod.FPrice as 单价,pod.fqty as 数量,
pod.FAmount as 总金额,pod.FTaxAmount as 税额,pod.FAllAmount as 价税合计,(case pd.FStatus when '0' then '保存'
when '1' then '审核' when '3' then '关闭' else null end)as 单据状态
from POOrderEntry pod left join
POOrder pd on pod.FInterID=pd.FInterID left join t_icitemcore tre on tre.fitemid=pod.fitemid
left join t_item tm on tm.fitemid=tre.fitemid left join t_user tu on tu.FuserID=pd.FBillerID
left join t_Supplier tp on tp.fitemid=pd.fsupplyid where 1=1";
。。。
这里是if判断条件。。。
。。。
sql += " union all
select '合计','','','',
'',null,null, null,null,sum(pod.fqty)as 数量, sum(pod.FAmount) as 总金额,sum(pod.FTaxAmount) as 税额,
sum(pod.FAllAmount) as 价税合计,''from POOrderEntry pod left join
POOrder pd on pod.FInterID=pd.FInterID left join t_icitemcore tre on tre.fitemid=pod.fitemid
left join t_item tm on tm.fitemid=tre.fitemid left join t_user tu on tu.FuserID=pd.FBillerID
left join t_Supplier tp on tp.fitemid=pd.fsupplyid where 1=1";
这样就可以了

现在的问题是where 后面两边都要加条件
正确的SQL应该是
select tp.fname as 供应商名称,tm.fnumber as 物料编码,tre.fmodel as 规格型号,tre.fname as 物料名称,
pd.Fbillno as 单据编号,pd.FCheckDate as 审核日期 ,pd.fdate as 单据日期,
pod.Fdate as 交货日期,pod.FPrice as 单价,pod.fqty as 数量,
pod.FAmount as 总金额,pod.FTaxAmount as 税额,pod.FAllAmount as 价税合计,(case pd.FStatus when '0' then '保存'
when '1' then '审核' when '3' then '关闭' else null end)as 单据状态
from POOrderEntry pod left join
POOrder pd on pod.FInterID=pd.FInterID left join t_icitemcore tre on tre.fitemid=pod.fitemid
left join t_item tm on tm.fitemid=tre.fitemid left join t_user tu on tu.FuserID=pd.FBillerID
left join t_Supplier tp on tp.fitemid=pd.fsupplyid where 1=1 and tm.fnumber LIKE '%3.1001.2015%' union all
select '合计','','','',
'',null,null,
null,null,sum(pod.fqty)as 数量,
sum(pod.FAmount) as 总金额,sum(pod.FTaxAmount) as 税额,sum(pod.FAllAmount) as 价税合计,''

from POOrderEntry pod left join
POOrder pd on pod.FInterID=pd.FInterID left join t_icitemcore tre on tre.fitemid=pod.fitemid
left join t_item tm on tm.fitemid=tre.fitemid left join t_user tu on tu.FuserID=pd.FBillerID
left join t_Supplier tp on tp.fitemid=pd.fsupplyid where 1=1 and tm.fnumber LIKE '%3.1001.2015%'
Poopaye 2015-08-06
  • 打赏
  • 举报
回复
where 1=1and 还是因为这边少了个空格?
Poopaye 2015-08-06
  • 打赏
  • 举报
回复
你用的条件判断全是LIKE,就是这种结果,很难理解吗?
yingbo833 2015-08-06
  • 打赏
  • 举报
回复
引用 6 楼 Z65443344 的回复:
既然你要做筛选
必然是在两个表里都做筛选
一个明细表,一个统计表

你只在统计表里做筛选,那么只有合计行的数据是正确的,前面的明细,不还是都查询出来了

你没有明白我的意思,我的意思怎么不按条件来查询
请看截图:
於黾 2015-08-06
  • 打赏
  • 举报
回复
既然你要做筛选 必然是在两个表里都做筛选 一个明细表,一个统计表 你只在统计表里做筛选,那么只有合计行的数据是正确的,前面的明细,不还是都查询出来了
yingbo833 2015-08-06
  • 打赏
  • 举报
回复
断点查询后的代码:

select tp.fname as 供应商名称,tm.fnumber as 物料编码,tre.fmodel as 规格型号,tre.fname as 物料名称,
pd.Fbillno as 单据编号,pd.FCheckDate as 审核日期 ,pd.fdate as 单据日期,
pod.Fdate as 交货日期,pod.FPrice as 单价,pod.fqty as 数量,
pod.FAmount as 总金额,pod.FTaxAmount as 税额,pod.FAllAmount as 价税合计,(case pd.FStatus when '0' then '保存'
when '1' then '审核' when '3' then '关闭' else null end)as 单据状态
from POOrderEntry pod left join
POOrder pd on pod.FInterID=pd.FInterID left join t_icitemcore tre on tre.fitemid=pod.fitemid
left join t_item tm on tm.fitemid=tre.fitemid left join t_user tu on tu.FuserID=pd.FBillerID
left join t_Supplier tp on tp.fitemid=pd.fsupplyid where 1=1
union all
select '合计','','','',
'',null,null, null,null,sum(pod.fqty)as 数量, sum(pod.FAmount) as 总金额,sum(pod.FTaxAmount) as 税额,
sum(pod.FAllAmount) as 价税合计,''from POOrderEntry pod left join
POOrder pd on pod.FInterID=pd.FInterID left join t_icitemcore tre on tre.fitemid=pod.fitemid
left join t_item tm on tm.fitemid=tre.fitemid left join t_user tu on tu.FuserID=pd.FBillerID
left join t_Supplier tp on tp.fitemid=pd.fsupplyid where 1=1and tm.fnumber LIKE '%3.1001.2015%'
附图

c#中的代码:

代码怎么不跑到第一个where条件后面,要怎么处理,求指点
阿苗1 2015-08-06
  • 打赏
  • 举报
回复
在if判断之前,你的sql语句是一个整体字符串,if判断之后,只是把符合条件的字符串拼接到之前的字符串后面,不能进行指定位置拼接,比如: sql="select语句1 union all select语句2" if判断之后,如果符合条件,那就把“条件”拼接在后面,最后sql变成“select语句1 union all select语句2 条件”,它不可能变成“select语句1 条件 union all select语句2”的。 如果想把条件加在第一个select后面,你可以将从union all开始到最后的字符串写在所有if之后,即 string sql = @" select tp.fname as 供应商名称,tm.fnumber as 物料编码,tre.fmodel as 规格型号,tre.fname as 物料名称, pd.Fbillno as 单据编号,pd.FCheckDate as 审核日期 ,pd.fdate as 单据日期, pod.Fdate as 交货日期,pod.FPrice as 单价,pod.fqty as 数量, pod.FAmount as 总金额,pod.FTaxAmount as 税额,pod.FAllAmount as 价税合计,(case pd.FStatus when '0' then '保存' when '1' then '审核' when '3' then '关闭' else null end)as 单据状态 from POOrderEntry pod left join POOrder pd on pod.FInterID=pd.FInterID left join t_icitemcore tre on tre.fitemid=pod.fitemid left join t_item tm on tm.fitemid=tre.fitemid left join t_user tu on tu.FuserID=pd.FBillerID left join t_Supplier tp on tp.fitemid=pd.fsupplyid where 1=1"; 。。。 这里是if判断条件。。。 。。。 sql += " union all select '合计','','','', '',null,null, null,null,sum(pod.fqty)as 数量, sum(pod.FAmount) as 总金额,sum(pod.FTaxAmount) as 税额, sum(pod.FAllAmount) as 价税合计,''from POOrderEntry pod left join POOrder pd on pod.FInterID=pd.FInterID left join t_icitemcore tre on tre.fitemid=pod.fitemid left join t_item tm on tm.fitemid=tre.fitemid left join t_user tu on tu.FuserID=pd.FBillerID left join t_Supplier tp on tp.fitemid=pd.fsupplyid where 1=1"; 这样就可以了
道玄希言 2015-08-06
  • 打赏
  • 举报
回复
引用 17 楼 yingbo833 的回复:
[/code]
此方法行不通,还是赋值到了统计部分去了[/quote] 将执行 sql = string.Format(sql, whr, whr); 这一句之前的 SQL 和之后的SQL语句贴出来。
yingbo833 2015-08-06
  • 打赏
  • 举报
回复
引用 16 楼 yangb0803 的回复:
你的第一部分,SQL查询语句写完后, 后边的条件语句没有赋值到查询中去,而只是赋值到了统计部分去了。


string sql = @"
                           select tp.fname as 供应商名称,tm.fnumber as 物料编码,tre.fmodel as 规格型号,tre.fname as 物料名称,
                            pd.Fbillno as 单据编号,pd.FCheckDate as 审核日期 ,pd.fdate as 单据日期,
                        pod.Fdate as 交货日期,pod.FPrice as 单价,pod.fqty as 数量, 
                         pod.FAmount as 总金额,pod.FTaxAmount as 税额,pod.FAllAmount as 价税合计,(case pd.FStatus  when '0' then '保存' 
                   when '1' then '审核' when '3' then '关闭' else null end)as 单据状态
                    from POOrderEntry pod left join
                    POOrder pd on pod.FInterID=pd.FInterID left join t_icitemcore tre on tre.fitemid=pod.fitemid
                    left join t_item tm on tm.fitemid=tre.fitemid left join t_user tu on tu.FuserID=pd.FBillerID
                 left join t_Supplier tp on tp.fitemid=pd.fsupplyid where 1=1  {0}  
                    union all
                    select '合计','','','',
                  '',null,null, null,null,sum(pod.fqty)as 数量, sum(pod.FAmount) as 总金额,sum(pod.FTaxAmount) as 税额,
                   sum(pod.FAllAmount) as 价税合计,''from POOrderEntry pod left join
                    POOrder pd on pod.FInterID=pd.FInterID left join t_icitemcore tre on tre.fitemid=pod.fitemid
                    left join t_item tm on tm.fitemid=tre.fitemid left join t_user tu on tu.FuserID=pd.FBillerID
                 left join t_Supplier tp on tp.fitemid=pd.fsupplyid where 1=1  {1} ";    //添加两个参数进去
                   

            String p = comboBox1.Text;
            string whr = string.Empty ;
            if (!string.IsNullOrEmpty(textBox1.Text))
            {
                var fstCondition = textBox1.Text.Trim();

                if (fstCondition.Length > 0)
                    whr= whr+ "and tm.fnumber LIKE '%" + textBox1.Text + "%'";
            }
            ......条件部分,你自己补充完整, 將sql 替换成 whr......

            
           if (!string.IsNullOrEmpty(comboBox3.Text))
            {
                var secCondition10 = comboBox3.Text.Trim();

                if (secCondition10.Length > 0)
                    whr= whr+ "and tp.fname LIKE '%" + comboBox3.Text + "%' ";
            }
           
             //添上这句
             sql = string.Format(sql, whr,  whr);  

             //调试时,可在上面这句设置断点,拷贝出SQL 语句,去查询分析器里面执行看是否正确。

            dateClass del10 = new dateClass();

            del10.BindDataGridView(dataGridView1, sql);

此方法行不通,还是赋值到了统计部分去了
道玄希言 2015-08-06
  • 打赏
  • 举报
回复
你的第一部分,SQL查询语句写完后, 后边的条件语句没有赋值到查询中去,而只是赋值到了统计部分去了。


string sql = @"
                           select tp.fname as 供应商名称,tm.fnumber as 物料编码,tre.fmodel as 规格型号,tre.fname as 物料名称,
                            pd.Fbillno as 单据编号,pd.FCheckDate as 审核日期 ,pd.fdate as 单据日期,
                        pod.Fdate as 交货日期,pod.FPrice as 单价,pod.fqty as 数量, 
                         pod.FAmount as 总金额,pod.FTaxAmount as 税额,pod.FAllAmount as 价税合计,(case pd.FStatus  when '0' then '保存' 
                   when '1' then '审核' when '3' then '关闭' else null end)as 单据状态
                    from POOrderEntry pod left join
                    POOrder pd on pod.FInterID=pd.FInterID left join t_icitemcore tre on tre.fitemid=pod.fitemid
                    left join t_item tm on tm.fitemid=tre.fitemid left join t_user tu on tu.FuserID=pd.FBillerID
                 left join t_Supplier tp on tp.fitemid=pd.fsupplyid where 1=1  {0}  
                    union all
                    select '合计','','','',
                  '',null,null, null,null,sum(pod.fqty)as 数量, sum(pod.FAmount) as 总金额,sum(pod.FTaxAmount) as 税额,
                   sum(pod.FAllAmount) as 价税合计,''from POOrderEntry pod left join
                    POOrder pd on pod.FInterID=pd.FInterID left join t_icitemcore tre on tre.fitemid=pod.fitemid
                    left join t_item tm on tm.fitemid=tre.fitemid left join t_user tu on tu.FuserID=pd.FBillerID
                 left join t_Supplier tp on tp.fitemid=pd.fsupplyid where 1=1  {1} ";    //添加两个参数进去
                   

            String p = comboBox1.Text;
            string whr = string.Empty ;
            if (!string.IsNullOrEmpty(textBox1.Text))
            {
                var fstCondition = textBox1.Text.Trim();

                if (fstCondition.Length > 0)
                    whr= whr+ "and tm.fnumber LIKE '%" + textBox1.Text + "%'";
            }
            ......条件部分,你自己补充完整, 將sql 替换成 whr......

            
           if (!string.IsNullOrEmpty(comboBox3.Text))
            {
                var secCondition10 = comboBox3.Text.Trim();

                if (secCondition10.Length > 0)
                    whr= whr+ "and tp.fname LIKE '%" + comboBox3.Text + "%' ";
            }
           
             //添上这句
             sql = string.Format(sql, whr,  whr);  

             //调试时,可在上面这句设置断点,拷贝出SQL 语句,去查询分析器里面执行看是否正确。

            dateClass del10 = new dateClass();

            del10.BindDataGridView(dataGridView1, sql);

於黾 2015-08-06
  • 打赏
  • 举报
回复
要么你不要使用union all 而是先查出明细,再在内存里做合并 比如用linq,或者自己循环累加出合计行
於黾 2015-08-06
  • 打赏
  • 举报
回复
把你后面拼接字符串的部分封装个方法 然后在2个select后面都调用一次这个方法 也就是所有的查询条件,必须同时作用于2个select语句
於黾 2015-08-06
  • 打赏
  • 举报
回复
什么系统自动生成的,不是你自己用字符串拼接的吗 既然你一个查询用到了2个select语句 当然后面要分别跟where条件 你只给后面的select语句加where条件,而前面的不加where条件 可不是有多少记录查多少记录吗
yingbo833 2015-08-06
  • 打赏
  • 举报
回复
引用 10 楼 Z65443344 的回复:
[quote=引用 7 楼 yingbo833 的回复:] [quote=引用 6 楼 Z65443344 的回复:] 既然你要做筛选 必然是在两个表里都做筛选 一个明细表,一个统计表 你只在统计表里做筛选,那么只有合计行的数据是正确的,前面的明细,不还是都查询出来了
你没有明白我的意思,我的意思怎么不按条件来查询 请看截图: [/quote] 因为你用了union all 其实是查询了2次 而你的条件全写在了后面,也就是对第二次查询做了筛选,但是第一次查询没有做筛选[/quote] 条件在后面是系统自动生成的,我也想把条件放到前面
於黾 2015-08-06
  • 打赏
  • 举报
回复
你可以看到,如果输入的查询条件不同,合计行的内容应该是不同的 但是前面的明细都是相同的
加载更多回复(5)

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧