Detailed explanation of Delphi CxGrid usage

1. Remove the header box in cxgrid, groupbybox=false in optionsview of tableview1;


2. Add sum (column) to the GRID footer, footer=ture of the optionsview of tableview1

Then footer.add of summary in customize of cxGRid1 needs to aggregate the column kind= skSum


3. Remove the filter drop-down arrow in cxgrid and select tableview1.optionscustomize.columnfiltering=fasle;


4. In the OptionView of cxGrid1DBTableView1, set the property GroupFooters to gfAlwaysVisible and set the columns to be summed. Add the fields that need to be summed under summary.defaultfor Groups. The fieldname displayed under the column is the statistical field, format is the format, kind is the statistical method, and position For the position spfooter below the group, spgroup above the group
or use cxGridPopupMenu1, you can sum any number type column at runtime, the method is to simply set the property Grid of cxGridPopupMenu1 to cxGrid of cxGrid1DBTableView1,

to achieve what you need


5. The default value format of cxGrid in DEVExpress has ¥, how to remove it!

  Double-click Cxgrid, select colmnum, change the type options.properties.Memo, and set it in displayformat


6. Enter the last box of the last row of the grid to automatically add a record and jump to the first box of the new record

Set FocusFirsCellOnNewRecord and GoToNextCellOnEnter of OptionsBehavior of cxGrid1DBTableView1 to True, and FocusCellOnCycle to True


7. Jump through Tab in the grid and set the FocusCellOnTab of the OptionsBehavior of cxGrid1DBTableView1 to True


8. To set whether the data can be edited in the grid, set the Appending of the OptionsData of cxGrid1DBTableView1 to True, Delete to True, and Editing to True


9. To set whether the grid can be selected, set the CellSelect of the OptionsSelection of cxGrid1DBTableView1 to True


10. To set whether the selection can be reversed, set the InsertSelect of the OptionsSelection of cxGrid1DBTableView1 to True


11. Set the row height: in cxGridDBTableView1.OptionsView.DataRowHeight


12. Change the font
  Double click it in cxStyleRepository, add GridTableViewStyleSheetDevExpress inside it

 Then change the font of cxStyle in the Style tag to control the font of cxGrid


13. Example with pictures and MEMO:
   drag in a cxGrid1, Table1, DataSource1. The DatabaseName of Table1 is set to DBDEMOS, the TableName is set to biolife.db,
Active is set to True; the DataSet of DataSource1 is set to Table1; the DataSource in the DataController of cxGrid1DBTableView1

  Set as DataSource1; right-click cxGrid1DBTableView1 and select Create AllColumns; double-click cxGrid1, find in the pop-up window  

For cxGrid1DBTableView1Notes and cxGrid1DBTableView1Graphic, set their Properties to BlobEdit; run them to see the results. Then set the Properties property of cxGrid1DBTableView1Graphic to Image, then set Stretch under Properties to True, set cxGrid1DBTableView1->optionsview->CellAutoHeight to True, and see the result.


14. How to make "Drag a column here to group by that column" not displayed
   Solution: Click on cxGrid1DBTableView1 on cxGrid1

   In cxGrid1DBTableView1->optionsview->groupbybox:=false


15. How can the English [Drag a column header to group by that column] on the GroupPanel be changed to Chinese?
   Solution: The easiest way is TcxGridTableView.OnCustomDrawPartBackground, you can also use OnCustomDrawGroupCell:
   procedureTForm1.cxGrid1DBTableView1CustomDrawPartBackground(
              Sender: TcxGridTableView; ACanvas: TcxCanvas;
             AViewInfo: TcxCustomGridCellViewInfo; var ADone:Boolean);
   begin
     AViewInfo.Text:=' to dynamically set the display content of the GroupBox ';
     ACanvas.FillRect(AViewInfo.Bounds);

   end;


16. How to realize the following functions:
          Finance Department
          Raw Material Warehouse
          Finished Product Warehouse
          Stamping Workshop
          Software Development Department
   This is the name of the department, click the plus sign to display the personnel situation of this department.
   Solution: In fact, this is a master-slave table relationship,
 (1): Fill in the keyfieldnames of the master table
 (2): Fill in the keyfieldnames of the slave table
 (3): Fill in the detaikeyfieldNames and masterkeyfieldnames of the slave table
 (4): Fill in the detaikeyfieldNames and masterkeyfieldnames of the slave table The data source must be sorted by the fields associated with the main table

     Note: If the master-slave table structure is set in other places, it will not be displayed, for example, the mastersource and

  asterfield will not be able to display data! If it is a master-slave relationship between two cxGrids, this setting is OK.


