My question is: How can I order a DBGrid by a calculated field. I am using the C++Builder Starter Editon and do not have a ClientDataSet available in this version to create an Index on the field and order by the index of a column.So this is not an option. (Read this in many threads) I am using an TIBDataSet (ibds below) and I am filtering the data. Works fine....for the DB-columns, not for the calculated ones... Any ideas of how I might get around this problem?

void __fastcall TForm1::DBGrid3TitleClick(TColumn *Column)
{
    static cIdx = 0;
    static String oby = "ASC";

    TBookmark CurrentPosition;
    TIBDataSet *ibds = IBDS_accountsDist;
    CurrentPosition = ibds->GetBookmark();
    if (cIdx != Column->Index) {
        oby = "ASC"; // ANOTHER column choosen
    } else if (oby == "ASC") {
        oby = "DESC";
    } else oby = "ASC";
    cIdx = Column->Index;

    ibds->Filtered = false;
    switch (Column->Index){
        case 0: ibds->Filter = "ORDER BY SumAj "+oby; break; // SumAj is a calculated field => Does not work
        case 1: ibds->Filter = "ORDER BY CSAL_ACCOUNTNAME "+ oby; break; // DB-field WORKS FINE
    }
    ibds->Filtered = true;
    ibds->GotoBookmark(CurrentPosition);
}
有帮助吗?

解决方案

You cannot do it. TIBDataSet is a representation of the underlying database. Basically it fetches the records in the order defined in the SQL.

The easiest way is to use TDBClientDataset but it is not included in Starter version of c++ Builder. You can explore other ways, for example pre-loading all records in a std::list and then use the order function to order the records. Finally you can show them using a simple TGrid o TStringGrid.

In any case, I recommend to upgrade C++Builder since TClientDataSet is one of the main pieces in most of data projects, specially when you need to create medium-large projects.

Mixing database specific components like TIBDataSet with the user interface penalizes the scalability and maintenance of the project.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top