How to adjust row height of DBGrid - Delphi - delphi

I have a DBGrid that receives data from an ADOTable in a data module for my project, but I noticed the text or fields of the records do not display fully. The bottom part of a "g" would be cut off due to the small row height. How do I code to change this?
I know you can change column widths. Could one do something similar for rows as well?

The only way you can do this at design-time is to define your own TDBGrid descendant, add a RowHeight property to it and install it in the IDE.
The following will work at run-time:
type
TMyDBGrid = class(TDBGrid);
procedure TForm1.Button1Click(Sender: TObject);
begin
TMyDBGrid(DBGrid1).DefaultRowHeight := 32;
end;
This works because DefaultRowHeight is a protected property of TCustomGrid, from which TDBGrid descends.

Related

Delphi: Bind a Chart with a DBGrid

I want to bind a Chart with a DBGrid.
Refer to the exhibit.
When I click in the DBGrid on number 3 (X-Axis), then the bar at position three should be highlighted(or a pointer to bar 3).
When I click on number 4 in the Grid then bar 4 is highlighted etc.
I'm using a TDBChart
Is there a way to do this?
Not knowing what the charting component is, cannot provide a working example, but the key which you are looking for is to use the AfterScroll event for the dataset in the grid. Since each row represents a different record, that event is fired when the grid selection moves to the row.
Edit: This doesn't highlight with a box, but does change the color of the value marks at the top of each bar. Hopefully this gets you on your way. MyQuery is what is feeding the datasource
var
savecolor : tcolor;
procedure MyForm.FormShow(Sender:TObject);
begin
...
SaveColor := dbchart1.series[0].marks.items[0].color;
...
end;
procedure MyForm.MyQueryBeforeScroll(DataSet : TDataSet);
begin
dbchart1.series[0].marks.items[MyQuery.recno-1].color := SaveColor;
end;
procedure MyForm.MyQueryAfterScroll(DataSet:TDataSet);
begin
dbchart1.series[0].marks.items[MyQuery.recno-1].color := clRed;
end;

Delphi, expand grouped by grid by default

