I have a delphi form with a cxgrid on it, the grid is connected to a query/datasource.
If a field in the database table is an Integer, is there anyway of me displaying the integer as a string on the cxgrid column?
e.g. 1 = January
2 = February
3 = March
and so on.
Thanks,
Yes, there is such possibility.
Put TcxEditRepository component next go your grid. Double click on it, you should see the empty window with "Add..." button. Click it and from the list of available components select ImageComboBox.
Now, you need to edit Items property of this combo.
After filling up all rows go to your view (TcxGridDBTableView or TcxGridTableView) and pick the column which contains integer values. This column has a property called RepositoryItem. If you did everything correctly you should be able to select the repository item which you've created earlier (the ImageComboBox). After selecting it, your column should immediately display month names instead of numbers.
Other approach is to override cxGrid column OnGetDisplayText event. You could do something like that:
procedure TSomeForm.GetDisplayText(Sender: TcxCustomGridTableItem;
ARecord: TcxCustomGridRecord; var AText: string);
var
nVal : Integer;
begin
nVal := ARecord.Values[Sender.Index];
case nVal of
1: AText := "January";
//and so on
end;
end;
Related
I have got a table column of datatype char and by making use of ADO dataset I am fetching the data. But because of type char, it displays the data with trailing extra spaces. And the form on which this ADO dataset lies is inherited by many forms. I want to trim the extra spaces by making some change in the Delphi code on parent form by using some ADO dataset property
I have made use of FixedChar property of the data field by making it to false but that did not work
Select Cast(mycharcolumn as varchar(10)) as mynewcharcolumn from tablename
If you can change the query that retrieves the data, like I've shown here, then you effectively convert your char columns to varchar columns at the dataset level.
If you are unable to make changes to the query, then another way is to add a calculated column to the Tfield list of the dataset, and in the OnCalcFields event handler, you can Trim(mycharcolumn) to 'calculate' the value of the calculated field, which you then use in the code, in place of the original field.
You can also set the OnGetText event of the field and trim the text:
procedure TMyForm.AdoDataSet1GetText(Sender: TField; var Text: string; DisplayText: Boolean);
begin
Text := Trim(Text);
end;
I'm writing a FMX program in XE4 using the TGrid class. I want to extract the value from a particular column of the row I double clicked in a TGrid. The grid was loaded with strings from a database table (using live binding).
For example, say the TGrid displays 5rows x 10columns, and I'm interested in the values in column 9. If I double click anywhere in row 2, I want the value of cell(row=2, col=9) to be put in a TEdit.
I may be looking at things simplistically, but in TGrid I did not find any function that can get me a cell value based on its (row,col).
You can use the protected method GetValue.
function GetValue(Col, Row: Integer): TValue; virtual;
Then using the ColumnIndex and Selected properties you can get the current col and row.
Try this
type
TGridClass=class(TGrid);
procedure TForm1.Grid1DblClick(Sender: TObject);
begin
ShowMessage(TGridClass(Sender).GetValue(TGrid(Sender).ColumnIndex, TGrid(Sender).Selected).ToString);
end;
Or you can use something like
Grid1.Columns[Col].Controls[Row]
I'm used to calculate the balance of the calculated field., But when I connect the dbgrid. Calculated by moving the scroll is wrong.
Please get help
var
Form1: TForm1;
i : Integer;
procedure TForm1.FormShow(Sender: TObject);
begin
i := 0;
DataSource1.DataSet := ADOTable1;
DBGrid1.DataSource := DataSource1;
end;
procedure TForm1.ADOTable1CalcFields(DataSet: TDataSet);
begin
i := (ADOTable1Debtor.AsInteger - ADOTable1creditor.AsInteger) + i;
ADOTable1Total.AsInteger := i;
end;
Now run the application and move the scroll in dbgrid column numbers (total) will change.
I'd like to know how to stop the change.
The calculated fields is designed to show value calculations at the row level and is not designed to make aggregations (calculations based on a set of rows).
For example, the database layer will fire the OnCalc event in no particular order and every time is needed to obtain the value of the field (for display pruposes, for example), since that value is not stored and may (and usually do) depend on the values of other fields.
In a set of 10 rows, you can get it called for rows 1, 2, 3, 4, 5 and then in 1 again...
You can use it, for example, in a line_total column which is the product of quantity and unit_price, but you can't use it to show e.g. the sum(line_total) of all the lines, as I infer you're trying to do in the shown code.
How to perform aggregations then?
You may want to link your DataSet to a ClientDataSet, which have AggregateFields, in which you can perform calculations like SUM(Quantity * Price) on the entire row-set or sub-sets based on a Index.
To learn more about AggregateFields read ClientDataSet Aggregates and GroupState by Cary Jensen in EDN.
What you are trying can't work, since any scroll will change the result.
Take a AdoDataset instead with following Commandtext
select ID, creditor, Debtor,(Select sum (Debtor-Creditor) from Table1 t where t.ID<=Table1.ID) as Total
from Table1 order by ID
I have e DevExpress grid where I would like to expand some groups that contain a certain value.
I have a grouping on year and week and I would like to have only the current year and the current week expanded as default.
I have been searching for some hints, but havn't found any yet.
The question is a "bit" old but since you are still active I'll answer it.
For the simplicity I've used only two columns: "Year" and "Population", so the grouping is only by "Year".
Here is the screenshot of an example application:
You see an initial data.
I am grouping rows using a "Year" column.
I am expanding group with "Year" = 1, I've assumed that current year = 1.
procedure TfrmMain.btnExpandClick(Sender: TObject);
const
CurrentYear = 1;
var
i: Integer;
begin
for i := 0 to tbvMain.ViewData.RowCount - 1 do
begin
// Check if a row is a grouping row.
if not tbvMain.ViewData.Rows[i].IsData then
begin
// Check if a grouping value is the one that you want expanded.
if tbvMain.ViewData.Rows[i].Values[clmYear.Index] = CurrentYear then
tbvMain.ViewData.Rows[i].Expand(False);
end;
end;
end;
procedure TfrmMain.FormCreate(Sender: TObject);
var
i: Integer;
begin
// Prepare some random data.
Randomize;
tbvMain.DataController.RecordCount := 10;
for i := 0 to tbvMain.DataController.RecordCount - 1 do
begin
tbvMain.DataController.Values[i, clmYear.Index] := Random(3) + 1;
tbvMain.DataController.Values[i, clmPopulation.Index] := Random(100);
end;
end;
The key here is to check if the given row is data or not. If it is not data then it is a grouping row, for this you need to use tbvMain.ViewData.Rows[i].IsData
You can find full source code here. Please note that I've used Delphi 2009 and Dev Express build 56.
Have you try with something like this?
TableView1.ViewData.Records[0].Expand(true)
If the current year is the first, otherwise replace 0 with the correct record number
This question already has answers here:
Setting a DBGrid column format in Delphi
(3 answers)
Closed 8 years ago.
I would like to format specific cells to force two decimal places. The data is coming from an ElevateDB stored procedure and hooked into a TDataSource.
EDIT: SQL Programming Note:
I wasn't sure if this was just an ElevateDB issue or not. Before knowing about the Fields Editor, I attempted to format the data at the SQL level by using a CAST (NumericField as varchar(10)) statement inside the stored procedure. By doing so, it did not expose the DisplayFormat property inside the fields editor for this particular field.
When I removed the CAST() statement from the stored procedure, the DisplayFormat property showed up in the Fields Editor.
You can format the DBGrid columns by formatting the underlying fields. If not done, create static fields in your dataset and then set the DisplayFormat property of the field in question to 0.00 and you are done.
I use same method as Uwe answered, in code after opening the dataset just add this line of code to format certian column:
TFloatField(MyDs.FieldByName('Cost')).DisplayFormat := '0.00';
You can format the field using the DrawDataCell event.
procedure TFormMain.DBGridCompareDrawDataCell(Sender: TObject;
const Rect: TRect; Field: TField; State: TGridDrawState);
begin
if Field.Name = 'FIELDNAME' then
TFloatField(Field).DisplayFormat := '#,##0.00';
end;