Positioning and coloring implementation of DBGRID in Delphi
1 question
When you operate on a database system, it is found that the data lines in the DBGRID control cannot be launched, and the current line cannot be obviously marked (for example, change color) after positioning to a line, and a lot of information is found, discovered Basically, there is no introduction to this content, including some articles, such as the color of the dynamic set, etc. are all done when data is initialized, and there is no repositioning function after the data source refresh, so determined to solve this problem. On the morning, the research and viewing information about Delphi, it is slightly small, and it is now written and learned together. Inappropriate, please advise.
2 solution
First find the TTable and TQuery controls have not found the way to directly locate the data line, and also find the DBGRID nor find a function that can directly locate the data, then I will find it from the class of the data set. After continuous efforts, finally A method found in TDataSet: gotoBookmark, this method enables the record pointer in the current DBGRID to the row you need to specify.
After finding this method, the problem only solves half, but also change the color of the data line points to the current pointer, that is, you must use an obvious way to mark the current line (out of the little on dbgrid). Outside the bookmarks) This is clearly implemented in the DBGRID control, there is an event overdrawdatacell in the DBGRID control, and the desired data line can be changed.
The specific use process is as follows:
(1) Dynamic positioning data line
/ / =========================================================================================================================================================================================== ================
// Process Name: DydbGDataline
// Author: haitian
/ / Date: 2003-02-22
// Features: Automatically move to a row of data that is automatically moved to the DBGRID control based on the criteria specified by the user.
//Input parameters:
// sValue: The value of the row that is currently moving;
// Tab: data of the corresponding table in the current dbgrid;
// DSR: Data source currently required;
// Return value: no
// Modify the record:
/ / =========================================================================================================================================================================================== ================
Procedure Dydbgdataline (Svalue: String; Tab: Ttable; DSR: TDataSource) Var
Bookmark: TBOOKMARK;
Begin
// Record the current marked row;
Bookmark: = Self. Tab.getbookmark;
Self. Tab.first;
While Not Self. Tab.eof DO
Begin
IF self. Tab.fieldbyName ('cpbh'). asstring = svalue the
Begin
Bookmark: = Self. Tab.getbookmark;
Break;
END;
Self. Tab.next;
END;
Self. dsr.dataset.gotobookmark (Poinmark);
END;
Description: Use the table that has been bound to the DBGRID on the current display interface;
(2) Changing the color mark Current data line
First set the DBGRID's defaultDrawing property to false; then call the following function in the OnDrawDataAcell event function:
/ / =========================================================================================================================================================================================== ================
// Process Name: Drawline
// Author: haitian
/ / Date: 2003-02-22
// Function: Change the colors in DBGRID as tagging;
//Input parameters:
// ZDM: field name;
// Rect: a unit of the row that needs to be taken;
// Field: The current displayed domain;
// State: The current line display status;
// ZDZ: The value of the row that needs to be moved;
// Tab: data of the corresponding table in the current dbgrid;
// DBG: DBGRID currently required;
// Return value: no
// Modify the record:
/ / =========================================================================================================================================================================================== ================
Procedurdrawline (Tab: Ttable; Const Rect: TRECT; Field: Tfield; State: TgriddrawState; DBG: TDBGRID) Begin
IF (Tab.fieldByname (ZDM) .sstring = zdz) THEN
Begin
Dbg.canvas.font.color: = CLRED;
Dbg.canvas.brush.color: = ClyEllow;
END;
DBG.Defaultdrawdatacell (Rect, Field, State);
END;