I am using a DBGrid component in Delphi. I wonder how I can set the format of a column. I have real values that I want to be displayed as currency in the grid.
Someone knows how?
You can set the DisplayFormat of the Field
You can handle OnGetText event. This approach allows to do more complex operations with the value.
If you don't add the fields to field Editor list you can get the formating by code as :
TFloatField(MyQuery.fieldByName('MyField').DisplayFormat := '0.00';
if you don't want to show the zeros replace '0.00' with '#.##';
The first port of call is the DisplayFormat of the data field in the database itself.
Related
My goal is to have a TGrid with several columns, connected to a TClientDataSet via LiveBindings. One of the columns is of type TProgressColumn.
The grid displays the data, but the progress bar column shows nothing (i.e. 0% progress).
The TProgressColumn is connected to a field of type ftInteger. The values in this field are between 0 and 100.
I've tried with ftSingle, but with no luck.
I set the type of the column to be TProgressColumn via ColumnStyle property, available under TLinkGridToDataSourceBindSourceDB/Columns.
The strange thing is that when I use TPrototypeBindSource to generate values - the TProgressColumn works, but only for ftUInteger values. It fails for ftInteger generators.
Here is a little demo (Delphi XE7):
When I put a breakpoint in TProgressCell.DrawCell() and step over the two conditions for Value.IsOrdinal and Value.IsType are skipped and ClampValue receives a value "Min" which is 0.
There seems to be something wrong with the value, passed to the function.
Is there something special when working with TProgressColumn? Do I need to use CustomFormat, CustomParse in TLinkGridToDataSourceColumn?
Is that a bug or I miss something?
UPDATE:
Thanks to "nompa" the mystery was solved!
In fact "asInteger" is the well known property of the TField class i.e.:
someDataSet.fieldByName('myFieldName').asInteger
In CustomFormat property you can get access to many things, including self.asInteger properties.
More information here:
Using Custom Format and Parse Expressions in LiveBindings
Using binding expressions in the CustomFormat property of a TLinkPropertyToField component
Formatting your Fields
How to treat an Integer field as a Boolean?
The value is string by default, not matter is a integer field. In property CustomFormat write AsInteger.
The value will be take as integer and the progress now is visible.
I have a data-aware cxgrid connected to a toracledataset object.
Inside the DB is a column defined as number(1,0) to allow boolean values.
I want to format the corresponding grid column as ComboBox to give the user the ability to choose between the two options.
How can I cast the string inside the combobox to the integer value that will be inserted in the DB?
I know how to do that with a regular combobox, but not inside a cxgrid
The best way will be to use a cxEditrepositiory
add a cxImageComboBox
add Items and define Value and Valuetype
this then can be choosen for your GridColum.RepositoryItem
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;
I use DBGrid to display Hyperlink type field from Microsoft Access database (MDB).
Normally dbgrid displays hyperlink values like "(MEMO)", without editing capablity. Is there a way to solve this?
DBGrid displays values based on field type. If it shows (MEMO) then you probably declared your field in your database as TEXT or something equivalent. Can't remember Access, but in MS-SQL Server you can change field type to varchar and DBGrid will display values as editable text.
I'm using ADO-components to connect to an access database. In a column defined as text with width 50, dataaware textfields always displays 50 characters even when the actual string value contains less characters. The value gets padded with spaces, and if the textfield is not wide enough, it looks like it is empty.
Anyone got any clues?
Thanks,
-Vegar
Edit: I'm using Delphi 2007.
The problem comes from using Char(50) instead of Varchar(50) when creating the table.
I still think there should be a way of displaying the unpadded value, but switching to varchar is ok.
Which version of Delphi?
And can't it be a field setting in the access database?
Are you using a query or a table component? If you are using a query then just embed the column name in Trim(). If not then use the OnGetText event on the field in question to put in the following code
Text := Trim(Text);