Link to home
Avatar of IT79637
IT79637Flag for United States of America

asked on

How do I get data out of the DBGrid1 control?

Urgent please...
The Raize DBGrid1 control...
How can I  programmatically get data out of a cell or row of cells into variabless in my program on a double click event or a push button event?


There must be some sort of cell property that can be addressed like a two dim array???

Thanks much.
Avatar of Johnjces
Johnjces
Flag of United States of America image

I am pretty certain that Raize dbGrid is pretty much an enhanced dbGrid.

I use the OnMouseUp event of the grid and set my variables there like:

procedure DBGrid1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
 DacalPosNum.Text := ADOQuery1.FieldByName('Location').AsString;
end;

The variable is DacalPosNum and you can set it by using the table's or the query's field name. My example uses a query and the filed name is Location'.

So, each time you click the grid in a row, the data for that row appears in your variable.

Hope this got you going in the right direction.

There are other ways.

John
Avatar of IT79637

ASKER

I'm not sure if I gave enough info.  I''m using MicroOLAP DAC for MySQL as my db access components.   The application is doing a vendor lookup similar to an AJAX auto-complete--type-ahead function.  The Delphi program is an ole server called from an html page using vbscript.  

I'm not going to explain the inner workings.  I would be typing and revising for hours and hours to make the description clear and concise for you to read.  

It is an odd-ball mixture of web and ole server technology as follows:
HTML --> VBS --> OLE Server vai a Method call with Input and Output parameters --> back to VBS -->  back to HTML page.


See attached image of web page and ole server with dbgrid.    Note:
1.  In lower left of web page is the vendor number of the first row of the grid also note on html page (sent to servier via vbs).
2.  I have typed in part of the vendor name on the html page (received from server via vbs).

At this time, I have filled my grid with data.  The DAC query and grid are left in a "suspended" state.  The Delphi based dbgrid is the active window on my desktop.

Now, using theRaize dbgrid control, I need to get a row of data out of the dbgrid.  That is my problem/question.


Thanks for putting up with a question mixed in with a lot of technology.






VendorLookupResults.jpg
It is now very well understood!

I wish I could help you.

John
Avatar of IT79637

ASKER

The second image shows the DBGrid where I have double clicked on a row and want to extract that information to send back to the tml page.  The vendor information is all fake.  It is made up for testing purposes.
SelectIRowDoubleClick.jpg
Avatar of SteveBay
I don't know if you can make this work in you particular situation but this is a simple way of looking back at the data from a DBGrid.
procedure TForm1.Button1Click(Sender: TObject);
var Col : integer;
begin
     RzDBGrid1.DataSource.DataSet.First;
     While Not RzDBGrid1.DataSource.DataSet.Eof do
          begin
          for col := 0 to RzDBGrid1.Columns.Count -1 do
               ListBox1.Items.Add(RzDBGrid1.DataSource.DataSet.Fields[Col].AsString);
          RzDBGrid1.DataSource.DataSet.Next;
          end;
end;

Open in new window

Avatar of IT79637

ASKER

Good, I can loop through the dbgrid, I need to select the row that I have highlighted with the button click event. How do I do that?
Thanks.
Avatar of IT79637

ASKER

I found this on about,delphi.com:

f you want to get the line number of a selected row in a DBGrid component, here's what to do... Note: just substitute the Table1 component for whatever dataset you are using to provide data to your DBGrid.

~~~~~~~~~~~~~~~~~~~~~~~~~
ShowMessage(IntToStr(Table1.RecNo)) ;
~~~~~~~~~~~~~~~~~~~~~~~~~

fyi
Avatar of IT79637

ASKER

Is there a test I can do something like this:

if DBGrid1.RowHighlighted or DBGrid1.RowSelected  then
  BGrid1.Get RowNumber;  
  Extract that row as shown above, but for one row only.
end;

Or can I loop through the grid and determine which row is highlighted?   If I have that index value then I can extract the row values.

Thanks.

procedure TForm1.RzDBGrid1MouseUp(Sender: TObject; Button: TMouseButton;  Shift: TShiftState; X, Y: Integer);
begin
     ListBox1.Items.Add(RzDBGrid1.DataSource.DataSet.FieldByName(RzDBGrid1.SelectedField.FieldName).AsString);
