0

I want to add new entries generated by logs at run time from my software at the bottom of StringGrid widget (20 rows and 2 columns) 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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

3

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...
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770