Increase and deletion of StringGrid rows
typeTExCell = class (TStringGrid) publicprocedure DeleteRow (ARow: Longint); procedure DeleteColumn (ACol: Longint); procedure InsertRow (ARow: LongInt); procedure InsertColumn (ACol: LongInt); end; procedure TExCell.InsertColumn (ACol: Integer); beginColCount: = ColCount 1; MoveColumn (ColCount-1, ACol); end; procedure TExCell.InsertRow (aRow: Integer); beginRowCount: = RowCount 1; MoveRow (RowCount-1, aRow); end; procedure TExCell.DeleteColumn (ACol: Longint); beginMoveColumn (ACol, ColCount -1); ColCount: = ColCount - 1; end; procedure TExCell.DeleteRow (aRow: Longint); beginMoveRow (aRow, RowCount - 1); RowCount: = RowCount - 1; END;
How to write a column in the StringGrid with a Check function, like the Checkbox effect
unit Unit1; interfaceusesWindows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Grids; typeTForm1 = class (TForm) grid: TStringGrid; procedure FormCreate (Sender: TObject); procedure gridDrawCell (Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); procedure gridClick (Sender: TObject); private {Private declarations} public {public declarations} end; varForm1: TForm1; fcheck, fnocheck: tbitmap; implementation {$ R * .DFM} procedure TForm1.FormCreate (Sender: TObject); vari: SmallInt; bmp: TBitmap; beginFCheck: = TBitmap.Create; FNoCheck: = TBitmap.Create; bmp: = TBitmap.create; try bmp.handle: = LoadBitmap (0, PChar ( OBM_CHECKBOXES)); With FNoCheck Do Begin width: = bmp.width div 4; height: = bmp.height div 3; canvas.copyrect (canvas.cliprect, bmp.canvas, canvas.cliprect); End; With FCheck Do Begin width : = Bmp.Width Div 4; Height: = Bmp.Height Div 3; Canvas.copyRect (canvas.clipRect, Bmp.canvas, Rect (Width, 0, 2 * width, height); end; finally bmp.freeend; End; Proced ure TForm1.gridDrawCell (Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); beginif not (gdFixed in State) then with TStringGrid (Sender) .Canvas dobegin brush.Color: = clWindow; FillRect (Rect ); If Grid.cells [Acol, Arow] = 'YES' THEN DRAW ((Rect.Right Rect.LCheck.width) Div 2, (Rect.Bottom Rect.top - fcheck.Height) Div 2, FCHECK ELSE DRAW ((Rect.Right Rect.LCheck.width) Div 2, (Rect.Bottom Rect.top - fCheck.Height) Div 2, FnoCheck); End; End; Procedure TFORM1.GRIDCLICK (Sender : TOBJECT); Beginif Grid.cells [Grid.col, Grid.Row] = 'YES' Then Grid.cells [Grid.col, Grid.Row]: = 'NO'
Else Grid.cells [Grid.col, Grid.Row]: = 'yes'; end; end.stringgrid component Cells content branch is displayed in TStringGrid.ondrawcell event
Drawtext (stringgrid1.canvas.handle, pchar (stringgrid1.cells [acol, it]), length (stringgrid1.cells [acol, appro "), Rect, DT_wordbreak or dt_left); you can implement a word wrap!
How to make read-only columns in the StringGrid in the OnseTelectCell event handler, add: (All columns are set to modifiable)
IF col MOD 2 = 0 Then Grd.Options: = Grd.Options [GoEditing] Else Grd.Options: = GRD.OPTIONS - [gaediting];
StringGrid issues from text (SAVE / LOAD A TSTRINGGRID TO / FROM A FILE?) StringGrid issues from text
// Save a TStringGrid to a fileprocedure SaveStringGrid (StringGrid: TStringGrid; const FileName: TFileName); varf: TextFile; i, k: Integer; beginAssignFile (f, FileName); Rewrite (f); with StringGrid dobegin // Write number of Column / Rows Writeln (F, Colcount); Writeln (f, rowcount); // loop through cells for i: = 0 to colcount - 1 do fork: = 0 to rowcount - 1 do Writeln (F, Cells [i, k]); end; CloseFile (F); end; // Load a TStringGrid from a fileprocedure LoadStringGrid (StringGrid: TStringGrid; const FileName: TFileName); varf: TextFile; iTmp, i, k: Integer; strTemp: String; beginAssignFile (F, FileName); Reset (f); with stringgrid dobegin // Get Number of Columns Readln (f, ITMP); Colcount: = ITMP; // Get Number of Rows Readln (F, ITMP); RowCount: = ITMP; // Loop through cells & fill in value for i: = 0 to colcount - 1 do fork: = 0 to RowCount - 1 Do Begin Readln (f, straTemp); cells [i, k]: = strTemp; end; Closefile (f); end; // save stringgrid1 to 'c: .txt': Procedure TFORM1.B utton1Click (Sender: TObject); beginSaveStringGrid (StringGrid1, 'c: .txt'); end; // Load StringGrid1 from 'c: .txt': procedure TForm1.Button2Click (Sender: TObject); beginLoadStringGrid (StringGrid1, 'c: .txt '); End; ************************************************************ Open an existing text file and put the content into the stringgrid, the text line is consistent with the StringGrid line; if you encounter a space in the text, you will be placed in the next cells. Get it! Note that I only wrote a space interval, you can modify splitstring to separate with multiple spaces! Procedure TForm1.Button1Click (Sender: Tobject); Varaa, BB: TstringList; i: integer; begina: = tstringlist.create; bb: = tstringlist.create; aa.loadfromfile ('c: .txt'); for i: = 0 to aa.count-1 dobegin bb: = splitstring (aa.strings [i], ''); stringgrid1.rows [i]: = bb; end; aa.free;
Bb.free; end; Splitstring is: Function Splitstring (const source, ch: string): TStringList; VARTEMP: String; i: integer; beginResult: = tstringlist.create; temp: = source; i: = POS (CH, Source); While i <> 0 dobegin Result.add (Copy (Temp, 0, I-1)); delete (temp, 1, i); i: = POS (CH, TEMP); end; result.add ( Temp); END; STRINGGRID Component Cells content Align similar code to Drawcell in StringGrid: Varvcol, Vrow: longint; beginvcol: = acol; vrow: = arow; with sender as tstringgrid, canvas do if vcol = 2 THEN Begin /// Set to Right Align SetTextAlign (Handle, TA_RIGHT) for the second column; refRect (Rect); Rect, Rect.right-2, Rect.top 2, Cells [Vcol, VROW]); End; end; When I contain the StringGIRD's options attribute, whenewes in the StringGrid, choose the stringGrid, choose the row to use dark blue display, I want to change dark blue to other colors What should I do? When I contain the GorowSelect item in the StringGIRD Options attribute, whenever I select a line in StringGrid, choose the row to use dark blue display, I want to change dark blue to other colors What should I do? procedure TForm1.StringGrid1DrawCell (Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); beginWith StringGrid1 dobegin If (ARow = Krow) and not (acol = 0) then begin Canvas.Brush.Color: = clYellow ; // CLBLUE; canvas.FillRect (Rect); canvas.font.color: = CLBLACK; canvas.textout (Rect.left, Rect.top, cells [acol, arow]); end; end; end; procedure TFORM1. Stringgrid1SelectCell (Sender: Tobject; ACOL, AROW: Integer; var canselect: boolean; beginkrow: = arow; // * kcol: = acol; end; Note: The value of the variable KROW must be 1 or other less than 0 Value, otherwise, if the first line is locked, the color of the first line will be replaced by self-colors, and the locking line will not be redrawn.
How to change the background of the StringGrid control, a column read-only property, the StringGrid control title bar. How to change the background of the StringGrid control, a column of read-only properties, and the StringGrid control title bar. Align.
Please refer to the following code: Process the background color in the OnDrawcell event. The program is as follows: // turn the second column background into red. procedure TForm1.StringGrid1DrawCell (Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); beginif not ((acol = 1) and (arow> = stringgrid1.fixedrows)) then exit; with stringgrid1 dobegin canvas. Brush.Color: = CLRED; canvas.FillRect (Rect); canvas.textout (Rect.Text 2, Rect.top 2, cells [acol, it]) end; end; // Add to code, then stringgrid the fourth column to the other column read-only non-read-only procedure TForm1.StringGrid1SelectCell (Sender: TObject; ACol, aRow: Integer; var CanSelect: Boolean); beginwith StringGrid1 do begin if ACol = 4 then Options:. = Options - [goEditing ] else Options: = Options [goEditing]; end; procedure TForm1.StringGrid1DrawCell (Sender: TObject; ACol, aRow: Integer; Rect: TRect; State: TGridDrawState); vardx, dy: byte; beginif (acol = 4) and NOT (AROW = 0) THEN with STRINGRID1 Do Begin canvas.brush.color: = CLYELLOW; canvas.FillRect (rect); canvas.font.color: = CLBLUE; DX: = 2; // Adjust this value, control word The horizontal position displayed in the grid DY: = 2; / / Adjust this value, the vertical position displayed in the grid canvas.Textout (Rect.left DX, Rect.top Dy, Cells [Acol, AROW] End; // Control the title bar to IF (AROW = 0) THEN WITH STRINGRID1 Do Begin Canvas.brush.Color: = CLBTNFACE; canvas.FillRect (rext); dx: = 12; // Adjust this value, the control word is displayed in the grid Horizontal position DY: = 5; // Adjust this value, control the vertical position displayed in the grid Canvas.Textout (Rect.LEFT DX, RECT.TOP DY, CELLS [ACOL, AROW]); END; use the Enter key to simulate the function of the Tab key to switch the function of the TAB button in StringGrid ... Procedure TFORM1.STRINGRID1KEYPRESS (Sender: Tobject; var key: char); labelnextTab; beginif key = # 13 thenbegin key: = # 0; NextTAB: IF (StringGrid1.col)
Begin
StringGrid1.col: = stringgrid1.col 1;
end
Else
Begin
IF stringgrid1.row> = stringgrid1.rowcount-1 TenstringGrid1.rowcount: = stringgrid1.rowcount 1;
StringGrid1.row: = stringgrid1.row 1;
StringGrid1.col: = 0;
Goto nexttab;
END;
END;
END;
.........
How to empty StringGrid
WITH STRINGGRID1 Do for i: = 0 to colcount - 1 do cols [i] .clear;
Select a cell and modify it in this cell -> Select a cell, then modify the setting attribute in the cell: StringGrid1.Options: = StringGrid1.Options [gaediting];
Let added StringGrid recorded in the page display in Uses: ADOInt // set first PageSize, taken PageCountprocedure TForm1.Button1Click (Sender: TObject); beginADoquery1.Recordset.PageSize: = spinedit1.Value; Edit1.Text: = IntToStr (ADoquery1 .Recordset.PageCount); ShowData (spinedit2.Value); end; // then AbsolutePage diversion to a large universe of data StringGrid1 the procedure TForm1.ShowData (page: integer); variRow, iCol, iCount: Integer; rs: ADOInt. Recordset; beginADoquery1.Recordset.AbsolutePage: = Page; Currpage: = page; iRow: = 0; iCol: = 1; stringgrid1.Cells [iCol, iRow]: = 'FixedCol1'; Inc (iCol); stringgrid1.Cells [iCol , IROW]: = 'fixedcol2'; INC (IROW); DEC (ICOL); rs: = adoquery1.recordset; for iCount: = 1 to spinedit1.value do begin stringgrid1.cells [icol, irow]: = rs.fields .Get_item ('FieldName1'). Value; Inc (ICOL); StringGrid1.cells [ICOL, IROW]: = rs.fields.get_item ('FieldName1'). Value; Inc (IROW); DEC (ICOL); RS. Movenext; end; // Previous procedure tform1.button2click (sender: TOBJECT); Beginif (currpage) <> 1 dam showdata (currpage-1); end; // Next Procedure TFORM1.BUTTON3CLICK (Sender: TOBJECT); Beginif Currpage <> adoQuery1.recordset.pagecount the showdata (CurrPage 1);
Print StringGrid Source Code This code did not understand, but some friends may need, so share it:) Procedure tform1.speedButton11Click (Sender: Tobject); varIndex_r, Aleft: integer; index: integer; beginstringGrid_file ('d: /AAA.TXT ':;if not linktextfile kilin showMessage (' failed "; exit; end; // quickrew1.dataset: = adotable1; index_r: = resize; aleft: = 13; create_title (titleband1, ALeft, 24, HeaderControl1.Sections.Items [0] .Width, 20, HeaderControl1.Sections [0] .Text, taLeftJustify); with Create_QRDBText (DetailBand1, ALeft, 8, StringGrid1.ColWidths [0], 20, StringGrid1.Font , taLeftJustify) dobegin DataSet: = ADOTable1; DataField: = ADOTable1.Fields [0] .DisplayName; end; ALeft: = ALeft StringGrid1.ColWidths [0] * Index_R Index_R; For Index: = 1 to ADOTable1.FieldCount - 1 dobegin Create_VLine (TitleBand1, ALeft - 13,16,1,40); create_Title (TitleBand1, ALeft, 24, HeaderControl1.Sections.Items [Index] .Width, 20, HeaderControl1.Sections [Index] .Text, taLeftJustify); Create_VLine (DetailBand1, Aleft - 13, -1, 1, 31); with create_qrdbtext (detailb and1, ALeft, 8, StringGrid1.ColWidths [Index] * Index_R, 20, StringGrid1.Font, taLeftJustify) do begin DataSet: = ADOTable1; DataField: = ADOTable1.Fields [Index] .DisplayName; end; ALeft: = ALeft StringGrid1 .ColWidths [Index] * Index_R Index_R; end; QuickRep1.Preview; end; function TForm1.ReSize (AGridWidth: Integer): Integer; beginResult: = Trunc (718 / AGridWidth); end; function TForm1.StringGrid_File (aFileName: String ): Boolean; varStrValue: String; Index: Integer; ACol, aRow: Integer; AFileValue: System.TextFile; beginStrValue: = ''; Try AssignFile (AFileValue, aFileName); ReWrite (AFileValue); strValue: =
HeaderControl1.Sections [0] .Text; For Index: = 1 to HeaderControl1.Sections.Count - 1 do StrValue: = StrValue ',' HeaderControl1.Sections [Index] .Text; Writeln (AFileValue, StrValue); StrValue: = '; For,: = 0 to stringgrid1.rowcount - 1 do begin strval: ='; strval: = stringgrid1.cells [0, arow]; for acol: = 1 to stringgrid1.colcount - 1 do begin strength: = strValue ',' StringGrid1.Cells [ACol, aRow]; end; Writeln (AFileValue, strValue); end; Finally CloseFile (AFileValue); end; end; function TForm1.LinkTextfile: Boolean; beginResult: = False; with ADOTable1 dobegin {ConnectionString: = 'Provider = Microsoft.Jet.OLEDB.4.0;' 'Data Source = D: /; Extended Properties = Text;' 'Persist Security Info = False'; TableName: = 'AAA # TXT' ; Open;} if Active then Result: = True; end; end; function TForm1.Create_QRDBText (Sender: TWinControl; ALeft, ATop, aWidth, AHight: Integer; aFont: TFont; AAlignMent: TAlignment): TQRDBText; varAQRDBText: TQRDBTex t; beginAQRDBText: = TQRDBText.Create (Nil); with AQRDBText dobegin Parent: = Sender; Left: = ALeft; Top: = ATop; Width: = AWidth; Height: = AHight; AlignMent: = AAlignMent; Font.Assign (AFont ); end; Result: = AQRDBText; end; function TForm1.Create_VLine (Sender: TWinControl; ALeft, ATop, aWidth, AHight: Integer): TQRShape; varAQRShapeV: TQRShape; beginAQRShapeV: = TQRShape.Create (Nil); with AQRShapeV dobegin PARENT: = Sender; Left: = aleft; top: = atop; width: = awidth; height: = AHIGHT; END; result: = AQRSHAPEV;