We help IT Professionals succeed at work.

Problem with DBAdvGrid (TMS)

ST3VO
ST3VO asked
on
1,614 Views
Last Modified: 2012-06-21
Hi experts,

I really need some help here with the component.

I am trying to do the following:

In Column 0 Titled ID i've got this:

 ID
-----
3
4
65
34
21
34
etc

and I need to change it to:

ID
---
1
2
3
4
5
6
7
8
etc...

This is the latest code I'm trying:

procedure TForm1.Button1Click(Sender: TObject);
var
i : integer;
begin

for i := 0 to (DBAdvGrid1.Columns.Count - 1) do
begin

 DBAdvGrid1.Columns[i].FieldName:=inttostr(i + 1 );

 end;
end;

I get errors all the time:

ADOQuery1 Field 2 Not Found
ADOQuery1 Field 12 Not Found
etc...

Can anyone please help me?

Thanks

ST3VO
Comment
Watch Question

Top Expert 2007

Commented:
Try this


  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;

Author

Commented:
I got an error:

ADOQuery1 Field '' Not Found.

Any Ideas why?

Author

Commented:
UPDATE:

OK, This is as far as I've got:

Latest Code:

procedure TForm1.Button11Click(Sender: TObject);
var i: integer;
begin

for i := 0 to (DBAdvGrid1.Columns.Count - 1) do begin

  DBAdvGrid1.DataSource.dataset.First;
while not DBAdvGrid1.DataSource.DataSet.Eof do
begin
  DBAdvGrid1.DataSource.dataset.Edit;
  DBAdvGrid1.DataSource.dataset.FieldByName('ID').AsString := IntToStr(i+1);
  DBAdvGrid1.DataSource.dataset.Next;
end;
end;
end;

Now I get an error saying: Field 'ID' Cannot be modified.

I don't want to modify the 'ID' which is the header, I want to replace all the Column contents :o/

I also got another email from TMS see below:

To change the fieldnames, you'll need to use:

for i:=0 to dataset.Fields.Count -1 do
  dataset.Fields[i].FieldName := newname;


To change values in a column, you'll need:

dataset.First;
while not dataset.Eof do
begin
  dataset.FieldByName('fieldname').AsString := newvalue;
  dataset.Next;
end;

I've tried this and still nothing. I'm I need coding it properly?
Top Expert 2007

Commented:
procedure TForm1.Button1Click(Sender: TObject);
begin
  With DBGrid1.DataSource.DataSet do
  begin
   DisableControls;
   try
     First;
     while not Eof do
     begin
       edit;
       FieldByName('ID').Value :=inttostr(RecNo);
       post;
       next;
     end;
   finally
     EnableControls;
   end;
  end;
end;

Author

Commented:
I get an error:

Field 'ID' cannot be modified :o/

Author

Commented:
Hmm...I've been looking arround and it seems like the problem is the a TQuery cannot be modified?

Do I need to copy the results to another component and then change it there?

This is really doing me head now :o)
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Mike LittlewoodEngineer

Commented:
Do you actually want to change the values, or do you just want to display a different number to the user.
If you just want to display a different number, you could always create a calculated field on the dataset instead and show this as the ID column. This would get around any need for updating the query.

Author

Commented:
Thanks a lot for your time!!! :o)

Commented:
Okay here's another solution.
Unfortunately this is only for persistent datasets e.g. TTable NOT TQuery

Set up and connect your TTable component.
Double-click on it (a small window appears)
Right-click and say "Add all fields"
(Your fields are now persistent)
Click on the ID field
In the Object inspector, click on the 'Events' tab, and create an eventhandler for "OnGet Text"

Type "text := inttostr(table1.RecNo);"

Example:
procedure TForm1.Table1IDGetText(Sender: TField; var Text: string;
  DisplayText: Boolean);
begin
 text := inttostr(table1.RecNo);
end;

Commented:
Got it!

Here's the actual answer you were looking for

For this you will need a DBGrid, connected to a dataset (e.g. a TTable)

Click on your DBGrid, and in the Object Inspector in the Events tab, add an event handler called "OnDrawColumnCell".

then add the following to the event:

if column.Title.Caption = 'ID' then
DBGrid1.canvas.TextRect(rect,rect.Left,rect.top,inttostr(table1.RecNo))
else
DBGrid1.canvas.TextRect(rect,rect.Left,rect.top,table1.fields[DataCol].AsString);

HOW IT WORKS:
This overrides how the dbgrid draws its cells. -- specifically the text in its cells.
It says "If the column whose cell we're drawing is called "ID" then... " "set the text to the record number of the dataset"
else
"set the text to the actual data value"

Author

Commented:
Brilliant!!! Thanks again :o)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.