Code Translation Needed

I need to replicate this code on a DBGrid.

What it basically does is re-numbers column 0 (replaces it's contents with inc numbers 1 to end)
The code below works on a istView but I have no clue of how to code the same thing for a DBGrid.

Here's the code:  

for i := 0 to listView1.Items.Count-1 do
       listView1.Items[i].Caption :=intToStr(i+1);

Hope you can help!


ST3VOAsked:
Who is Participating?
 
rfwoolfCommented:
Fortunately there is more than one way to skin a cat!

In the following solution, the change takes place in the Dataset itself:
procedure TForm1.Button2Click(Sender: TObject);
var
i : integer;
begin
  for i  := 0 to table1.Fields.Count - 1 do
  begin
    table1.Fields[i].DisplayLabel := inttostr(i + 1);
  end;
end;
0
 
xr1140Commented:

column 0 of the DBGrid is empty ?
0
 
rfwoolfCommented:
I have put this in a button's OnClick event handler
Note that this only renames the columns at run time, and will not change them permanently.

procedure TForm1.Button1Click(Sender: TObject);
var
i : integer;
begin
for i := 0 to (dbgrid1.Columns.Count - 1) do
begin
  dbGrid1.Columns[i].title.caption := inttostr(i + 1);
end;
end;
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
ST3VOAuthor Commented:
Hmm It doesn't like: title.caption for some reason and no the Column is populated and I need to overwrite it!
0
 
rfwoolfCommented:
Hi ST3VO

Can you please explain the error that comes when you say
"hmm it doesn't like title.caption"

Yes in my example I had connected a DBGrid to a datasource which was connected to a dataset
e.g. TDBGrid <-- TDataSource <-- TTable
(TTable is a BDE dataset, but you can use a qery or ADO or Interbase etc)

Once the DBGrid is populated (because I set the dataset to 'open' or its Active property to 'true') I then clicked on the button and it worked.

Can you please explain the error that comes when you say
"hmm it doesn't like title.caption"
0
 
rfwoolfCommented:
Oh i must add:
when using "dbGrid1.Columns[i].title.caption" for some reason you MUST give it an index (i.e. [ X ] <-- square brackets) - even though Delphi wasn't giving me any options to.
If you don't provide an index, you cannot say ".title.caption"

Hope that helps
0
 
ST3VOAuthor Commented:
Sorry, here is the error:

[Pascal Error] Unit1.pas(131): E2003 Undeclared identifier: 'title'
0
 
rfwoolfCommented:
oh here's what i missed, can you please add 'grids' and 'dbgrids' to your form's uses unit - my version of delphi did this for me automatically when i first type it and compile - maybe yours doesn't. Please try again and let me know if there's still an error
0
 
ST3VOAuthor Commented:
I don't know if this might make a different but it's actually AdvDBGrid that I'm using...I just thought the code might now change.
0
 
ST3VOAuthor Commented:
Sorry it's DBAdvGrid

0
 
rfwoolfCommented:
thats a  commercial component by TMS. im afraid i dont have it, sorry. try the jedi's jvdbultimgrid (open source)
0
 
ST3VOAuthor Commented:
Thanks, and sorry about not specifing it at the beginning. I really thought it would be the same.

0
 
ST3VOAuthor Commented:
Thanks :o)

I just got a little problem with it.

The main code changed the subitems of the 1st column.

This one is changing the Header (Across)...

Right now the first column looks like this:

ID
1
34
55
2
54
66
77
etc...

I need it to change it to this:

ID
1
2
3
4
5
6
7
etc

0
 
ST3VOAuthor Commented:
I wrote to TMS the creators of the component and this is their response:

I'm not sure what you want to change, as with your original DBGrid
code , it looks like you wanted to change the column title.
If you want to change what field is displayed in a column, you can use
grid.Columns[i].FieldName:string;

The problem is that it doesn't work either.

I get errors of the time says...Field 1 not found....Field 12 not found etc...
0
 
diniludCommented:
procedure TForm1.Button1Click(Sender: TObject);
begin
  With DBGrid1.DataSource.DataSet do
  begin
   DisableControls;
   try
     First;
     while not Eof do
     begin
       FieldByName(DBGrid1.Columns[0].FieldName).Value :=inttostr(RecNo);
     end;
   finally
     EnableControls;
   end;
  end;
end;
0
 
ST3VOAuthor Commented:
I got an error:

ADOQuery1 Field '' Not Found.

Any Ideas why?
0
 
ST3VOAuthor Commented:
Thanks Guys!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.