ClientDataSet CalcFields' odd trouble
I've got an odd and persistent problem... (Also my right shift key just stopped working, so please bear with my possibly odd caps & symbols; trying to get my fingers trained to use the left shift key is a pain.)
I'm running a CalcFields event when a ClientDataSet opens & goes through records (ie, AutoCalcFields is true). It takes every record and does a little modification to it, as in a field comes in with "88", it changes it to "$88.00" and puts it in a new field, and so on for formatting purposes. This works great, the only problem is that CalcFields seems to completely ignore the first record in the set. It shows the records in a DBGrid and the first record is there, but none of this formatting has been done to it. So I step through the code and CalcField never touches the first record. Well, actually, it DOES touch the first record, (I'm assuming). When I step through the code, CalcField fires off twice with two completely blank sets of information. I know CalcFields is executed when the dataset is open, and stepping through, that's where the first blank input comes from, which is fine. The second blank input comes in first when it's going through each record. Again, the first record shows up perfectly intact in the DBGrid, but completely blank on Calcfield.
Does anybody have any idea on why this is happening? This is driving me crazy and I've tracked it up and down and can't figure out what the heck is going on.
Here's the CalcField code, for what it's worth:
void __fastcall TDataModule1::sdsSEARCHCalcFields(TDataSet *DataSet)
{
// to view for debugging....
DataSet->FieldByName("MISBN")->AsString;
AnsiString formattedField;
String field;
double dInputPower;
dInputPower = DataSet->FieldByName("MBILL$")->AsFloat;
formattedField = FormatFloat("#,##0.00", dInputPower);
DataSet->FieldByName("BILL")->Text = formattedField;
dInputPower = DataSet->FieldByName("MTGUID")->AsFloat;
formattedField = FormatFloat("#,##0.00", dInputPower);
DataSet->FieldByName("GUID")->Text = formattedField;
field = DataSet->FieldByName("MISBN")->AsString;
int lght = field.Length();
String str = field.SubString(14, 1);
if (field.Length() > 13 && field.SubString(14, 1) == ".")
{
DataSet->FieldByName("ISBN")->Text = field.SetLength(13);
}
}
//---------------------------------------------------------------------------
and here's the开发者_如何学JAVA code that executes it. note that it's a ctQuery...
DataModule1->sdsSEARCH->Active = false;
DataModule1->cdsSEARCH->Active = false;
DataModule1->dsSEARCH->Enabled = false;
DataModule1->sdsSEARCH->CommandType = ctQuery;
DataModule1->sdsSEARCH->CommandText = queryStr;
DataModule1->sdsSEARCH->Active = true;
DataModule1->cdsSEARCH->Active = true;
DataModule1->dsSEARCH->Enabled = true;
why not use the DisplayFormat property? for example, in in a TClientDataSet.AfterOpen event do something like this:
dynamic_cast<TNumericField*>(DataSet->FieldByName("quantity_to_date_adjustment"))->DisplayFormat = "#,##0.00;(-#,##0.00)";
精彩评论