Positioning and coloring implementation of rows in DBGrid in DELPHI
When operating a database system, I found that the data row in the DBGrid control could not be positioned and that after locating a certain row, the current row could not be clearly marked (such as changing the color). After searching for a lot of information, I found that There is basically no introduction to this content, including some dynamic top row colors. Articles such as Se and others all talk about operations performed during data initialization, but there is no repositioning function after the data source is refreshed, so I determined to solve this problem. After a morning of research on DELPHI help and viewing relevant information, I got a little Xiaocheng, now write it down and learn from it together with all my colleagues. Please give me some advice on any inappropriateness.
First, I searched the Ttable and Tquery controls and found no method that can directly locate the data row. I also searched for the DBGrid and found no corresponding function that can directly locate a certain row of data. Then I searched from the class of the data set. After continuous efforts, I finally found it. I found a method in TdataSet: GoToBookmark. This method can make the record pointer in the current DBGrid point to the row you need to specify.
After finding this method, the problem is only half solved. The color of the data row pointed to by the current pointer must also be changed. That is to say, the currently selected row must be marked in an obvious way (except for the small and unsightly mark on the DBGrid). (Except for obvious bookmarks) This is obviously implemented in the DBGrid control. There is an event OnDrawDataCell in the DBGrid control. Overloading this method can change the color of the specified data row.
The specific usage process is as follows:
(1) Dynamically position data rows
//================================================ ===============
//Process name: DyDbgDataLine
//Author: haitian
//Date: 2003-02-22
//Function: Automatically move to a row of data in the DBGrid control that meets this condition based on user-specified conditions
//Input parameters:
// sValue: The value of the row that currently needs to be moved;
// tab: data of the corresponding table in the current DBGrid;
// dsr: the data source that currently needs to be operated;
//Return value: None
//Modify records:
//================================================ ================
PRocedure DyDbgDataLine(sValue:string;tab:Ttable;dsr:TDatasource)
var
bookmark:TBookMark;
begin
//Record the currently marked line;
bookmark:=self.tab.GetBookmark;
self.tab.first;
while not self.tab.Eof do
begin
if self.tab.FieldByName('cpbh').AsString= sValue then
begin
bookmark:=self.tab.GetBookmark;
break;
end;
self.tab.Next;
end;
self.dsr.DataSet.GotoBookmark(pointer(bookmark));
End;
Description: The table used has been bound to the DBGrid on the current display interface;
(2) Change the color to mark the current data row
First, set the DefaultDrawing property of DBGrid to false; then call the following function in the OnDrawDataCell event function:
//================================================ ===============
//Process name: DrawLine
//Author: haitian
//Date: 2003-02-22
//Function: Change the color of the specified row in Dbgrid as a mark;
//Input parameters:
// zdm: field name;
// Rect: a unit of the row that needs to be entered and exited;
// Field: currently displayed field;
// state: the display status of the current row;
// zdz: The value of the row that currently needs to be moved;
// tab: data of the corresponding table in the current DBGrid;
// dbg: DBGrid that currently needs to be operated;
//Return value: None
//Modify records:
//================================================ ================
procedurDrawLine(tab:Ttable;const Rect:Trect;Field:Tfield;state:TgridDrawState;dbg:TDBGrid)
begin
if (tab.fieldbyname(zdm).asstring=zdz)then
begin
dbg.canvas.font.color:=clred;
dbg.canvas.brush.color:=clyellow;
end;
dbg.DefaultDrawDataCell(Rect,Field,State);
end;