Link to home
Start Free TrialLog in
Avatar of majala
majala

asked on

playing with dbgrid...

If I click one cell of DBGrid, how can I get that cells value?? What DBGrid's event I have to use and what properties? Could you give me an example.

Thanks in advance!
Avatar of waty
waty
Flag of Belgium image

You could get directly the value of the needed field in the linked datarecordset.

Otherwise :
Dim Col1, Col2 as Column
Set Col1 = DBGrid1.Columns(0)
Set Col2 = DBGrid1.Columns(1)
Col1.Caption = "Column 1"
Col2.Caption = "Column 2"


Avatar of majala
majala

ASKER

User clicks dbgrid and I wanna get that lines first (Data1.Recordset(0)) cells value and put it to the statusbar.
I tryed to use recordset but recordset gave me always previous value. I mean that when I clicked the next line, recordset gave that previous lines first cells value.

I tryed this:
Private Sub DBGrid_Click()

  StatusBar1.Panels(3).Text = Data1.Recordset(0)

End Sub

Avatar of majala

ASKER

I ment that recordset gave that previous lines value that user had selected. If user has selected line 10 and after that selects line 21, recordset gives me that first cells value from line 10.
Did you tried StatusBar1.Panels(3).Text = DBGrid1.Columns(0)

Avatar of majala

ASKER

I tried now and I got an error:

  Run-time error '424':
  Object required


This should work:

MsgBox DBGrid1.Columns(DBGrid1.Col).Text
Avatar of majala

ASKER

Sorry, previous comment was wrong! I doublechecked my grids name!

Anyway it works in a same way as when I used recordset.
Should I use it with some other event than Click-event??
Avatar of majala

ASKER

scucketts answer worked same way as watys comment...
Avatar of majala

ASKER

when I click a cell first time that cells value turns blue but this (StatusBar1.Panels(3).Text = DBGrid.Columns(0)) don't give me a new value. But when I click second time that cell, I get that new value.
ASKER CERTIFIED SOLUTION
Avatar of sduckett
sduckett

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of majala

ASKER

It seem to work fine but it won't work in a first entry even if I tried just like you said?!
waty's answer is effectively the same as mine (fortunately TEXT appears to be the default property for COLUMNS) but only returns the data in the first column whichever column you click.
What I meant by 'on first entry' is that the event used is triggered if you tab to the grid, not just if you click a field.
There are other times that thwe event is triggered such as going to the next record using the data control so it is useful to have some means of disabling the event.
This sort of behaviour is easily seen if using message boxes as I have, but if your just setting a variable to be the value returned then it probably doesn't matter.