17. Similar to PageControl display

  Solution: Add a Level, set cxGrid1->RootLevelOptions->DetailTabsPosition to dtpTop, and then set the Caption values ​​of cxGrid1Level1 and cxGrid1Level2 accordingly. 


18. How to set the columns on the left without scrolling?
   Solution: Only DB BandedTable can be used
        . Create Band0, Band1
        Band0 Fixed=tfLeft
        Band1 Fixed=tfnone in cxGrid1DBBandedTableView

        Set BandIndex=0 of the field to be locked, and the others are 1, and it is OK.


19. How to achieve the same function as EXCEL, the current grid=G14 G15 G16 This function is
  solved: give a simple example: label1.Caption :=cxGrid1DBTableView1.DataController.Values[2,
3] cxGrid1DBTableView2.DataController.Values[1, 1 ]cxGrid1DBTableView3.DataController.Values[1, 1];

Therefore, the data in different cxGrid1DBTableView can be given to the current grid, so that the current grid in EXCEL = G14 G15 G16 similar functions.


20. What is the use and how to use EditLayout in the menu of cxGrid1DBBandedTableView1 by right-clicking the mouse? Solution: You can drag the fields, and the parallel ones can be dragged to have a sense of hierarchy (layer by layer), and arrows will be displayed when dragging, that is to say, you can drag a field to put

Going to the top allows records to be grouped by this field. Click on one of the fields, there will also be a small rising or falling triangle on it, this

The role of the small triangle is that in the running phase, the data will be sorted ascending or descending according to this field.

   There is also a function of Set asDefault to keep the parameters of the current TableView. When a new TableView is generated, it will be the same as the parameters kept last time. This has not been tested yet.


21. How to import the data in cxGrid into EXCEL, HTML, XML and TEXT
  solution: This problem becomes very simple after using cxGrid,
uses
 cxExportGrid4Link;

procedure TForm1.Button1Click(Sender: TObject);
begin
 ExportGrid4ToEXCEL('d:\wang.xsl',cxGrid1,True,True);
 ExportGrid4ToTEXT('d:\wang.txt',cxGrid1,True,True);
 ExportGrid4ToXML('d:\wang.xml',cxGrid1,True,True);
 ExportGrid4ToHTML('d:\wang.html',cxGrid1,True,True);

end;


22. 如何使满足条件的数据显示不同的颜色?
解决:
var
AYellowStyle: TcxStyle;

procedure TForm1.FormCreate(Sender: TObject);
begin
  //行颜色
  AYellowStyle :=TcxStyle.Create(Self);
  AYellowStyle.Color := $0080FFFF;
  AYellowStyle.TextColor := clMaroon;
end;

