?
Solved

Problem with DBAdvGrid (TMS)

Posted on 2007-10-04
12
Medium Priority
?
1,455 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
0
Comment
Question by:ST3VO
  • 6
  • 3
  • 2
  • +1
12 Comments
 
LVL 10

Expert Comment

by:dinilud
ID: 20019693
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;
0
 

Author Comment

by:ST3VO
ID: 20020368
I got an error:

ADOQuery1 Field '' Not Found.

Any Ideas why?
0
 

Author Comment

by:ST3VO
ID: 20020486
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?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 10

Expert Comment

by:dinilud
ID: 20020549
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;
0
 

Author Comment

by:ST3VO
ID: 20020576
I get an error:

Field 'ID' cannot be modified :o/
0
 

Author Comment

by:ST3VO
ID: 20020681
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)
0
 
LVL 13

Accepted Solution

by:
rfwoolf earned 2000 total points
ID: 20021042
ST3VO

Okay, your first problem was that you were modifying the field's name instead of its value.
WRONG:
DBAdvGrid1.DataSource.dataset.FieldByName('ID').AsString := IntToStr(i+1);
RIGHT:
DBAdvGrid1.DataSource.dataset.FieldByName('ID').value := IntToStr(i+1);

In the wrong way you're saying "take the field (a TField) and read its string, and assign the read string"
--which it cannot do
in the correct way, you're saying "take the field (a TField) and take the value, and assign it to..."
--which is the way you asign values to fields :P
That mistake was corrected by dinilud.

===

In your second problem, it seems like you are trying to modify a primary index. As you may know when you design a database table you need to make sure that SOMEHOW every record is different, and the best way to do this is to add a field called 'ID' or 'RecordNo' that auto-increments its value every time a record is created.
This cannot usually be modified - the "server" decides on its value whenever the record is created.

Your options are:
1. Add yet another field to the table that allows you to dictate its value and change it whenever you want.
2. Add all the fields to your DB grid as persistent fields. Then add one to the list that you make up and set the value to. (Example on its way...)
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 20021118
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.
0
 

Author Comment

by:ST3VO
ID: 20021208
Thanks a lot for your time!!! :o)
0
 
LVL 13

Expert Comment

by:rfwoolf
ID: 20021276
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;
0
 
LVL 13

Expert Comment

by:rfwoolf
ID: 20021533
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"
0
 

Author Comment

by:ST3VO
ID: 20021607
Brilliant!!! Thanks again :o)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Screencast - Getting to Know the Pipeline
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question