How do you select a row in DBGrid and let it enter the editing state?
Maybe you'll ask me what's the use? Oh, do the database application brothers will have deep feelings, when using DBGrid display of the field too much, users have to pull the bottom of the scroll bar, to see the most right thing, if not set Dbgrid->options[dgrowselect], then, pull to the far right, It is possible to look at the serial, if set Dbgrid->options[dgrowselect], then pull to the rightmost, will not look at the serial, but when the mouse clicks on other rows (not the currently selected row), the DBGrid view will return to the display of the leftmost column, It was really troublesome and the user had to haul the following scroll bar again and again.
The core code is as follows:
type
TMyDBGrid=class(TDBGrid);
//////////////////////////////////
//DBGrid1.Options->dgEditing=True
//DBGrid1.Options->dgRowSelect=False
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
with TMyDBGrid(Sender) do
begin
if DataLink.ActiveRecord=Row-1 then
begin
Canvas.Font.Color:=clWhite;
Canvas.Brush.Color:=$00800040;
end
else
begin
Canvas.Brush.Color:=Color;
Canvas.Font.Color:=Font.Color;
end;
DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;
end;
His solution is: curve the National salvation, cancel Dbgrid->options[dgrowselect], the current selected line of the background to draw blue, as if it was selected, the idea is really wonderful. Our company uses C++builder, I had to change this code to C++builder version, at this time, I found that the subtlety of the code.
I found that the Datalink property was declared as protected in Tcustomdbgrid, and that its visibility was not declared in DBGrid, so it could not be used directly, and the row property was declared Tcustomgrid in protected, In the subclass of Tcustomgrid also does not declare its visibility, so why does this code run well in Delphi?
The reason for this is that: objectpascal cell encapsulation, the class defined in the same unit, is the relationship between friends, and we'll look at the beginning of this code:
Type
Tmydbgrid = Class (TDBGrid);
Declared a Tmydbgrid class, then, the current form class and the Tmydbgird class are friends, then of course the current form class can directly access the Tmydbgrid private properties row and Datalink, everything is clear, then in C + + is a good implementation, The core code is as follows:
void __fastcall tmainform::lineseledit (tobject *sender,const trect &rect, int datacol, Tcolumn *Column, Tgriddrawstate state)
{
Class tmygridbase:public Tcustomgrid
{
Public:
__propert Y Row;
};
Class Tmygrid:public Tcustomdbgrid
{
Public:
__property DataLink;
};
Tmygrid *mygrid = (tmygrid*) Sender;
Tmygridbase *mygridbase = (tmygridbase*) Sender;
TDBGrid *grid = (tdbgrid*) Sender;
if (Mygrid->datalink->activerecord = = mygridbase->row-1) {
Grid->canvas->font->color = Clwhite;
Grid->canvas->brush->color = TColor (0x00800040);
} else {
Grid->canvas->brush->color = Grid->color;
Grid->canvas->font->color = grid->font->color;
}
Grid->defaultdrawcolumncell (rect,datacol,column,state);
}
I encapsulate it as a function, and the parameter of the function is the same as the parameter of the DBGrid Ondrawdatacell, the way to use it is to cancel the setting of Dbgrid->options[dgrowselect], and then set the dbgrid-> Defaultdrawing = False, and then call this function in this DBGrid Ondrawdatacell event, as follows:
void __fastcall TMainForm::DBGridDrawColumnCell(TObject *Sender,
const TRect &Rect, int DataCol, TColumn *Column,
TGridDrawState State)
{
this->LineSelEdit(Sender,Rect,DataCol,Column,State);
}