How to set focus on a particular row in a TDBgrid Delphi - delphi

I want to set the focus on a specific row on a Tdbgridview.
First, I choose the criteria field ( column concerned by search ex: FisrtName) from a combo box then I type criteria in a TeditField (ex : Jack ).
Then the arrow of the Dbgrifd should point on the concerned row.
How should I do?
thanks.

The TDBGrid component (and all other TDBxxx components as well) are what Delphi defines as dataware components. This kind of component exists as a visual expression of a dataset.
So, in a TDBGrid you should not think about focusing a row of the grid, rather think about positioning a row of the grid's dataset (using the Locate method suggested by TLama, for instance). The grid will notice that the current row of its dataset has changed (because the grid is aware about the condition of the dataset) and will focus the corresponding row.
Update 1
Below you can see an example of what I said:
MyDataset.Locate('Id', 123, []);
The code above simply looks for a certain record in a dataset named MyDataset. The first parameter is the name of a existing field in the dataset. So, you have a dataset with some fields and one of those is named Id. The second parameter is the value contained in that field in the desired row and the third parameter is some options that do not apply here. Go to the Delphi docwiki for more details on the Locate method.
In other words, we are looking for a row in which the field Id has the value 123! If the dataset can find such a row, it will become the current record (or row). If there is a TDBGrid connected to a TDataSource that is connected to TMyDataset, it will automatically update to select the corresponding row, just like you wanted.

Related

How to load pictures from a list asyncron, to show the text first

I want to load a list with thumbnail pictures from the internet. In order to have a good user experiance, I want to load and display the text of the list first, and want to load the pictures from the list in a background thread. When a picture is downloaded I want to show it in (refresh) the correspondig row of the list.
Actually, I don't know where to start. Can I use a TClientDataset component to load the text first and load the pictures in a background thread and insert it with .Locate() .Edit .Post to the dataset?
You can use a TClientDataSet for requesting the data directly, except the picture. Thus, the fetching of the main data should be fast enough.
You should load the picture as a calculated field then. Documentation says (with my own emphasis):
A calculated field displays values calculated at runtime by a
dataset's OnCalcFields event handler. For example, you might create a
string field that displays concatenated values from other fields.
To create a calculated field in the New Field dialog box
Enter a name for the calculated field in the Name edit box. Do not enter the name of an existing field.
Choose a data type for the field from the Type combo box.
Enter the size of the field in the Size edit box, if appropriate. Size is only relevant for fields of type TStringField, TBytesField, and TVarBytesField.
Select Calculated or InternalCalc in the Field type radio group. InternalCalc is only available if you are working with a client dataset. The significant difference between these types of calculated fields is that the values calculated for an InternalCalc field are stored and retrieved as part of the client dataset's data.
Choose OK. The newly defined calculated field is automatically added to the end of the list of persistent fields in the Field editor list box, and the component declaration is automatically added to the form's or data module's type declaration.
Place code that calculates values for the field in the OnCalcFields event handler for the dataset. For more information about writing code to calculate field values, see Programming a calculated field.
In OnCalcFields event handler you then need to implement asynchron loading of the picture.

Delphi - How to get a image for a cxGrid column?

I have a TcxGrid component to show data of a MS Access table. One of this columns have a picture. The column "Properties" of this column is marked as "ButtonEdit".
I don't want to use a BlobEdit. I want to create another form, for display this picture. It will be opened by OnButtonClick() event of the grid column. But, I don't know how to get the column content (as TStream or other type), or the column name/field name in the OnButtonClick() event. I can't fix the column name, because it's a "generic" grid for show any Access table.
How can I do it?
It will be nice to understand your question better if you can post your code. But offcourse You can get column contents easily by using FieldByName method. Try to use that.
You did not say how you load the data into the grid, but to get the cell content you can use:
VarAsType(cxGrid1TableView1.DataController.Values[cxGrid1TableView1.Controller.FocusedRecordIndex,cxGrid1TableView1.Controller.FocusedColumnIndex],varString)

Delphi grid with a different data type in each row, displayed dynamically