I have this procedure for a grid in Delphi, and I need to add this property to expand all collapsed grouped by data in the grid.
procedure TProjectForm.dxDBGridEventDrawCell(
Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
inherited;
DrawHighlight(ACanvas);
TcxGridDbTableView.ViewData.Expand(True);
end;
I get the following error:
E2233 Property 'ViewData' inaccessible here
Help is appreciated please. And I also need to remove the collapsible button for the grouped data in this grid. Thank you
You can call cxGrid1DBTableView1.ViewData.Expand(True) without problems as long as you don't do in in a drawing event like the one in your q. However, you don't actually need to do this if you use the example below.
This works fine
procedure TDevexGroupingForm.FormCreate(Sender: TObject);
begin
cxGrid1DBTableView1.Columns[2].GroupIndex := 0; // group by the 3rd column
// NOTE: this step is only necessary if the table view has not been grouped at design-time
cxGrid1DBTableView1.DataController.Options := cxGrid1DBTableView1.DataController.Options
+ [dcoGroupsAlwaysExpanded]; // this hides the +/- buttons of the grouped nodes
cxGrid1DBTableView1.DataController.FocusedRowIndex := 0; // focuses the first group
end;
Note : This has been updated at #Nil's suggestion.
The first line, setting a column's GroupIndex is only necessary if the TableView has not already been grouped at design time.
Setting the FocusedRowIndex is optional, depending on how you want the TableView to display initially
So in fact the hiding of the +/- grouping buttons and the expansion of all the top-level group nodes can be achieved by the single step of setting the DataController Options property to include the dcoGroupsAlwaysExpanded option.
Btw Setting the DataController options to suppress the drawing of the expand/collapse buttons and is derived from the article https://www.devexpress.com/Support/Center/Question/Details/Q105527/how-do-i-hide-the-expand-button-on-a-grid

How can I get a TDBLookupComboBox to show what you are typing?

I have a TDBLookupComboBox on my form.
When I put my cursor in it and type, the selection jumps a head to what I've typed (or as close as it can).
However I don't get any indication of what I've typed in the field.
TDBComboBox performs similarly to TDBLookupComboBox however, when I type in the field, the characters I type appear in regular text, and the 'completion' of the selection appears in inverse/selected following the regular text.
Is there a way I can make TDBLookupComboBox perform like TDBComboBox in this respect?
No, you can't make TDBLookupComboBox act like a TDBComboBox (without a bit of hacking).
The problem is that TDBLookupComboBox is used to lookup an index-field (normally a number) from another table. The chosen index is set in the destination-field. When you make TDBLookupComboBox "editable" (like TDBComboBox), you could type in anything, even values not in the source-table. And that shouldn't happen (by design). What index-value would you set in that case in the destination field?
You have several options.
You could "hack" TDBLookupComboBox to override the paint procedure to overwrite the selected text with the not selected (already typed) text. It's not easy. Especially if you want to maintain the functionality correctly. But here is some code where I think I've come close to what you want. Although when typing something that's not in the lookup-dataset it doesn't go any further. (It also doesn't account for right aligned text etc.):
type
TDBLookupComboBox = class(DBCtrls.TDBLookupComboBox)
protected
procedure Paint; override;
procedure KeyPress(var Key: Char); override;
end;
type
TForm1 = class(TForm)
DBLookupComboBox1: TDBLookupComboBox;
//.....
procedure TDBLookupComboBox.Paint;
var
TextPart: String;
begin
inherited;
Canvas.Font := Font;
Canvas.Brush.Color := Color;
// keeps case like the field is drawn
TextPart := Copy(Text, 1, Length(SearchText));
Canvas.TextOut(2, 2, TextPart);
end;
procedure TDBLookupComboBox.KeyPress(var Key: Char);
begin
inherited;
// we need this because paint is already called
// we need to call it again to overwrite the text
invalidate;
end;
It overrides the DBLookupComboBox at the top of your form. You don't have to compile this into a new component. You can just paste it into your form with the DBLookupComboBox. We need to call Paint again after keypress (with invalidate) because paint is already called before coming to that routine.
Another option is to use the TDBComboBox. You can fill the pulldown-items in OnEnter.
procedure TForm1.DBComboBox1Enter(Sender: TObject);
begin
DBComboBox1.Items.Clear;
IBQuery1.First;
while not IBQuery1.EOF do
begin
DBComboBox1.Items.Add(IBQuery1.FieldByName('TESTFIELD').DisplayText);
IBQuery1.Next;
end;
end;
If that's too slow (when entering the combobox) you could also fill the items at opening of the dataset.
Another few options from Jedi-library:
JvDBLookupCombo1
Has a pulldown directly when typing. The original typed text stays in the box.
TJvDBSearchComboBox
Also does what you want but is not connected to a destination datasource. You'll need to set the desired field yourself on OnExit.

TcxGrid Auto posting if another row is focused?

I'm using Delphi the devexpress components.
I'm running into a annoying problem where the grid seems to call a postdata if I'm inserting a new row and then accidently click on a different row.
I'm not sure the TcxGrid is doing this but I want to know if there is a property I could set to prevent this from happening?
As fas as this is not a problem of the grid, but of the dataset, the only way to intervene is preveting a post for conditions you have to define.
procedure TForm.aDatasetBeforePost(DataSet: TDataSet);
begin
if YourConditionForInvaliddata then
begin
Dataset.Cancel;
Abort;
end;
end;

How to increase row height of listview in report style?

I need to add just 2px :) to a height of a row in a list view (a custom drawn progress bar is too narrow now).
There are two good answers Change Listview item height, http://www.delphipages.com/forum/showthread.php?t=49939, but I couldn't do it.
I know that it is possible to do with an image list, but I have already 16x16 images :)
Can anybody help me?
I'll appreciate it.
Respond to the CN_MEASUREITEM control notification message, as follows:
type
TListView = class(ComCtrls.TListView)
private
procedure CNMeasureItem(var Message: TWMMeasureItem); message CN_MEASUREITEM;
end;
TForm1 = class(TForm)
...
procedure TListView.CNMeasureItem(var Message: TWMMeasureItem);
begin
inherited;
Inc(Message.MeasureItemStruct.itemHeight, 2);
end;
Note: this message will only be send if the OwnerDraw property is true.
A quick and dirty alternative without writing any code would be to add a TImageList, set its width to 1 and its height to whatever you want the lines height to be and assign it to the SmallImages of the listview.

Resources