• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

Replace Data on DBGrid

Hi Experts,

I've got a DBGrid populated and I need to replace the Column name and all the Column data from it.

For example:

I'm got a column called

'Column7'
-----------
somedata1
somedata2
etc

I need to change it to this:

'ImagePath'
-------------
SomePath
SomePath
etc...

The field somePath is always going to be the same.

Hope you can help!

Thanks

ST3VO
0
ST3VO
Asked:
ST3VO
  • 2
1 Solution
 
mokuleCommented:
You should populate DBGrid using Query.
If You want to change only the column's title You can dblclick dbgrid and edit columns.
An appropriate dataset should be active.
0
 
ST3VOAuthor Commented:
I need to do all the by code and not manually.

An example code would be perfect!
0
 
rfwoolfCommented:
Select your DBGrid, in the Object Inspector on the events tab, double-click on "OndrawColumnCell" to create an event handler for that event.

The make the event handler's procedure be as follows (where "DbGrid1" is the name of your DBGrid, the column you want to change is 'Column7', 'ImagePath' is the name you want the column to then be, and the new data will be 'SomePath':

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if column.Title.Caption = 'Column7' then
begin
  DBGrid1.canvas.TextRect(rect,rect.Left,rect.top,'somepath');
  column.Title.Caption := 'ImagePath';
end
else
  DBGrid1.canvas.TextRect(rect,rect.Left,rect.top,table1.fields[DataCol].AsString);
end;
0
 
ST3VOAuthor Commented:
Perfect! Thanks :o)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now