Board index » delphi » DBGrid problem, please HELP !

DBGrid problem, please HELP !

Hello,

We are currently developing software in Gupta's SQLWindows and Centura. After reading
a lot about Delphi, we decided to give it a go. We are now faced with the following
problem :

We have a form, with a DBGrid component, and some edit fields. We have succeeded in
linking a TTable component to the TDatasource to the TDBGrid component, in other
words, when we put the 'active' property of the DBGrid to TRUE, the data of the
database appears in the grid.

We have set the options of the grid in such a way that when the user clicks a row, it
selects the entire row. It is not possible to edit in the grid.

What we now want is this : when a user clicks a row in the grid, the contents of the
columns should be copied to the fields. We do this with the following code :

edit1.text := dbgrid1.fields[0].text;
edit2.text := dbgrid1.fields[1].text; and so on...

The problem is that the contents of the last selected row are NOT copied to the
fields, but the contents of the previous clicked row are copied. In other words, the
OnColEnter and OnColExit events happen BEFORE the black rectangle is drawn over the
new selected row. It is really driving me nuts !!

A friend told us to use DBEdit fields and link them to the table component, to have it
all run automatically. What we really want is to write our own Insert, Update and
Delete SQL-statements with the contents of the fields as parameters by using tquery
components.

We really want to have a serious go at Delphi, so if anyone has any suggestions or
solutions, they would be greatly appreciated.

Thanks in advance,

Spencer Pieters
Belgium

 

Re:DBGrid problem, please HELP !


Quote
> The problem is that the contents of the last selected row are NOT copied
to the
> fields, but the contents of the previous clicked row are copied. In other
words, the
> OnColEnter and OnColExit events happen BEFORE the black rectangle is

drawn

Use the OnDataChange (i think that is it) event of the data source
component.

--
Mark Pritchard

Re:DBGrid problem, please HELP !


Quote
Spencer Pieters <Jerry.Piet...@Kerknet.be> writes:
> Hello,

> We are currently developing software in Gupta's SQLWindows and Centura. After reading
> a lot about Delphi, we decided to give it a go. We are now faced with the following
> problem :

> We have a form, with a DBGrid component, and some edit fields. We have succeeded in
> linking a TTable component to the TDatasource to the TDBGrid component, in other
> words, when we put the 'active' property of the DBGrid to TRUE, the data of the
> database appears in the grid.

> We have set the options of the grid in such a way that when the user clicks a row, it
> selects the entire row. It is not possible to edit in the grid.

> What we now want is this : when a user clicks a row in the grid, the contents of the
> columns should be copied to the fields. We do this with the following code :

> edit1.text := dbgrid1.fields[0].text;
> edit2.text := dbgrid1.fields[1].text; and so on...

> The problem is that the contents of the last selected row are NOT copied to the
> fields, but the contents of the previous clicked row are copied. In other words, the
> OnColEnter and OnColExit events happen BEFORE the black rectangle is drawn over the
> new selected row. It is really driving me nuts !!

> A friend told us to use DBEdit fields and link them to the table component, to have it
> all run automatically. What we really want is to write our own Insert, Update and
> Delete SQL-statements with the contents of the fields as parameters by using tquery
> components.

> We really want to have a serious go at Delphi, so if anyone has any suggestions or
> solutions, they would be greatly appreciated.

> Thanks in advance,

> Spencer Pieters
> Belgium

Spencer:

Sorry if I mis-understand your request, but it reads like you are using the
DBGrid as a display list, and data entry is done in non-database aware
Edit fields.

If this is the case, then instead of using the dbGrid to get change events,
try the OnDataChange event of the TDataSource connected to your table:

procedure TForm1.GridSourceDataChange(Sender: TObject; Field: TField);
begin
  Edit1.Text := GridTable.Fields[0].AsString;
  Edit2.Text := GridTable.Fields[1].AsString;
  {etc}
end;

When the grid selection row moves to a new record, your edit fields will
be updated with the current data.

A problem with this occurs when the user resizes columns in the grid, or
drags a column into a different order: OnDataChange events are generated
(lots of them in the case of column re-ordering).

The simple fix is to set the dgColumnResize option of the dbGrid to false,
then no resizes or order changes can happen. If you want to allow grid
column changes then perhaps someone can come up with a simple, effective,
nay elegant means of testing for table record changes. What occurs to me
is comparing key fields but that seems crude.

My other misgiving is all the newsgroup postings I see about Delphi
database controls getting out of sync due to caching. Whether this will
be a problem for you remains to be seen.

With sincere apologies to all if this post is a tangent to the actual problem,

I remain,

                     Chris Snell
                     cesn...@wownet.kosone.com

Re:DBGrid problem, please HELP !


Quote
Spencer Pieters wrote:

> What we now want is this : when a user clicks a row in the grid, the contents of the
> columns should be copied to the fields. We do this with the following code :

> edit1.text := dbgrid1.fields[0].text;
> edit2.text := dbgrid1.fields[1].text; and so on...

> The problem is that the contents of the last selected row are NOT copied to the
> fields, but the contents of the previous clicked row are copied. In other words, the
> OnColEnter and OnColExit events happen BEFORE the black rectangle is drawn over the
> new selected row. It is really driving me nuts !!

Hello Spencer,

try to use the OnDataChange event of the Datasource that links your
table with your grid.
In this event use the property text of the fields you need. It could
look like this:

procedure TForm1.DataSource1DataChange(Sender: TObject; Field: TField);
begin
     Edit1.Text := Table2Einrichtung.Text;
     {more...}
end;

Before you can do this, don't forget to add the fields to your table
(doubleclick on the
Table icon, choose add). Feel free to mail me if you have any problems
with this.

HTH

Thomas

---------------------------------------------------------
thomas scheffczyk
email: scheffc...@verwaltung.uni-mainz.de
home: http://www.verwaltung.uni-mainz.de/Dez1/edv/folks/scheff/

Re:DBGrid problem, please HELP !


Quote
Chris Snell <cesn...@wownet.kosone.com> wrote:
>> OnColEnter and OnColExit events happen BEFORE the black rectangle is drawn over the
>> new selected row. It is really driving me nuts !!

>> A friend told us to use DBEdit fields and link them to the table component, to have it
>> all run automatically. What we really want is to write our own Insert, Update and
>> Delete SQL-statements with the contents of the fields as parameters by using tquery
>> components.

Could you not use the DBEdit fields as suggested and then use the
BeforePost event to handle your custom Insert, Update and Delete SQL
statements?

Rob
--------------------------------
email: RobDa...@wheel.demon.co
       Rob.Da...@unn.ac.uk
Web:   http://www.unn.ac.uk/~isu8

Other Threads