procedureTForm1.cxGrid1DBBandedTableView1StylesGetContentStyle(
Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
begin
if ARecord.Values[cxGrid1DBBandedTableView1Lengthcm.Index]< 81 then
  AStyle := AYellowStyle;
end;

这里cxGrid1DBBandedTableView1Lengthcm.Index小于81时就显示黄色


23. 如何从外边的TXT文件导入到cxGrid?
解决:   procedure CustomizeColumns;
         procedure LoadData;

procedure TForm1.CustomizeColumns;
const
 cDistance = 1;
 cRadius = 5;
 cPeriod = 4;
 cPstring = 0;
var
 I: Integer;
begin
 DecimalSeparator := '.';
 with cxGrid1TableView2 do
 for I := 0 to ColumnCount - 1 do
   if I in[cDistance, cRadius] then
    Columns[I].DataBinding.ValueTypeClass :=TcxIntegerValueType//1,5列为Integer
   else
    if I in [cPstring,cPeriod] then
    Columns[I].DataBinding.ValueTypeClass :=TcxStringValueType//0,4列为String
    else
     Columns[I].DataBinding.ValueTypeClass :=TcxFloatValueType;//其他为Float
end;

procedure TForm1.LoadData;
const
 AFileName = '资产负债表.txt';
 AHeaderLineCount = 2;

var
 ARecords, AValues: TStringList;
 I: Integer;

 procedure InitRecord(const Str:string);
 var
   J:Integer;
   V:Variant;
 begin
  AValues.CommaText := Str;
   for J := 0 toAValues.Count - 1 do
   if AValues.Strings[J] <> '-'then
   begin
    V := AValues.Strings[J];
    if not VarIsNull(V) then
      cxGrid1TableView2.DataController.Values[I, J] := V;
   end;
 end;

begin
 if not FileExists(AFileName) then
   raiseException.Create('Data file not found');

 ARecords := TStringList.Create;
 AValues := TStringList.Create;

 with ARecords do
 try
  LoadFromFile(AFileName);
  cxGrid1TableView2.BeginUpdate;
  cxGrid1TableView2.DataController.RecordCount := Count -AHeaderLineCount;
   for I := 0 toCount - (AHeaderLineCount 1) do
    InitRecord(Strings[I AHeaderLineCount]);
 finally
  cxGrid1TableView2.EndUpdate;
  ARecords.Free;
  AValues.Free;
 end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 CustomizeColumns;
 LoadData_Zcfz;

end;


24. 如何改变列的颜色?
var
 AFirstColumnStyle: TcxStyle;

procedure TForm1.FormCreate(Sender: TObject);
begin
 //列颜色
 AFirstColumnStyle :=TcxStyle.Create(Self);
 AFirstColumnStyle.Color := clAqua;
 AFirstColumnStyle.TextColor :=clBlue;
 cxGrid1TableView1.Columns[1].Styles.Content:= AFirstColumnStyle;

end;


25.Set as default的用法?
 解决:Set asdefault的用法是为了解决设置参数的方便而做的,比如:
      连好数据库以后,更改cxGrid1DBBandedTableView1->OptionsCustomize->ColumnFiltering设为False。(这个设置可以将字段名的下拉单给去掉)更改cxGrid1DBBandedTableView1->OptionsView->Navigator设置为True。然后右击cxGrid1DBBandedTableView1,在弹出的菜单栏里面点击Set asdefault。

 OK,下次你再产生一个新的cxGrid1DBBandedTableView1时这些设置和刚才的一样了。如果需要设置的参数很多的时候,这个Setas default很有用!


 
26. 怎样使鼠标移动时,相应的单元里的文字变色?
  解决:
var
 FTrackItem: TcxCustomGridTableItem;
 FTrackRec: TcxCustomGridRecord;

procedure TForm1.cxGrid1DBTableView1CustomDrawCell(
 Sender: TcxCustomGridTableView; ACanvas:TcxCanvas;
 AViewInfo:TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
 if (AViewInfo.GridRecord = FTrackRec) and(AViewInfo.Item = FTrackItem) then
 begin
  ACanvas.Font.Color :=clred;  //红色字体
  ACanvas.Font.Style := [fsUnderline];//带下划线
 end;
end;

procedure TForm1.cxGrid1DBTableView1MouseMove(Sender:TObject;
 Shift: TShiftState; X, Y: Integer);
var
 AHitTest: TcxCustomGridHitTest;
 ATrackItem: TcxCustomGridTableItem;
 ATrackRec: TcxCustomGridRecord;
begin
 ATrackItem := FTrackItem;
 ATrackRec := FTrackRec;

 AHitTest := (Sender asTcxGridSite).GridView.ViewInfo.GetHitTest(X, Y);
 if AHitTest isTcxGridRecordCellHitTest then
 begin
   FTrackItem :=TcxGridRecordCellHitTest(AHitTest).Item;
   FTrackRec :=TcxGridRecordCellHitTest(AHitTest).GridRecord;
 end
 else
 begin
   FTrackItem :=nil;
   FTrackRec :=nil;
 end;

 if (ATrackItem<> FTrackItem) or (ATrackRec<> FTrackRec) then
 begin
   // Invalidateold cell
   if ATrackRec<> nil then
    ATrackRec.Invalidate(ATrackItem);
   // Invalidatenew cell
   if FTrackRec<> nil then
    FTrackRec.Invalidate(FTrackItem);
 end;

end;


27. 怎样设计多表头的cxGrid?
  解决:cxGrid可以解决如下的表头:
                  ---------------------------------
                  |    说明1    |    说明2    |
                  ---------------------------------
                  | 字段1 | 字段2 | 字段3 | 字段4 |

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325960244&siteId=291194637