问题出现在红色代码处:
int CurProductID = (int)odr["Productid"];
while (CurProductID == (int)odr["Productid"])
{
DateTime dt = (DateTime)odr["OrderDate"];
int FieldIndex = SumByMonth? (dt.Month-1) : (dt.Month-1) / 3;
Report.DetailGrid.Recordset.Fields[m_AmtFieldIndexs[FieldIndex]].AsFloat += (double)odr["Amount"];
Report.DetailGrid.Recordset.Fields[m_QtyFieldIndexs[FieldIndex]].AsInteger += (int)(Int16)odr["Quantity"];
if ( !odr.Read() )
break;
}
经斑竹修改后:
private void ReportFetchRecord(ref bool Eof)
{
OleDbConnection ocnnNorthwind = new OleDbConnection(GridppReportDemo.Utility.GetDatabaseConnectionString());
OleDbCommand ocmd = new OleDbCommand("select d.Productid,p.ProductName,m.OrderDate,d.Quantity,d.UnitPrice*d.Quantity*(1-d.Discount) as Amount "
+ "from orders m inner join ([Order Details] d inner join Products p on d.ProductID=p.ProductID) "
+ "on m.orderid=d.orderid "
+ "where m.OrderDate between #1/1/97# And #12/31/97# "
+ "order by d.Productid,m.OrderDate", ocnnNorthwind);
ocnnNorthwind.Open();
OleDbDataReader odr = ocmd.ExecuteReader(CommandBehavior.CloseConnection);
int DataGroupCount = SumByMonth? 12 : 4;
int CurProductID = -1; //(int)odr["ProductID"];
while ( odr.Read() )
{
if (CurProductID != (int)odr["ProductID"])
{
//提交上一笔数据
if (CurProductID > 0)
Report.DetailGrid.Recordset.Post();
Report.DetailGrid.Recordset.Append();
//设初值
for (int i=0; i<DataGroupCount; ++i)
{
((IGRField)m_pAmtFields[i]).AsFloat = 0;
((IGRField)m_pQtyFields[i]).AsFloat = 0;
}
m_pProductIDField.Value = odr["ProductID"];
m_pProductNameField.Value = odr["ProductName"];
CurProductID = (int)odr["ProductID"];
}
DateTime dt = (DateTime)odr["OrderDate"];
int FieldIndex = SumByMonth? (dt.Month-1) : (dt.Month-1) / 3;
((IGRField)m_pAmtFields[FieldIndex]).AsFloat += (double)odr["Amount"];
((IGRField)m_pQtyFields[FieldIndex]).AsInteger += (int)(Int16)odr["Quantity"];
}
//提交最后一笔数据
Report.DetailGrid.Recordset.Post();
odr.Close();
}