Jump to content


Photo

Reading columns from selected row?


  • Please log in to reply
13 replies to this topic

#1 littlebigfred

littlebigfred
  • Members
  • 176 posts

Posted 26 June 2009 - 08:30 PM

Hello

I'd to like to read some of the columns of the currently-selected row in a dataset/NextDBGrid.

I know I can hit the dataset directly like this:

CODE
ShowMessage(ASQLite3Query1.FieldByName('label').AsString));


... but I was wondering if there is a corresponding method in NextDBGrid, eg. something like this to read the contents of column "label':

CODE
ShowMessage(NextDBGrid1.SelectedRow['label'])


Thank you.

#2 Boki (Berg)

Boki (Berg)

    Boki (Berg)

  • Forum Admin
  • PipPipPipPipPip
  • 8,191 posts
  • Gender:Male

Posted 27 June 2009 - 11:31 AM

Hello Fred,

You may also read selected row with using Field property of Column:

CODE
n := NextDBGrid1.Columns[4].Field.AsInteger;


This will return value of field associated with column of currently selected (active) record.

I hope that this helps,

Best regards
boki@bergsoft.net | LinkedIn Profile
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.

#3 littlebigfred

littlebigfred
  • Members
  • 176 posts

Posted 27 June 2009 - 04:16 PM

QUOTE (Boki (Berg) @ Jun 27 2009, 11:31 AM) <{POST_SNAPBACK}>
You may also read selected row with using Field property of Column:


Thx Boky. Is it possible to refer to the column by its name instead of hard-coding the column number?


#4 Boki (Berg)

Boki (Berg)

    Boki (Berg)

  • Forum Admin
  • PipPipPipPipPip
  • 8,191 posts
  • Gender:Male

Posted 27 June 2009 - 04:30 PM

Hello Fred,

You may use ColumnByName property in next way:

CODE
NextDBGrid1.Columns.ColumnByName['name'].Field


I hope that this helps

Best regards
boki@bergsoft.net | LinkedIn Profile
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.

#5 littlebigfred

littlebigfred
  • Members
  • 176 posts

Posted 29 June 2009 - 01:36 PM

QUOTE (Boki (Berg) @ Jun 27 2009, 04:30 PM) <{POST_SNAPBACK}>
You may use ColumnByName property in next way:


Great, thanks.


#6 littlebigfred

littlebigfred
  • Members
  • 176 posts

Posted 30 June 2009 - 05:42 PM

Er... I just gave it a try, and I get an error, even after upgrading to 4.6.9.1.

1. Added a pop-up menu to the form with a single item "Lire" ("Read")
2. Set NextDBGrid.PopUpMenu to point to this pop-up control
3. Added this code:

CODE
//Added NxColumns, NxColumnClasses, NxDBColumns manually
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, NxScrollControl, NxCustomGridControl, NxCustomGrid,
  NxDBGrid, NxColumns, NxColumnClasses, NxDBColumns, DB, ASGSQLite3, Grids, DBGrids, Menus;

[...]

procedure TForm1.Lire1Click(Sender: TObject);
begin
  //E2003 Undeclared identifier: 'ColumnByName'
  ShowMessage(NextDBGrid1.Columns.ColumnByName['name'].Field);

  //E2010 Incompatible types: 'string' and 'TNxCustomColumn'
  //Note: .AsString isn't available
  ShowMessage(NextDBGrid1.ColumnByName['label']);
end;


Am I missing something in the Uses cause maybe?

Thank you.

#7 Boki (Berg)

Boki (Berg)

    Boki (Berg)

  • Forum Admin
  • PipPipPipPipPip
  • 8,191 posts
  • Gender:Male

Posted 30 June 2009 - 05:46 PM

Hello Fred,

Try to modify your code to:

CODE
ShowMessage((NextDBGrid1.ColumnByName['name'] as TNxDBCustomColumn).Field.AsString);


I hope that this helps.

Best regards
boki@bergsoft.net | LinkedIn Profile
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.

#8 littlebigfred

littlebigfred
  • Members
  • 176 posts