end;

Open in new window

Avatar of IT79637

ASKER


I'm not trring to get the data into another control, such as a ListBox1. I want the data in local string variables.  Since I can get the first column of the row selected, how do I get the remainder of the columns.  Is there a way I can index into them using [0],[1],[2],[3], etc?

When I double click on a DBGrid1 row with this statement:

sName:=(DBGrid1.DataSource.DataSet.FieldByName(DBGrid1.SelectedField.FieldName).AsString);

I get the vendor number is sName.

The attached image shows I am getting the first data item.
SelectVendor.jpg
ASKER CERTIFIED SOLUTION
Avatar of SteveBay
SteveBay
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of IT79637

ASKER

I have the problem solved now.  Since I can get the vendor number from the double click event, I can then look through the grid and strComp with the value of the vendor number in the table.

 i:= StrComp(PChar((DBGrid1.DataSource.DataSet.Fields[0].AsString)), PChar(sVendorNo)) ;

If i:= 0 then
   get contents of row and exit
else
   continue looping;
end;

It is not the best solution or very elegant, but it works.

If you have any suggestions, please post them.

Thanks much!!!

procedure TfrmImageDisplay.DBGrid1CellClick(Column: TColumn);
begin
  DBGrid1.Options := DBGrid1.Options  +  [dgRowSelect]     ;
end;
 
procedure TfrmImageDisplay.DBGrid1DblClick(Sender: TObject);
  var sVendorNo:string;
  var Col,i:integer;
begin
    sVendorNo:=Trim(DBGrid1.DataSource.DataSet.FieldByName(DBGrid1.SelectedField.FieldName).AsString);
    DBGrid1.DataSource.DataSet.First;
    While Not DBGrid1.DataSource.DataSet.Eof do
    begin
      for col := 0 to DBGrid1.Columns.Count -1 do begin
        i:= StrComp(PChar((DBGrid1.DataSource.DataSet.Fields[0].AsString)), PChar(sVendorNo)) ;
        //ShowMessage(IntToStr(i) + '|'+ (DBGrid1.DataSource.DataSet.Fields[0].AsString)+'|'+ sVendorNo);
        if  i = 0 then begin
          sVendor_No := DBGrid1.DataSource.DataSet.Fields[0].AsString;
          sName      := DBGrid1.DataSource.DataSet.Fields[1].AsString;
          sAddress   := DBGrid1.DataSource.DataSet.Fields[2].AsString;
          sCity      := DBGrid1.DataSource.DataSet.Fields[3].AsString;
          sState     := DBGrid1.DataSource.DataSet.Fields[4].AsString;
          sPostal    := DBGrid1.DataSource.DataSet.Fields[5].AsString;
          sAllVendorFields := sVendor_No + sBar + sName + sBar + sAddress + sBar +
              sCity + sBar + sState+ sBar + sPostal;
          ShowMessage(sAllVendorFields);
          i := 200;
          Exit;
        end;
 
        DBGrid1.DataSource.DataSet.Next;
 
      end; 
    end;
end;

Open in new window

It's not clear to me why you cannot do this:

    DBGrid1.DataSource.DataSet.First;
    While Not DBGrid1.DataSource.DataSet.Eof do
          begin
          sVendor_No := DBGrid1.DataSource.DataSet.FieldByName('Vendor_No').AsString;
          sName      := DBGrid1.DataSource.DataSet.FieldByName('Name').AsString;
          sAddress   := DBGrid1.DataSource.DataSet.FieldByName('Address').AsString;
          sCity      := DBGrid1.DataSource.DataSet.FieldByName('City').AsString;
          sState     := DBGrid1.DataSource.DataSet.FieldByName('State').AsString;
          sPostal    := DBGrid1.DataSource.DataSet.FieldByName('Postal').AsString;
          sAllVendorFields := sVendor_No + sBar + sName + sBar + sAddress + sBar +
               sCity + sBar + sState+ sBar + sPostal;
          ShowMessage(sAllVendorFields);
          DBGrid1.DataSource.DataSet.Next;
          end;

Open in new window