I am trying to create a Delphi grid to allow display and edit in a db grid of data that might have a different data type on each row. I would like to display a specific control for each data type, e.g. when the data type is DateTime, I want to display my custom edit control that allows typing a date in or popping up a calendar.
The data looks something like this:
Name DataType DateValue StringValue BooleanValue
---------------------------------------------------------
A Date 1/1/2007
B String asdf
C Boolean True
...and in the db, this table has a column for each possible type of value. So, there is a BooleanValue column, DateValue, etc.
What I would like to do is display a single 'Value' column in the grid that displays the appropriate edit control depending on what the 'DataType' is for that row. So, the grid should look like :
Name DataType Value
---------------------------
A Date 1/1/2007
B String asdf
C Boolean True
It seems I will need to display a different edit control (to allow the user to edit the Value column) for each row dynamically based on the value of the DataType column. I know there are more advanced grids out there that handle this sort of problem, but the powers that be will not allow anything but what is available out-of-the-box with Delphi.
Any ideas on how to make something like this work?
Personally, I would not go for editing directly inside the TDBGrid in this case, since your Table is not DB normalized (I don't use it in any case actually). I would have used a Calculated field to display the desired value in the grid, And dynamically created the TDBxxxEdits on the form for each field type (How about your own TDBTreeEdit for example, a TDBRichEdit, or a DB Image pickup editor, etc...?).
In case you do want to use your own controls on the TDBGrid, and replace the default TInplaceEdit editor, you can refer the following article: Adding components to a DBGrid, and a related article: Displaying and editing MEMO fiels in Delphi's TDBGrid
Displaying all of the data in the same column is quite easy. You can simply add a calculated string field, and change the value according to what you are storing in that row.
The editing is quite a bit more complicated. If you want to have an in-place editor, you are in for a world of hurt... I've done it, it's a pain, and takes a lot of time. If you want to display a dialog to edit the value, that's much easier. You can add a column objects to the grid and you can setup the column you have attached to the calc field to display a button. When the button is clicked you simply display the editing dialog needed for that row, and commit the edits when the dialog is closed.
There are other ways to get this done, but I would say the above would be the shortest way. Other ways may include custom draw events to display your data in one column, intercept clicks to create your own editor, etc, etc, etc...
after add calculated fields .
Sample:
procedure OnCalculate(DataSet:TDataSet);
begin
case IndexText(DataSet['ValueType'],['Date','String','Boolean']) of
0:DataSet['DateValue']:=StrToDateTime(DataSet['Value']); // also converting
1:DataSet['StringValue']:=DataSet['Value'];
2:DataSet['BooleanValue']:= MatchText(DataSet['Value'],['1','True','T','Y','Yes']);
{etc datatypes}
end;
end;

How to fill a combobox when a row in a dbgrid is selected?

In Delphi 2007 I have a DBGrid. I need to fill a ComboBox when a row in the DBGrid is selected. The data in the the ComboBox depends on the selected row, and can be different for each selected row.
The DBGrid contains products orders. When an order is selected, I need a ComboBox to be filled with batch numbers of the product in the order. This batch number is saved in the order record, when the order is executed.
I could not find a onSelect or onChange event, which I could use to do this.
So how can I do this?
You should use TDataSet.AfterScroll event
If the data comes from a DataSet in a DataModule, as it looks from comments to #Roman response, you can rely on TDataSource events.
If your datasource is also located in the datamodule, you can place a new DataSource in the form itself (IMO is where it belongs).
Then, use the TDataSource.OnDataChange event, from help:
Write an OnDataChange event handler to take specific actions when a field in the current record has been edited and the application moves to another field, or when the current record in the associated dataset changes. OnDataChange is especially useful in applications that must synchronize data display in controls that are not data-aware. This event is typically used to make sure the control reflects the current field values in the dataset, because it is triggered by all changes.
The Field parameter is nil when the linked dataset is moving to a new record or refreshing a record buffer. The Field is not nil when the changes belong to a particular field (a user or the program itself changing it's value).
I suggest the double click event (OnDblClick) to catch when the user clicks with his mouse and OnKeyPress for the Enter key (Key=#13)

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