Posted 30 June 2009 - 05:56 PM

Thanks but I'm getting an exception:

CODE
procedure TForm1.Lire1Click(Sender: TObject);
begin
  //E2003 Undeclared identifier: 'ColumnByName'
  //ShowMessage(NextDBGrid1.Columns.ColumnByName['label'].Field);

  //E2010 Incompatible types: 'string' and 'TNxCustomColumn'
  //Note: .AsString isn't available
  //ShowMessage(NextDBGrid1.ColumnByName['label']);

  //E2010 Incompatible types: 'string' and 'TNxCustomColumn'
  //ShowMessage(NextDBGrid1.Columns.Column['label']);

  //Project1.exe raised exception class EAccessViolation with message 'Access violation at address 004F6B39 in module 'Project1.exe'. Read of address 000000C8'.
  ShowMessage((NextDBGrid1.ColumnByName['label'] as TNxDBCustomColumn).Field.AsString);
end;


Maybe this property is only available in the NextGrid object?

#9 Boki (Berg)

Boki (Berg)

    Boki (Berg)

  • Forum Admin
  • PipPipPipPipPip
  • 8,191 posts
  • Gender:Male

Posted 30 June 2009 - 06:14 PM

Hello Fred,

Last code usage is ok, but you need to assign name of column to be label. Also, Field must be assigned (DataSet is active).

I hope that this helps,

Best regards
boki@bergsoft.net | LinkedIn Profile
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.

#10 littlebigfred

littlebigfred
  • Members
  • 176 posts

Posted 30 June 2009 - 07:59 PM

QUOTE (Boki (Berg) @ Jun 30 2009, 06:14 PM) <{POST_SNAPBACK}>
Last code usage is ok, but you need to assign name of column to be label. Also, Field must be assigned (DataSet is active).


Ah, since the dataset was active, I thought the NextDBGrid would receive the list of columns from the dataset.

I didn't find how to do this in the NextDBGrid QuickGuide. Do I have to run a loop after opening the Query?


#11 littlebigfred

littlebigfred
  • Members
  • 176 posts

Posted 30 June 2009 - 08:06 PM

I tried this, but I get the same error :-/

CODE
  DataSource1.DataSet := ASQLite3Query1;
  NextDBGrid1.DataSource := DataSource1;

  //Added
  NextDBGrid1.DataAwareOptions := [doAddColumns];


#12 Boki (Berg)

Boki (Berg)

    Boki (Berg)

  • Forum Admin
  • PipPipPipPipPip
  • 8,191 posts
  • Gender:Male

Posted 30 June 2009 - 09:14 PM

Hello Fred,

To use ColumnByName you must first set Name property of column.

Maybe this bother you.

Best regards
boki@bergsoft.net | LinkedIn Profile
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.

#13 littlebigfred

littlebigfred
  • Members
  • 176 posts

Posted 01 July 2009 - 05:00 PM

QUOTE (Boki (Berg) @ Jun 30 2009, 09:14 PM) <{POST_SNAPBACK}>
To use ColumnByName you must first set Name property of column.


Got it smile.gif

For those interested, here's how to read column names from a dataset, and use this to set a NextDBGrid's Name property for each column so they can be refered to with names instead of column indexes:

CODE
  With NextDBGrid1 do begin
    DataSource := DataSource1;
    for i := 0 to Columns.Count - 1 do begin
      Columns[i].Name := Columns[i].FieldName;
    end;
  end;

  ShowMessage((NextDBGrid1.ColumnByName['label'] as TNxDBCustomColumn).Field.AsString);


Thank you.

#14 Boki (Berg)

Boki (Berg)

    Boki (Berg)

  • Forum Admin
  • PipPipPipPipPip
  • 8,191 posts
  • Gender:Male

Posted 23 November 2009 - 02:36 AM

Hello Amatary,

Since currently selected row in grid is also a currently selected record in DataSet, all you need is to read Field property of Column (Columns[5].Field) or use Fields property of TDataSet.

I hope that it helps.

Best regards
boki@bergsoft.net | LinkedIn Profile
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users