Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sub titles in DBGrid

Status
Not open for further replies.

paul555

Technical User
Aug 7, 2001
4
GB
Does anybody know how to put a sub title into a grid so that it can straddle the entire grid but with the following records normally placed in columns (then followed by another sub title and then more normal records and so on)?
 
Whooee, that doesn't sound easy.

One way to get a similar effect would be to load records from the table into a treeview, and use the treeview's functionality to get what you want. Have a look around Torry's, and I'm sure you'll be able to find a data-aware treeview, and probably you can find one that automates most of what you want and is blazingly fast.

A partial solution would be to provide a custom draw for your DBGrid:
Code:
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
var i, wid: Integer;
begin
  if thisisasubtitlerow then begin
    DBGrid1.Canvas.Brush.Color := clLime;

    wid := 3;
    for i := 0 to DataCol - 1 do
      wid := wid + DBGrid1.Columns[i].Width - 1;

    DBGrid1.Canvas.TextRect(Rect, Rect.Left - wid, Rect.Top + 2, 'mysubtitle')
  end else
    DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
This is cut down and modified from some other code of mine, so you'll have to do some testing. It replaces rows that match your subtitle condition with your subtitle text. The "wid" stuff is to figure out how far off the left of the cell to start drawing, so that this cell will contain an accurate continuation of the subtitle text. Note that this does not get rid of the DBGrid's column lines between cells. It also does not add a row for the subtitle to inhabit -- it just replaces an existing row. You might be able to add rows appropriately using some sort of SQL funkiness, but a solution does not immedaitely enter my head.

If you just want to highlight changes in the value of some leftmost column, another way out might be to change background color every time your key value changes, and then call the default draw from there:
Code:
    DBGrid1.Canvas.Brush.Color := mynewcolor;
    DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
You'd have some list of background colors that you'd be cycling through, and a global variable to remember which color comes next.
-- Doug Burbidge mailto:doug@ultrazone.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top