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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading a DBGrid column into a string 1

Status
Not open for further replies.

topcat01

Programmer
Jul 10, 2003
83
GB
Hi,

I am a newbie to DB programming so apologies if this is a stupid question.

I have a very small database loaded into a DBGrid and I would like to save the contents of a single column to a string (the database contains < 20 rows). I can do this no problem using StringGrid by looping through the cells of a column but I am really stuck how to do this with DBGrid???

TIA
 
this is 1 way, there are many other.

If you want to get values of all columns of selected row from DBGrid component, then use Fields and FieldCount properties, like in this example.

Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  Memo1.Clear;
  with DBGrid1 do
  begin
    for i:=0 to FieldCount-1 do
      Memo1.Lines.Add(Fields[i].AsString);
  end;
end;


Aaron
John Mutch Electronics
 
heres another way, with a little modification you could adapt this

by accessing the data via the datasource.
Code:
procedure CopyRecord;
var varCopyData: Variant;
    i: Integer;
begin
  with DataSource.DataSet do
  begin
    varCopyData := VarArrayCreate([0, FieldCount-1], varVariant);
    for i := 0 to FieldCount-1 do
      varCopyData[i] := Fields[i].Value;

    Insert;

    for i := 0 to FieldCount-1 do
      Fields[i].Value := varCopyData[i];
  end;
end;


Aaron
John Mutch Electronics
 
Aaron, both your examples take all the fields of the current record, whereas the request was for help getting the values of every record for a single field. Presuming you want each record delimited with a comma, something like this would work:
Code:
[b]var[/b]
  s : String;
[b]begin[/b]
  [b]with[/b] DataSource.Dataset [b]do[/b]  [navy]// change to refer to actual dataset[/navy]
  [b]begin[/b]
    First;
    s := [teal]''[/teal];
    [b]while not [/b]EOF [b]do[/b]
    [b]begin[/b]
      [b]if[/b] s <> [teal]''[/teal] [b]then[/b]
        s := s + [teal]','[/teal];
      s := s + FieldByName([teal]'fieldname'[/teal]).AsString;
      Next;
    [b]end[/b];
  [b]end[/b];
[b]end[/b];
 
oops my bad.

also dont forget disablecontrols at the beginning
and enablecontrols at the end to prevent the table scrolling to be visible.

and to bookmark the current record first so you can return to it afterwards.

Aaron
John Mutch Electronics
 
Thanks guys, that worked well. I take it that it is always best practice to access data via datasource?

I have another question, forgive me if the answer may seem obvious but I have to ask. Example: I have one DB containing one table that is being accessed by two users, if one user is filtering the table using sql queries will the other connected user be affected in any way? TIA
 
Yes - while you can in most cases get the same data from a data control, best practice is to go direct to the datasource. There will always be less overhead this way - plus from a maintenance point of view, it allows changes to be made to the data controls (visual changes) without affecting anything else.

In answer to your second question, no. While SQL queries can be used to modify the original data, simply viewing a query with a SELECT statement regardless of the fields or formatting used will not affect the original data.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top