1

I have a PageControl with five-tabs. Three of the tabs have a DBGrid, all using the same columns and DataSource. When a user clicks on a Title I change IndexFieldNames and Bold the appropriate Title. Currently I am doing...

for i:=0 to dbg1.Columns..Count-1 do
begin
  dbg1.Columns[i].Title.Font.Style:=[];
  dbg2.Columns[i].Title.Font.Style:=[];
  dbg3.Columns[i].Title.Font.Style:=[];
end;
dbg1.Columns[Column.Index].Title.Font.Style:=[fsBold];
dbg2.Columns[Column.Index].Title.Font.Style:=[fsBold];
dbg1.Columns[Column.Index].Title.Font.Style:=[fsBold];
tblCustomer.IndexFieldNames:=Column.Name;

But, it has occurred to me that I could just have one DBGrid and swap the Parent as needed in the PageControl OnChange event.

dgb1.Parent:=TabSheet1;

or

dgb1.Parent:=TabSheet2;

Is there a down side to doing this? The Parent could be changed many dozens of times in one sitting.

2 Answers2

1

Yes, no problem.

As long as the owner of your DBGrid is not one of those tabsheets, but is higher up in the owning chain. Because otherwise, the destruction of tabsheet A could lead to the destruction of your DBGrid on tabsheet B.

Community
  • 1
  • 1
NGLN
  • 43,011
  • 8
  • 105
  • 200
  • And of course both comments from whosrdaddy and TLama hit the nail. – NGLN Sep 26 '14 at 14:56
  • Thanks, I will mess with it. I had the idea that TTabControl was deprecated and not to be used if at all possible, as the PageControl was the desired replacement. I will check online and see what I can learn. –  Sep 26 '14 at 19:42
  • 1
    TPageControl is derived from TTabControl.What is kind of deprecated is TTabSet (the one in the "Win3.1" palette) – Andreas Hausladen Sep 26 '14 at 19:47
0

If you are using a TClientDataset, you can do something even better: have one grid on each tab and use clones (method CloneCursor) to change your index. At that point, your DBGrids won't all point to the same dataset but each one to its own, where you set the IndexName (or fields, according to what you are doing).

This will be easier to maintain (each DBGrid does not mess with the others, if you need more just add DBGrids) and extensible (if you need more indexes you just create more clones).

The main drawback is that if you add a field, it has to go into all clones, which can be a problem if the application is evolving.

Another (completely different) option is to look at CodeCentral - John Kaster uploaded a DBGrid which lets you order by title. This was a long time ago, but it should still be there. It only works with Client datasets though. You could, however, change that and add your own datasets where needed,I think it comes with source code. Hope it helps.

Andrea Raimondi
  • 527
  • 8
  • 30