首先,这是一个sql语句,实现的是分类统计售出数量 string strsql = "select TItemStockHistory.itemid,TItem.cname,sum(TItemStockHistory.quantity) as num "
                          + "from TItemStockHistory join TItem"
                          + " on TItemStockHistory.itemid=TItem.itemid where TItemStockHistory.historytype=5"
                          + " group by TItemStockHistory.itemid";
问题是:怎么把sum(TItemStockHistory.quantity) 这个值 填充到 datagirdview 里面去 ??完整代码如下:mds = new MainDataSet();
           
            newArrayList = new ArrayList();
            //DataController.adaptItemStockHistory.Fill(mds.TItemStockHistory);            string strsql = "select TItemStockHistory.itemid,sum(TItemStockHistory.quantity) as num "
                          + "from TItemStockHistory join TItem"
                          + " on TItemStockHistory.itemid=TItem.itemid where TItemStockHistory.historytype=5"
                          + " group by TItemStockHistory.itemid";            SqlDataAdapter adat = new SqlDataAdapter(strsql, DataController.adaptItemStockHistory.Connection);
            adat.Fill(mds.TItemStockHistory);
            //da1 = new BIMS.MainDataSetTableAdapters.TItemStockHistoryTableAdapter(strsql,DataController.adaptItemStockHistory.Connection);
            //da1.Fill(mds.TItemStockHistory);            newView = new DataView(mds.TItemStockHistory);
            newArrayList.Clear();
            foreach (DataRowView drv in newView)
            {
                 newArrayList.Add((MainDataSet.TItemStockHistoryRow)drv.Row);
            }
            this.gdvShowSold.Rows.Clear();
            for (int i = 0; i < newArrayList.Count; i++)
            {
                MainDataSet.TItemStockHistoryRow newRow = (MainDataSet.TItemStockHistoryRow)newArrayList[i];
                //MainDataSet.TItemRow itemRow=(MainDataSet.TItemRow)newArrayList[i];                this.gdvShowSold.Rows.Add();
                this.gdvShowSold.Rows[i].Cells["itemid"].Value =(newRow.IsitemidNull() ? "" : newRow.itemid.ToString());
                //this.gdvShowSold.Rows[i].Cells["cname"].Value=(itemRow.IscnameNull() ? "" : itemRow.cname);
                this.gdvShowSold.Rows[i].Cells["cname"].Value = "";
                this.gdvShowSold.Rows[i].Cells["sumsold"].Value =(newRow.IsquantityNull() ? "" : newRow.quantity.ToString());  
            }

解决方案 »

  1.   

    sum(TItemStockHistory.quantity) as num 不是已经指定了别名“num”了吗?把num当做列名读取就可以了
      

  2.   

    在你的dgv里再新加一行,把统计的数据加入新行。
      

  3.   

     this.gdvShowSold.Rows[i].Cells["sumsold"].Value =(newRow.IsquantityNull() ? "" : newRow.quantity.ToString()); 把quantity该为num 就可以了
      

  4.   


    sql 语句里面是 AS 成了 num 列,但是 newRow 的时候 . 不出来 这个列明,原始表 是没有这个字段的
      

  5.   

    还有 我想要把TItem表的 itemid相同的那个 cname 列的内容赋值给 控件的 cname列,代码该怎么 写?
     this.gdvShowSold.Rows[i].Cells["cname"].Value=(itemRow.IscnameNull() ? "" : itemRow.cname); ???
      

  6.   

    谢谢 大家了 刚才我没考虑完善 哎 技术不行啊 
    this.gdvShowSold.Rows[i].Cells["sumsold"].Value = newRow["sumsold"];就是这样实现的 直接绑定 列名 就可以了, 我晕死 太菜了