日期:2014-05-20  浏览次数:21071 次

csdn的朋友帮帮忙啊~~~DataGridView 的合计项 问题
请查看下图
http://www.globalmarket.com/gb/ls/god3.jpg

数据是从数据库读出到datatable然后在窗口底部添加合计项
string   selectCommand= "select   ID,GXID,GXname,GXprice   from   WS_GX   where   ProductID=1 ";
dataAdapter   =   new   OleDbDataAdapter(selectCommand,   conn);
DataSet   ds   =   new   DataSet();
dataAdapter.Fill(ds,   "WS_GX ");                                
DataTable   table   =   ds.Tables[ "WS_GX "];
object   objSum   =   table.Compute( "Sum(GXprice) ",   null);
bindingSource1.DataSource   =   table;

上面代码中objSum是要来计算合计的

但无论我的
selectCommand= "select   ID,GXID,GXname,GXprice   from   WS_GX   where   ProductID=1 ";
还是
selectCommand= "select   ID,GXID,GXname,GXprice   from   WS_GX ";


合计项的值都是按照数据库中所有项目加起来,导致合计错误,应该如何解决啊????

------解决方案--------------------
没有new row?
bindingSource1.DataSource = table;?
//偶写的一个不恰当的函数
private void SelData(string selquery,string tablename)
{
OleDbDataAdapter da = new OleDbDataAdapter(selquery, mc);
DataSet ds = new DataSet();
da.Fill(ds, tablename);
DataTable dt = ds.Tables[tablename];
DataRow dr = ds.Tables[tablename].NewRow();
dataGridView1.DataSource = ds.Tables[tablename];
object objSum = dt.Compute( "Sum(GXprice) ", null);
string san = objSum.ToString();
if (san== " ")//GXprice 为空时..如果无有为空的可省此判断!
{
objSum = (object)0;
}
dr[GXprice] = objSum.ToString();
ds.Tables[tablename].Rows.Add(dr);
}