I want to modify the content of a particular cell in dbgrid control when the database is loaded. For example, lets say I don't want any field of database to be displayed in dbgrid if it is equal to "forbidden". Is there any way that I can do that?
Going to your original question:
Use the OnGetText event of the field to provide a different value from what is stored on the database for presentation purposes.
The DisplayText boolean parameter will be True if the value is required to be presented to the user and will be False if the value is required for other purposes.
procedure TForm1.SQLQuery1Field1GetText(Sender: TField;
var Text: string; DisplayText: Boolean);
begin
if (Sender.AsString = 'forbidden') and (DisplayText)
and (PrivilegeLevel(CurrentUser) < 10) then
Text := '********'
else
Text := Sender.AsString;
end;
you can use the DataSetNotifyEvent Afteropen
DBGrid.Datasource.Dataset.Afteropen :=
and you can hide fields with:
if Condition then
DBGrid.columns[x].visible := false
alternative you can check the condition on the OnDrawColumnCell event in order to overrite / delete some content in a specific cell
Using the DataSet events to synchronize UI is not a good practice. You can rely on DataSource events to do that, separating UI Logic from business logic.
As the state of the DataSet will change from dsInactive to dsBrowse, you can rely on the DataSource OnState change to make anything UI-related upon the data is loaded from database.
You can rely on a Auxiliar field to track previous state to avoid the code executing more than needed.
for example (untested code)
procedure TForm1.DataSource1StateChange(Sender: TObject);
begin
if (DataSource1.State = dsBrowse) and (not FUIStateInSync) then
begin
//dataset is open, change UI accordingly
DBGrid1.Columns[0].Visible := SomeCondition();
//this will prevent the code to be executed again
//as state comes to dsBrowse after posting changes, etc.
FUIStateInSync := True;
end
else if (DataSource1.State = dsInactive) then
FUIStateInSync := False; //to let it happen again when opened.
end;
I publish this even when you have an accepted answer, because O.D. suggestion is just what you shall avoid.
Hookup OnAfterOpen event on dataset.
Get the hidden fields and set its Visible property to False and your dbgrid will not display them
Cheers
I would modify the query that provides the data to the grid so as not to include rows (tuples) which have the 'forbidden' string. This seems much easier than trying hard not to display data after it has already been retrieved from the database.
I think that the best way would be not to SELECT the fields WHERE SOME_VALUE="forbidden" FROM the DATABASE_TABLE
Related
I was always bugged by the fact that TMemo (and other similar controls) only have the OnChange event. I would like to know when the USER changed the text, not when the text was changed programmatically.
I know two methods to discriminate between the user changed text and programmatically changed text:
Put OnChange:= NIL before you change the text programmatically. Then restore the OnChange. This is error prone as you need to remember to do it every time you change text from the code (and to which memos/edits needs this special treatment to be applied). Now we know that every time the OnChange is called, the control was edited by user.
Capture the OnKeyPress, MouseDown, etc events. Decide if the text was actually changed and manually call the code that needs to be called when user edited the ext. This could add a big amount of procedures to an already large file.
There is a more elegant way to do it?
You can write a helper procedure to do your option 1, and use it in your framework whenever you want to ensure no OnChange event is triggered when you set the text. e.g.:
type
TCustomEditAccess = class(TCustomEdit);
procedure SetEditTextNoEvent(Edit: TCustomEdit; const AText: string);
var
OldOnChange: TNotifyEvent;
begin
with TCustomEditAccess(Edit) do
begin
OldOnChange := OnChange;
try
OnChange := nil;
Text := AText;
finally
OnChange := OldOnChange;
end;
end;
end;
TMemo has also the Lines property which also triggers OnChange, so you can make another similar procedure that accepts Lines: TStrings argument.
How about using the Modified property?
procedure TForm1.MyEditChange(Sender: TObject);
begin
if MyEdit.Modified then
begin
// The user changed the text since it was last reset (i.e. set programmatically)
// If you want/need to indicate you've "taken care" of the
// current modification, you can reset Modified to false manually here.
// Otherwise it will be reset the next time you assign something to the
// Text property programmatically.
MyEdit.Modified := false;
end;
end;
I've been trying to make a creative thing to avoid the dbgrids, and i've found the Tlistview (using the one from alphaskins, tslistview), and seems to be a nice way!
The problem is, I don't want to code the event onclick on every tlistview to position a record/dataset according to the item I selected on the tlistview .. and I'm doing it with the tlistview item's caption.. and there could be records with the same names
Here is one of the codes I want to avoid:
with q_find_process do
begin
close;
sql.Clear;
sql.Add('Select * from t_process where process_name like '+quotedstr(streeview1.Selected.Text)+');
open;
end;
And no, I don't want to put the ID of the Record on the item caption..!
Any ideas?
Does anyone know other way of showing a lot of records without being only text text and more text? I don't know all components on the tool palette, maybe someone could suggest me other one..
I have sometimes used listviews which have been loaded from database tables - only for small amounts of data. I don't understand what you mean by I don't want to code the event onclick on every tlistview to position a record/dataset according to the item I selected on the tlistview, so I'm going to show you how I solved this problem.
Basically, I create a sub-item which holds the primary key of each record. All the user interface code uses two list views, and at the end, the database is updated. There is no interaction with the database between loading and storing (which might be where I avoid your 'onclick' problem). The widths of each fields are set in the Object Inspector; the final subitem's width is 0 (ie not displayed).
Loading the list view:
srclist.items.clear;
with qSrcList do
begin
close;
params[0].asdate:= dt; // use date of deposit
open;
while not eof do
begin
ListItem:= srclist.Items.Add;
ListItem.Caption:= fieldbyname ('kabnum').asstring;
ListItem.SubItems.Add (fieldbyname ('price').asstring);
ListItem.SubItems.Add (fieldbyname ('duedate').asstring);
ListItem.SubItems.Add (fieldbyname ('docket').asstring);
ListItem.SubItems.Add (fieldbyname ('id').asstring);
next
end;
close
end;
Saving data:
with dstlist do
for index:= 1 to items.count do
with qInsert do
begin
dstlist.itemindex:= index - 1;
lvitem:= dstlist.selected;
parambyname ('p1').asinteger:= deposit;
parambyname ('p2').asinteger:= strtoint (lvitem.SubItems[3]);
parambyname ('p3').asfloat:= strtofloat (lvitem.SubItems[0]);
execsql;
end;
I hope that this helps you. The context of this code (not that it matters too much) is in a financial application where the user wishes to populate a bank deposit form with cheques. SrcList holds the cheques which have yet to be deposited (there will only be a few per given date) and DstList holds the cheques which have already been connected to a given deposit form.
I have a DataSet (TZQuery), which has several boolean fields, that have TDBCheckBoxes assigned to them.
These CheckBoxes have "OnClick" events assigned to them and they are triggered whenever I change field values (which are assigned to checkboxes).
The problem is that I do not need these events triggerred, during many operations i do with the dataset.
I've tried calling DataSet.DisableControls, but then events are called right after i call DataSet.EnableControls.
So my question is - is there a way to disable triggering Data-aware controls events.
Edit (bigger picture):
If an exception happens while let's say saving data, i have to load the default values (or the values i've had before saving it). Now while loading that data, all these events (TDBCheckBoxes and other data-aware controls) are triggered, which do all sorts of operations which create lag and sometimes even unwanted changes of data, i'm looking for an universal solution of disabling them all for a short period of time.
Building on Guillem's post:
Turn off everything:
Traverse each component on the form with the for-loop, shown below, changing the properties to the desired value.
If you want to later revert back to the original property values, then you must save the original value (as OldEvent is used below.)
Edit: The code below shows the key concept being discussed. If components are being added or deleted at run-time, or if you'd like to use the absolutely least amount of memory, then use a dynamic array, and as Pieter suggests, store pointers to the components rather than indexing to them.
const
MAX_COMPONENTS_ON_PAGE = 100; // arbitrarily larger than what you'd expect. (Use a dynamic array if this worries you.
var
OldEvent: Array[0.. MAX_COMPONENTS_ON_PAGE - 1] of TNotifyEvent; // save original values here
i: Integer;
begin
for i := 0 to ComponentCount - 1 do
begin
if (Components[i] is TCheckBox) then
begin
OldEvent[i] := TCheckBox(Components[i]).OnClick; // remember old state
TCheckBox(Components[i]).OnClick := nil;
end
else if (Components[i] is TEdit) then
begin
OldEvent[i] := TEdit(Components[i]).OnClick; // remember old state
TEdit(Components[i]).OnClick := nil;
end;
end;
Revert to former values
for i := 0 to ComponentCount - 1 do
begin
if (Components[i] is TCheckBox) then
TCheckBox(Components[i]).OnClick := OldEvent[i]
else if (Components[i] is TEdit) then
TEdit(Components[i]).OnClick := OldEvent[i];
end;
There may be a way to fold all of the if-statements into one generic test that answers "Does this component have an OnClickEvent" -- but I don't know what it is.
Hopefully someone will constructively criticize my answer (rather than just down voting it.) But, hopefully what I've shown above will be workable.
One way to do this is following:
var
Event : TNotifyEvent;
begin
Event := myCheckbox.OnClick;
try
myCheckbox.OnClick := nil;
//your code here
finally
myCheckbox.OnClick := Event;
end;
end;
HTH
The internal design of the TCustomCheckBox is that it triggers the Click method every time the Checked property if changed. Be it by actually clicking it or setting it in code. And this is happening here when you call EnableControls because the control gets updated to display the value of the linked field in your dataset.
TButtonControl (which is what TCustomCheckBox inherits from) has the property ClicksDisabled. Use this instead of (or in addition to) the DisableControls/EnableControls call. Unfortunately it is protected and not made public by TCustomCheckBox but you can use a small hack to access it:
type
TButtonControlAccess = class(TButtonControl)
public
property ClicksDisabled;
end;
...
TButtonControlAccess(MyCheckBox1).ClicksDisabled := True;
// do some dataset stuff
TButtonControlAccess(MyCheckBox1).ClicksDisabled := False;
Of course you can put this into a method that checks all components and sets this property if the control inherits from TCustomCheckBox or some other criteria.
i downloaded a DBDateTimePicker which i am using to edit dates in a table, a start date and completion date. When appending to a table in normal edit boxes i use the following code before the append and post in order to prevent a completion date being set before a start date.
if (DTPAddStartDate.Date) > (DTPAddCompletionDate.Date) then
begin
raise Exception.Create ('The Completion date of a Job can not be before a Start Date');
abort;
end;
The problem is that i want to be able to achieve the same thing when editing dates via the DBDateTimePickers, if i have similar code on the BeforePost event or something then i will get premature error messages as the user may not have had a chance to edit the other related DBDateTimePicker. I have no idea how i could relate the two fields so that i can validate them together somehow or come up with another solution to the problem, any suggestions?
(Im using an adotable connected to access and the component was downloaded from the following link, http://apollosoft.net/jms/).
I guess you are using the TDBDateTimePicker from this page but hard to say, it's missing in your question at this time.
However you may use the field's OnValidate event which is being fired whenever the value is changed, but it's the stage when the data are going to be written do the database, so it's IMHO the unnecessary wasting of time.
You can validate the values before they tries to update the record, what is in the case of (almost ?)every DB aware control when you try to exit the control, so you may pretty easily add some validation event, something like OnCanUpdate to your date time picker.
Because of missing link to the source where did you get your component I'll assume you are using this TDBDateTimePicker. If you add to the DBDateTimePicker.pas file the following lines and rebuild your package where is installed, the new OnCanUpdate event will appear in the component's Object Inspector. This event if you use it has one important parameter Allowed, if you set it to True you will allow the component to update the record, if you set it to False (what is default) no data will be updated in your dataset. So it's the place where you can do the validation and decide you want to update record or not.
type
TDBDateTimePicker = class;
TOnCanUpdate = procedure(Sender: TDBDateTimePicker;
var Allowed: Boolean) of object;
TDBDateTimePicker = class(TDateTimePicker)
private
FOnCanUpdate: TOnCanUpdate;
procedure CMExit(var Message: TCMExit); message CM_EXIT;
published
property OnCanUpdate: TOnCanUpdate read FOnCanUpdate write FOnCanUpdate;
end;
procedure TDBDateTimePicker.CMExit(var Message: TCMExit);
var
Allowed: Boolean;
begin
if Assigned(FOnCanUpdate) then
begin
Allowed := False;
FOnCanUpdate(Self, Allowed);
if not Allowed then
begin
SetFocused(True);
Exit;
end;
end;
try
FDataLink.UpdateRecord;
except
SetFocus;
raise;
end;
SetFocused(False);
inherited;
end;
So the code in the OnCanUpdate event may looks like this (note you can use this event as common for both of your date time pickers).
procedure TForm1.DTPAddStartDateCanUpdate(Sender: TDBDateTimePicker;
var Allowed: Boolean);
begin
if (DTPAddStartDate.Date) > (DTPAddCompletionDate.Date) then
begin
// the Allowed parameter is in current code initialized to False
// when it comes into this event, so the following line has no
// sense here
Allowed := False;
// here you can display the error message or raise exception or just
// whatever you want, the record won't be modified in any way
Application.MessageBox('The completion date of a job cannot be before a ' +
'start date. The record won''t be modified ;-)', 'Date Input Error...',
MB_YESNO + MB_ICONSTOP + MB_TOPMOST);
end
else
// only setting the Allowed to True will actually allows the record to be
// updated, so if your validation passes, set the Allowed to True
Allowed := True;
end;
Caviate:
A shot in the dark here since the component isn't familiar. If you have the components in a form then the objects are member variables of the form. Assuming they are created when the form is initialized (you're using them so they are) simple check an event when they lose focus.
//Pseudo Code--I'm sure it is using a standard event model
myForm.DTPAddCompletionDate.OnChange(...)
begin
//do your check if it is successful something like...
if not (DTPAddStartDate.Date > DTPAddCompletionDate.Date) then
return;
else
someSubmitButton.enabled = true;
end;
I don't have Delphi in front of me and it has been years so the code probably won't match up perfectly. However, the idea should work. Just check the references.
I want to create a 'fake' data field in a DataSet (not ClientDataSet):
the field should not be stored in the db
it's not a calculated field (the user should be allowed to enter input data)
the field has business logic meaning, so after the user updates its value it should update other fields (with the OnFieldChange event)
I know I can have a simple no-dbaware control, capture it's OnChange event and perform the calculations there (or call a DataModule function, where the DataSet resides) but I think it's more clean if I can reutilize the dataset automatic binding with db-ware controls and dataset events..
Also this way the unique connection between the Form (Presentation) and the DataModule (Model) it's the DataSet (less coupling)..
PD: I'm using fibplus, with I think the solution (if any) will be at the VCL level..
Thanks!
Have you tried using an InternalCalc field? Your data aware controls will let you edit an InternalCalc field's value, and the value is stored in the dataset.
If you create an InternalCalc field in the dataset (TClientDataSet, TQuery, etc.) at design time, it's almost exactly what you're asking for.
You could create an updatable view (with before insert/update/delete triggers) in your Interbase/Firebird database. This would look like a "virtual table" to the client (the select statement of the view's declaration can also contain "virtual fields") and it's totally up to you how you implement the triggers.
You can make a Calcfield writeable:
type
TCalcStringField = class(TWideStringField)
function GetCanModify: Boolean; override;
end;
function TCalcStringField.GetCanModify: Boolean;
begin
// Makes Calcfield editable
//if FieldNo > 0 then
if DataSet.State <> dsSetKey then
Result := not ReadOnly and DataSet.CanModify
else
Result := IsIndexField
//else
// Result := False;
end;
Set Calculated to true, and in OnSetText of this Field you can then write the Text to any other place
procedure TformXX.XXOnSetText(Sender: TField; const Text: string);
begin
...
end;