?
Solved

Acccessing DBGrid1 values using an array

Posted on 2004-04-16
15
Medium Priority
?
498 Views
Last Modified: 2010-04-05
Hey I would like to access my DBGrid using a two dimensional array like this
TStringGrid( DBGrid1 ).Cells[1,1] but it gives me "access violation" any other way this could be done?

Thanks
Erik
0
Comment
Question by:erik2000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +1
15 Comments
 
LVL 17

Accepted Solution

by:
mokule earned 280 total points
ID: 10846288
You certeinly don't like it, but...

// goto col, row
  DBGrid1.DataSource.DataSet.First;
  for i := 0 to row-1 do
    DBGrid1.DataSource.DataSet.Next;
  DBGrid1.Columns[col].Field.AsString;
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10846291
Erik2000,

This is not possible. Although both components are Grids and do share some similiar hierarchy, the Cells property is introduced in the TStringGrid class declaration. It does not exist in the Hierarchy prior to TStringGrid.

Hope this Helps!

What exactly are you trying to do, maybe we can suggest an alternative method (Such as the fields array property, or the FieldByName method.

Shane



0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10846473
Here is Mokules suggestion wrapped in a function called Cells

function MyForm.Cells(Row, Col): String;
begin
 result:= '';
  MyGrid.DataSource.DataSet.First;
  for i := 0 to Row - 1 do
    MyGrid.DataSource.DataSet.Next;
  result:= MyGrid.Columns[Col].Field.AsString;
end;


Shane
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:mokule
ID: 10846518

Yes,

I don't want to wrap it like this because when You want to get some cells from one row it'll be extremly ineffective ... :)

0
 

Author Comment

by:erik2000
ID: 10846562
Hmm I was thinking that DBGrid was desined for this purpose but I guess Im wrong, Im trying to:
1. Access any cells value by either an array (example [1,1] ) or by the row number/column name
2. I would be interested in reading all the values from a column or row

The method mokule used is something Im looking for but I think it will be really slow, Im working on a dbgrid that has 100k records each row has 12 fields
Is there another way to access the data other then this? Maybe directly from the ADOQuery that Im using to get the data from the sql

Thanks
Erik
0
 
LVL 17

Expert Comment

by:mokule
ID: 10846623

It depends what is Your aim but I suggest StoredProcedure, ADOCommand or ADOQuery
0
 

Author Comment

by:erik2000
ID: 10846658
http://64.233.167.104/search?q=cache:J9F7R5LZUD8J:www.delphinautes.org/scripts/delphin.dll/GET%3FACTION%3DIFAQ%26NUM%3D7+dbgrid+GetEditText&hl=en&ie=UTF-8

I also found this online, but I cant access GETEDITTEXT how would I go about doing that?

Thanks
Erik
0
 
LVL 17

Assisted Solution

by:geobul
geobul earned 280 total points
ID: 10857587
Hi,

Try this (directly from ADOQuery):

function GetADOQueryValue(q: TADOQuery; row, col: integer): string;
var
  offset: integer;
begin
  offset := row - q.RecNo;
  q.MoveBy(offset);
  result := q.Fields[col - 1].AsString;
end;

// usage:
procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(GetADOQueryValue(ADOQuery1, 3,2));
end;

Regards, Geo
0
 

Author Comment

by:erik2000
ID: 10861619
1. Isnt there a way to do it without moving the grid?
2. How would I go about writing to the grid?

Other then that the function works great.
Also I increased the points for this question
Thanks
Erik
0
 
LVL 17

Expert Comment

by:geobul
ID: 10866417
Hi,

>1. Isnt there a way to do it without moving the grid?

I would use another ADOQuery component and clone the original one:

...
ADOQuery1.Open; // DBGrid is accosiated to this query
ADOQuery2.Clone(ADOQuery1);
...

and then will get values from ADOQuery2:
ShowMessage(GetADOQueryValue(ADOQuery2,100,2));

>2. How would I go about writing to the grid?

What exactly do you mean?

Regards, Geo
0
 

Author Comment

by:erik2000
ID: 10870859
I want to write to the grid something like this SetADOQueryValue(ADOQuery2,1,1,"my value");

Thanks
Erik
0
 
LVL 17

Expert Comment

by:geobul
ID: 10876304
And you probably don't want to change the current record on DBGrid also? Assuming the current record is different from the one you're setting values to.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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 this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month12 days, 4 hours left to enroll

752 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