Delphi TQuery result set - how to avoid the caveats of having RequestLive set to true - delphi

I use Delphi 10.2 with ComponentAce's Absolute Database framework. So my question here can pertain to TAbsQuery or, I presume, the conventional Delphi TQuery class.
My database has a date field named lastViewed. I have the RequestLive set to true so that any changes made to various fields in the current record are persisted to the table. Additionally, in my BeforeScroll event (i.e., prior to advancing to another record) the lastViewed field is updated to the current date. It works just fine. However, I have a menu option that displays a list of all records sorted by the last viewed date. In other words,
SELECT * FROM myTable ORDER BY lastViewed
It looks just fine when the results are initially shown in the DBGrid (TAbsGrid, in my case). But as I scroll down the list, the lastViewed field is updated and that record is pushed to the bottom of the grid. I would like the original order of the list to be preserved as I scroll up or down the list.
Turning LiveRequest to false will obviously trigger a database error when updating the DB fields in my BeforeScroll event.
The same issue would occur if, say, I want to show results for all records having the myFavoriteColor field with a RED value. Changing the myFavoriteColor value to GREEN, that row would disappear from my query results list.
Is there a way to keep the current query results in tact, while still being able to update the underlying table with new data?
Thank you for reading this.

Related

Vaadin grid user column re-ordering and saving per user

How do people tend to let users re-order the grid columns and save that ordering for later?
The only way I can think of to do it, at least in Vaadin 7, is:
Listen for column re-ordering via addColumnReorderListener(…)
When re-order triggered, if user initiated, get columns from getColumns() and save to DB with any identifying information
When pull Grid back up, read grid ordering from DB and apply the same order with setColumnOrder(columns)
So is there a better way to do this? I just checked the Directory, could not find anything obvious to make this easier. Just looking for how others have addressed this user requirement. If Vaadin 14 already supports such actions a little easier, that would be good to know as well, as it might give me some ideas on how to get that ability short term before I can upgrade to Vaadin 14.
For a more customizable grid you can (in addition to what you've already done) add a button that opens a dialog that lists all possible columnnames, together with a checkbox.
Unchecking the checkbox removes the column, checking the checkbox adds the column.
Even more comfortable is when the dialog lists all available columns in a Grid with draggable rows and editable checkboxes, so that the user can show, hide and sort all columns in one place. After that you have to reorder all columns by calling grid.setColumnOrder.
Just so people know how I solved this issue, based on the comments:
When load data into Grid, first check database for columns of this Grid/user combination. If find such a column order, call setColumnOrder(userColumns).
Added 2 buttons to top, one to save column order, one to reset it.
"Save" button only enabled after moving at least one column.
"Reset" button only enabled if at least one column was moved. One column was moved either because of the DB, or because user JUST moved a column.
On save, save to DB. On reset, clear from DB, and reset Grid to original column order.
We chose not to save the column order each time they changed the order, directly in the addColumnReorderListener, because we realized sometimes users might move columns around temporarily, and one really want to save that column order for the future. That said, the saving inside the addColumnReorderListener worked well.
We don't currently need to save the column sizes, as suggested by #Simon Martinelli, but we are keeping it as an idea for the future. I fully expect it would work.

How to highlight changed cells when updating a DBgrid?

Let's say I am showing stock prices, or sports scores, or movie attendance or something.
Periodically, I will refresh the grid by Close() and then Open() of a query linked to its associated datasource.
I know how to owner draw a cell with OnDrawCell() - what I can't figure out is how to know if the new value is the same as or different from the previous value for a given cell.
I suppose there are two use cases here, one where the number of rows is fixed and they remain in the same row order and one where rows can change (insert/delete or reorder).
For the former, I can take a snapshopshot before updating and compare after the update, but that might be a lot of data. I am not sure if I want to restrict the operation to the currently visible rows. I think that a user might want to scroll down and still be notified of any which have changed during the last update.
For the latter, I am stumped, unless, of course, each row has a unique key.
How can I do this (efficiently)? A solution for TDbGrid would help everyone, a solution with TMS Software's TAdvDbGrid would be fine by me (as would a (preferably free) 3rd party component).
TDBGrid reads the data currently contained in its assigned dataset. It has no capacity to remember prior values, perform calculations, or anything else. If you want to track changes, you have to do it yourself. You can do it by multiple means (a prior value column, a history table, or whatever), but it can't be done by the grid itself. TDBGrid is for presenting data, not analyzing or storing it.
One suggestion would be to track it in the dataset using the BeforePost event, where you can store the _oldvalue of a your into a LastValue column, and then use that to see if the value has changed in your TDBGrid.OnDrawColumnCell event and alter the drawing/coloring as needed. Something like if LastValue <> CurrValue then... should work.

How to display all data from 2 related tables in a dbgrid with sql

I'm new to delphi and have created an SQL query which displays the results of a search entered by the user through an edit box (showing data from 2 related tables) in a dbGrid.
The dbgrid displays no data until a search has been made and then the results of that search are displayed, however i was wondering whether it was possible to show all the related data in the dbgrid as if it were a table and the search then simply selects from it?
thanks
You could display all the data in the table. Then write an onExit event handler for your edit box which would look something like this (assuming that the string which you are entering in the edit box is supposed to match the value in the 'name' field of the query)
query.locate ('name', edit1.text, [loPartialValue]);
This will cause the dbgrid's cursor to jump to the first line which matches the string in the edit box.
Instead of using the OnExit event, you could also use the OnChange event of the edit box, but this would mean a search after every key stroke.
Your question was a bit vague so it's difficult to give a specific answer.

Delphi 2007 Unable to modify the data in beforescroll event using source code, Error: Dataset not in Edit or Insert Mode

A bit of History:
I have a DBlistbox containing textt descriptions and I store just the codes of this in a mysql table which is displayed in a dbgrid. User can select multiple options and I like to process these and store as comma separated values in one of the columns (this column is invisible in the dbgrid).
Right now I can populate the listbox when the user scroll through the dbgrid using event Afterscroll and Formcreate. But when I try to process the list and update the myquery behind the dbrgid I get an error saying 'Dataset not in Edit or Insert mode' - I do this in beforescroll
Please help!!
I have tried to set the dataset to edit mode before changing then as soon the data is posted dbgrid seems to have funny characters
If I got you right, the DBListBox is not connected to the dataset lonked in the grid? This would explain why the grid's dataset is not set to edit mode when you change the data in the DBListBox.
Anyway, whenever you change data that has to go into the mysql table, you should switch this dataset into edit mode. Then you can place your coding of the comma separated values into the OnBeforePost event.
When the dataset scrolls without any change in the data of the listbox, there is no need to store any data. This is achieved with the above approach as without edit mode no BeforePost event will fire in this case.

Benefit of using DBComboBox over CombBox?

So I'm messing around with a new project in Delphi 2009 and the default components that can be dropped onto a form for accessing data consist of a SQLConnection, DataSource and SQLQuery. If I add a simple select to the query component, say:
select name from customers
and then drop a DBComboBox on the form and link it up with the DataSource I get a single record in the combo box. After using Google for half and hour to figure out what I was doing wrong it looks like you have to manually add some code to your project which loops through the dataset and adds all the records to the drop down box. Something like:
while not SQLQuery.eof do
begin
DBComboBox.items.add(SQLQuery.fieldbyname('name').asstring);
SQLQuery.next;
end;
And that actually sort of works, but then you get a list in the drop down which you can't actually select anything from. Regardless of the result though I'm wondering why would you even use a DBComboBox if you have to manually add the result of your query to it? Seems to me that if it doesn't automatically populate the db combo box with the result of the query then we might as well be using a non-data-aware component like tcombobox.
I guess what I'm asking is why does it work this way? Isn't the purpose of data aware drag-and-drop controls to minimize the amount of actual written code and speed development? Is there a method that I'm missing that is supposed to make this easier?
A TDBComboBox doesn't get its list of values from the database; it gets its current value from the database. Link it to a field in your dataset, and when you change the active record, the combo box's current value will change. Change the combo box's current value, and the corresponding field's value will change.
If you want to get the list of values from the database as well, then use a TDBLookupComboBox.
This is all covered in the help:
Using TDBListBox and TDBComboBox
Displaying and Editing Data in Lookup List and Combo Boxes
Defining a Lookup List Column
I think you want the TDBLookupComboBox because that allows you to lookup from a list of items where the list comes from a dataset.
In the TDBComboBox, the list is just a TStrings manually filled with data.
--jeroen
DbCombox is a dbaware version of the standard combobox component.

Resources