简体   繁体   中英

Adding New Entry at bottom of StringGrid in C++ Builder at run time

I want to add new enteries generated by logs at run time from my software at the bottom of StringGrid widget(20 rows and 2 coloumns) in C++ Builder 5.

Whether there is any Property of StringGrid widget which can automatically delete entry in top most row before adding new entry in bottom most row in case all rows of fixed size StringGrid is occupied with data.

Please inform me if you need any other information from me.

Many thanks

Saurabh Jain

As I told you in the Embarcadero forums ...

There is no public property/method for what you are asking for. You will just have to set/increment the RowCount property as needed, then manually shift up the contents of the Cells property, and then finally fill in the bottom row.

if (StringGrid1->RowCount < SomeMaxValue)
    StringGrid1->RowCount = StringGrid1->RowCount + 1;

for(int row = 1; row < StringGrid1->RowCount; ++row)
{
    for(int col; col < StringGrid1->ColCount; ++col)
    {
        StringGrid1->Cells[col][row-1] = StringGrid1->Cells[col][row];
    }
}

// fill in StringGrid1->Cells[...][StringGrid1->RowCount-1] for the last row as needed...

However, TStringGrid does have a protected DeleteRow() method, but being protected, you need to use an accessor class to reach it, eg:

class TStringGridAccess : public TStringGrid
{
public:
    void RemoveRow(int row) { TStringGrid::DeleteRow(row); }
};

if (StringGrid1->RowCount > 0)
    ((TStringGridAccess*)StringGrid1)->RemoveRow(0);

StringGrid1->RowCount = StringGrid1->RowCount + 1;
for(int row = 1; row < StringGrid1->RowCount; ++row)
{
    for(int col; col < StringGrid1->ColCount; ++col)
    {
        StringGrid1->Cells[col][row-1] = StringGrid1->Cells[col][row];
    }
}

// fill in StringGrid1->Cells[...][StringGrid1->RowCount-1] for the last row as needed...

That said, TStringGrid is really not the best choice for what you are trying to do. I would strongly suggest using TListView in vsReport mode instead. That is what I use in my log viewer, and it works very well, especially in virtual mode ( OwnerData=true ), and it just looks more natural than TStringGrid , since TListview is a wrapper for a native Windows control whereas TStringGrid is not.

if (ListView1->Items->Count > 0)
    ListView1->Items->Items[0]->Delete();

TListItem *Item = ListView1->Items->Add();
// fill in Item->Caption (column 0) and Item->SubItems (columns 1+) as needed...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM