3 使用 TDBGridEh 组件 理解 TDBGridEh, TDataLink 以及 TDataSet. All below text in equal degrees pertains as to TDBGridEh component as to TDBGrid component. A TDBGridEh control lets you view and edit records in a dataset in a tabular grid format. TDBGridEh does not story data in in itself, it only show data from dataset via TDataLink object. Every database control have internal TDataLink object to interaction with dataset. You can connect TDBGridEh to dataset using DataSource property. If you already use TStringGrid component you can see that data shows in TStringGrid and in TDBGridEh very similarly, but mechanism that use to show data in TStringGrid and in TDBGridEh very different. In TStringGrid count of rows is equal of rows in array of strings while in TDBGridEh (and TDBGrid) count of rows always not more then count of visible rows and although vertical scrollbar can display thumb position regarding the count of record in dataset it take info not from grid but directly from dataset. TDataSet don't allows us to work with data as with array of data i.e. we can not quickly get value of the field from certain record, some types of dataset have not even such notion as record number (in such datasets we can only know that we inhere in the begin of dataset or in the end of its or somewhere between, in that case DBGrid shows vertical vertical scrollbar only in three positions). But to have possibility to draw several record simultaneously TDataLink object allows to have buffer of records (record buffer window) with quick readonly access. DBGrid use this possibility of datalink and set size of record buffer window equal of count of visible rows in grid. We can not control what record must be first in this buffer, DataLink itself scroll record buffer window then we navigate through the dataset and it control the scrolling of record buffer window as that the active record as always in record buffer window. It is a reason why the active record change position when users change thumb position of vertical scrollbar using mouse.
TDBGridEh和纵向滚动条 If you works with different type of dataset you can notice that for some type of dataset DBGrid show vertical scrollbar validly but for over vertical scrollbar have only three position independently of record count in dataset. To set vertical scrollbar accomodation DBGrid use RecordCount and RecNo property of DataSet component. But some dataset and even same dataset under some condition holds -1 in RecordCount and RecNo. DataSet function IsSequenced indicates whether the underlying database table uses record numbers to indicate the order of records. When IsSequenced returns True, applications can safely use the RecNo property to navigate to records in the dataset and DBGrid use RecNo property to show thumb position in vertical scrollbar. But when IsSequenced returns False DBGrid can not define current record position and show vertical scrollbar in three positions. DBGridEh component have possibility to show proportional scrollbar for no sequenced dataset. To do it need to activate SumList and create list of record bookmars. Set SumList.Active to True and SumList.VirtualRecords to True. SumList will run through dataset and create list of record bookmarks, if you use client/sever technology to access database SumList will force dataset to fetch all records, so it operation can take much time. Keep in mind that VirtualRecords will work only for full relationship bookmarks dataset, it means that DataSet.ComapreBookmark function has to return > 0 if bookmark1 > bookmark1 (i.e. record to which indicates bookmark1 have to be after record to which indicates bookmark1), = 0 if bookmark1 = bookmark1, < 0 if bookmark1 = bookmark1. TBDEDataSet in most cases support full relationship bookmarks.
在标题中显示位图 To show bitmap in titles instead of caption use TitleImages property of TDBGridEh and ImageIndex property of TColumnTitleEh.
自动用位置标识排序标题. TDBGridEh allows to show special sortmarking bitmaps (small triangles) in the right part of title cell. In order to automatically marking title by sortmarking bitmaps add dghAutoSortMarking to OptionsEh property. Add dghMultiSortMarking too in order to allow sortmarking several columns simultaneously. Set Column.Title.TitleButton to true for titles which will have possibility to change sortmarkers at run time. At runtime clicking on title will change sortmarking. Holding Ctrl key allows to mark several columns simultaneously. After user change sormarking grid call OnSortMarkingChanged event. You can write this event to change sorting and reopen in dataset. Use SortMarkedColumns property to access to sortmarked columns.
定制网格页脚 页脚以及统计值 TDBGridEh allows to show special row (footer) or rows at bottom part. Use FooterRowCount property to specify the number of footer rows in the grid. Use Footer or Footers property of TColumnEh object to specify information which need to show in footer cells. Footers property useful then you have more then one footer rows. Footers is a collection of TColumnFooterEh objects where information from i-th aliment of collection will be show in i-th cell of footer column. In footer cell, it is possible to show: Sum value for specified field, record count, value of a specified field or static text. Use property Footer.ValueType or Footers[i].ValueType to specify which type of value will be show in footer cell. If ValueType = fvtStaticText, then set the property Value to specify text which need to show. If ValueType = fvtFieldValue, then you need to set property FieldName to specify field, value of which need to show. To force grid to calculate total values need to activate SumList (DBGridEh.SumList.Active := True). Set ValueType to fvtSum and grid must to show sum value of the column field in the footer cell, you can also specify Column.Footer.FieldName to calculate total value of the other field. Set ValueType to fvtCount to force grid to show count of records in the footer cell.
定制网格数据单元 在数据单元中显示字段值为图形。 TDBGridEh allows to show bitmaps from TImageList component depending on field values. To show bitmaps depending on field values need: Fill list of field values to Column.KeyList property (every value in separate line) and set Column.ImageList property to ImageList control that has the bitmap in according index. Set Column.NotInKeyListIndex to index of bitmap that will be shown if field's value does not correspond to any value in KeyList (for instance you can set index of image for Null field value). At run time you are not allowed to edit bitmap in column cell. Use blank key and mouse click to set next value from Column.KeyList to the field; Shift-blank key and Shift-Mouse click to set previous value from Column.KeyList. Set Column.DblClickNextval to True have allows to change value on mouse double click.
检查框式的逻辑及非逻辑值 Grid automatically shows checkboxes for boolean field. To show checkboxes for non boolean fields fill first line of Column.KeyList property that corresponds to the checked state of the checkbox, second line - non checked state, and set Column.Checkboxes ptoperty to True. Line of KeyList can represent more than one value in a semicolon-delimited list of items.
你可以通过KeyList 和 PickList 属性在相关的的字段中显示其它文本。KeyList显示包含在字段的值而非PickList索引所包含的值。 Column.NotInKeyListIndex to index of text from PickList that will be shown if field value do not contain in KeyList (for instance you can set index of text for Null field value). Set Column.DblClickNextval to True to change value on mouse double click.
-【在标题中显示位图】 To show bitmap in titles instead of caption use TitleImages property of TDBGridEh and ImageIndex property of TColumnTitleEh.
-【自动用位置标识排序标题】. TDBGridEh allows to show special sortmarking bitmaps (small triangles) in the right part of title cell. In order to automatically marking title by sortmarking bitmaps add dghAutoSortMarking to OptionsEh property. Add dghMultiSortMarking too in order to allow sortmarking several columns simultaneously. Set Column.Title.TitleButton to true for titles which will have possibility to change sortmarkers at run time. At runtime clicking on title will change sortmarking. Holding Ctrl key allows to mark several columns simultaneously. After user change sormarking grid call OnSortMarkingChanged event. You can write this event to change sorting and reopen in dataset. Use SortMarkedColumns property to access to sortmarked columns.
【定制网格页脚】-- -【页脚以及统计值】 TDBGridEh allows to show special row (footer) or rows at bottom part. Use FooterRowCount property to specify the number of footer rows in the grid. Use Footer or Footers property of TColumnEh object to specify information which need to show in footer cells. Footers property useful then you have more then one footer rows. Footers is a collection of TColumnFooterEh objects where information from i-th aliment of collection will be show in i-th cell of footer column. In footer cell, it is possible to show: Sum value for specified field, record count, value of a specified field or static text. Use property Footer.ValueType or Footers[i].ValueType to specify which type of value will be show in footer cell. If ValueType = fvtStaticText, then set the property Value to specify text which need to show. If ValueType = fvtFieldValue, then you need to set property FieldName to specify field, value of which need to show. To force grid to calculate total values need to activate SumList (DBGridEh.SumList.Active := True). Set ValueType to fvtSum and grid must to show sum value of the column field in the footer cell, you can also specify Column.Footer.FieldName to calculate total value of the other field. Set ValueType to fvtCount to force grid to show count of records in the footer cell.
-【定制网格数据单元】 在数据单元中显示字段值为图形。 TDBGridEh allows to show bitmaps from TImageList component depending on field values. To show bitmaps depending on field values need: Fill list of field values to Column.KeyList property (every value in separate line) and set Column.ImageList property to ImageList control that has the bitmap in according index. Set Column.NotInKeyListIndex to index of bitmap that will be shown if field's value does not correspond to any value in KeyList (for instance you can set index of image for Null field value). At run time you are not allowed to edit bitmap in column cell. Use blank key and mouse click to set next value from Column.KeyList to the field; Shift-blank key and Shift-Mouse click to set previous value from Column.KeyList. Set Column.DblClickNextval to True have allows to change value on mouse double click.
【检查框式的逻辑及非逻辑值】 Grid automatically shows checkboxes for boolean field. To show checkboxes for non boolean fields fill first line of Column.KeyList property that corresponds to the checked state of the checkbox, second line - non checked state, and set Column.Checkboxes ptoperty to True. Line of KeyList can represent more than one value in a semicolon-delimited list of items.
-【显示下拉方式的列】 你可以通过KeyList 和 PickList 属性在相关的的字段中显示其它文本。KeyList显示包含在字段的值而非PickList索引所包含的值。 Column.NotInKeyListIndex to index of text from PickList that will be shown if field value do not contain in KeyList (for instance you can set index of text for Null field value). Set Column.DblClickNextval to True to change value on mouse double click.
【注意】:可以按照下面的提示来转转换已存在的TDBGrid组件到TDBGridEh: 在Delphi的IDE中打开包含有TDBGrid组件的应用程序。 设置视图方式为文本方式(Alt-F12)。 if key=VK_RETURN then //Key:回车 begin if (grdDetailData.SelectedIndex=grdDetailData.Columns.Count-1) and not (dgRowSelect in grdDetailData.Options) then BEGIN if (dgEditing in grdDetailData.Options) and (grdDetailData.Row=grdDetailData.RowCount-1) then //从表GRID能进行编辑和当前光标焦点在最后一行时 acAddDetailExecute(self) else begin grdDetailData.Row:=grdDetailData.Row+1; //改变光标焦点行到下一行(当为新增加时,这句不用运行 grdDetailData.SelectedIndex:=0 //改变光标焦点列到第一列 end; END ELSE BEGIN grdDetailData.SelectedIndex:=grdDetailData.SelectedIndex+1; while grdDetailData.Columns[grdDetailData.SelectedIndex].ReadOnly do grdDetailData.SelectedIndex:=grdDetailData.SelectedIndex+1; END; key:=0; end; if key=VK_INSERT then //Insert键:新增一条记录 begin acAddDetailExecute(self); key:=0; end; if key=VK_DELETE then //Delte: 删除一条记录 begin acDeleteDetailExecute(self); key:=0; end;
procedure TInvoiceManager.ppmSaveSelectionClick(Sender: TObject); var ExpClass:TDBGridEhExportClass; Ext:String; begin SaveDialog1.FileName := 'file1'; if (ActiveControl is TDBGridEh) then if SaveDialog1.Execute then begin case SaveDialog1.FilterIndex of 1: begin ExpClass := TDBGridEhExportAsText; Ext := 'txt'; end; 2: begin ExpClass := TDBGridEhExportAsCSV; Ext := 'csv'; end; 3: begin ExpClass := TDBGridEhExportAsHTML; Ext := 'htm'; end; 4: begin ExpClass := TDBGridEhExportAsRTF; Ext := 'rtf'; end; 5: begin ExpClass := TDBGridEhExportAsXLS; Ext := 'xls'; end; else ExpClass := nil; Ext := ''; end; if ExpClass <> nil then begin if UpperCase(Copy(SaveDialog1.FileName,Length(SaveDialog1.FileName)-2,3)) <> UpperCase(Ext) then SaveDialog1.FileName := SaveDialog1.FileName + '.' + Ext; SaveDBGridEhToExportFile(ExpClass,TDBGridEh(ActiveControl), SaveDialog1.FileName,False); end; end; end;
2003-12-10 14:55:16 在dbgrideh中允许选择多行,如何知道哪些行被选中?是个BOOKMARK类型的属性。 SelectedRows: TBookmarkList procedure TForm1.Button1Click(Sender: TObject); var i, j: Integer; s: string; begin if DBGrid1.SelectedRows.Count>0 then with DBGrid1.DataSource.DataSet do for i:=0 to DBGrid1.SelectedRows.Count-1 do begin GotoBookmark(pointer(DBGrid1.SelectedRows.Items[i])); for j := 0 to FieldCount-1 do begin if (j>0) then s:=s+', '; s:=s+Fields[j].AsString; end; Listbox1.Items.Add(s); s:= ''; end; end;
procedure TForm1.Button1Click(Sender: TObject); var i, j: Integer; s: string; begin if DBGrid1.SelectedRows.Count>0 then with DBGrid1.DataSource.DataSet do for i:=0 to DBGrid1.SelectedRows.Count-1 do begin GotoBookmark(pointer(DBGrid1.SelectedRows.Items[i])); for j := 0 to FieldCount-1 do begin
if (j>0) then s:=s+', '; s:=s+Fields[j].AsString; end; Listbox1.Items.Add(s); s:= ''; end; end;
2004-2-11 12:45:10 在dbgrideh中直接点击title就可按点击的那个字段排序的方法第一种方法(未测试) procedure TForm1.DBGridEh1TitleClick(Column: TColumnEh); begin //点击GridEh标题排序 if (Column.Title.SortMarker = smNoneEh) or (Column.Title.SortMarker = smDownEh) then begin ADOQuery1.SORT := COLUMN.FIELDNAME; Column.Title.SortMarker := smUpEh end else begin ADOQuery1.SORT := COLUMN.FIELDNAME + ' DESC'; Column.Title.SortMarker := smDownEh end; end;
第二种方法(未测试) procedure TPrintMai_frm.DBGridEh1TitleClick(Column: TColumnEh); var sortstring: string; begin //进行排序 with Column do begin if FieldName = '' then Exit; case Title.SortMarker of smNoneEh: begin Title.SortMarker := smDownEh; sortstring := Column.FieldName + ' ASC'; end; smDownEh: sortstring := Column.FieldName + ' ASC'; smUpEh: sortstring := Column.FieldName + ' DESC'; end; //数据集排序。 try DM.DataModule1.qry2.Sort := sortstring //dataset为实际数据集变量名 except end; end; end;
很多说根据条件在数据网格中显示不同颜色的说法都是在DrawColumnCell事件里,这样显示没有问题,但在ehlib中如果想打印出来可就不行了。 ehlib提供了GetCellParams事件,可以显示和打印 //只能显示,不能打印 procedure TfrmQueryContractInfo.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumnEh; State: TGridDrawState); var OldColor,OldColumnColor:TColor; v:TColCellParamsEh; begin { OldColor:=DBGrid1.Canvas.Font.Color ; if (Column.Field.FieldName = '填制调单时间') then if (DBGrid1.DataSource.DataSet.FieldByName('填制调单时间').AsDateTime >DBGrid1.DataSource.DataSet.FieldByName('应发货时间').AsDateTime) then begin //v:=TColCellParamsEh.Create ; //Column.GetColCellParams(true,v); //v.Font.Color :=clRed; //Column.FillColCellParams(v); //v.Free; DBGrid1.Canvas.font.Color := clRed; end;
//Column.GetColCellParams .FillColCellParams()
if (Column.Field.FieldName = '移交时间') then if (DBGrid1.DataSource.DataSet.FieldByName('移交时间').AsDateTime >DBGrid1.DataSource.DataSet.FieldByName('应移交时间').AsDateTime) then DBGrid1.Canvas.Font.Color := clRed;
if (Column.Field.FieldName = '填制运输单时间') then if (DBGrid1.DataSource.DataSet.FieldByName('填制运输单时间').AsDateTime >DBGrid1.DataSource.DataSet.FieldByName('填制调单时间').AsDateTime) then DBGrid1.Canvas.Font.Color := clRed;
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State); DBGrid1.Canvas.Font.Color:=OldColor; } end; //可以显示,可以打印 procedure TfrmQueryContractInfo.DBGrid1GetCellParams(Sender: TObject; Column: TColumnEh; AFont: TFont; var Background: TColor; State: TGridDrawState); var OldColor,OldColumnColor:TColor; v:TColCellParamsEh; begin //OldColor:=DBGrid1.Canvas.Font.Color ; if (Column.Field.FieldName = '填制调单时间') then if (DBGrid1.DataSource.DataSet.FieldByName('填制调单时间').AsDateTime >DBGrid1.DataSource.DataSet.FieldByName('应发货时间').AsDateTime) then AFont.Color := clRed;
if (Column.Field.FieldName = '移交时间') then if (DBGrid1.DataSource.DataSet.FieldByName('移交时间').AsDateTime >DBGrid1.DataSource.DataSet.FieldByName('应移交时间').AsDateTime) then AFont.Color := clRed;
if (Column.Field.FieldName = '填制运输单时间') then if (DBGrid1.DataSource.DataSet.FieldByName('填制运输单时间').AsDateTime >DBGrid1.DataSource.DataSet.FieldByName('填制调单时间').AsDateTime) then AFont.Color := clRed;
if (Column.Field.FieldName = '实际到货时间') then if (DBGrid1.DataSource.DataSet.FieldByName('实际到货时间').AsDateTime >DBGrid1.DataSource.DataSet.FieldByName('要求到货时间').AsDateTime) then AFont.Color := clRed; //DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State); //DBGrid1.Canvas.Font.Color:=OldColor; end;
implementation procedure OptimizeGrid(AGrid: TCustomDbGridEh); var i: integer; begin // 優化GRID的寬度 for i := 0 to TDBGridEh(AGrid).Columns.count - 1 do THackDBGridEH(AGrid).OptimizeSelectedColsWidth(TDBGridEh(AGrid).Columns[i]); end;
2005-5-26 18:02:12 点击DBGridEh标题实现排序KeyLife富翁笔记 作者: tempmen 标题: 点击GridEh标题排序 关键字: 分类: 个人专区 密级: 公开 (评分: , 回复: 0, 阅读: 145) »» 1。 if (Column.Title.SortMarker = smNoneEh) or (Column.Title.SortMarker = smDownEh) then begin ADOQuery1.SORT := COLUMN.FIELDNAME; Column.Title.SortMarker := smUpEh end else begin ADOQuery1.SORT := COLUMN.FIELDNAME + ' DESC'; Column.Title.SortMarker := smDownEh end; 2。 以下方法用来实现 点击DBGrid列 按该列正序排序,再次点击 逆序 procedure TFormMain.MyDBGridTitleClick(Column: TColumn); var i:integer; begin if AdoQCX.Sort = Column.FieldName then //AdoQCX为MyDBGrid绑定的Tadoquery begin For i:=0 to MyDBGrid.Columns.Count-1 do MyDBGrid.Columns[i].Title.RestoreDefaults;
AdoQCX.Sort := Column.FieldName + ' DESC'; Column.Title.Font.Color:=clPurple; //用紫色表示 逆序 字段 end else begin For i:=0 to MyDBGrid.Columns.Count-1 do MyDBGrid.Columns[i].Title.RestoreDefaults;
3。 procedure TForm_main.DBGrid_topTitleClick(Column: TColumn); var lInteger_count,lInteger_Length:integer; lString_All,lString_Last:string; begin for lInteger_count := 0 to dbgrid_top.FieldCount - 1 do begin lInteger_Length := length(dbgrid_top.columns[lInteger_count].Title.Caption); lString_Last := copy(dbgrid_top.columns[lInteger_count].Title.Caption,lInteger_Length-2,3); lString_All := copy(dbgrid_top.columns[lInteger_count].Title.Caption,1,lInteger_Length-3); if dbgrid_top.columns[lInteger_count].FieldName = column.FieldName then begin if (lString_Last = ' ▼') then begin adoquery_main.Sort := column.FieldName + ' ASC'; dbgrid_top.columns[lInteger_count].Title.Caption := lString_All + ' ▲'; end else if (lString_Last = ' ▲') then begin adoquery_main.Sort := column.FieldName + ' DESC'; dbgrid_top.columns[lInteger_count].Title.Caption := lString_All + ' ▼'; end else begin adoquery_main.Sort := column.FieldName + ' ASC'; dbgrid_top.columns[lInteger_count].Title.Caption := lString_All + lString_Last + ' ▲'; end; end else begin if ((lString_Last = ' ▼') or (lString_Last = ' ▲')) then dbgrid_top.columns[lInteger_count].Title.Caption := lString_All; end; end; end;
4、显示日历下拉列表 Date 和 DateTime类型字段值均可以此形式显示。外观与编辑框无异,当点击该单元格时,右侧会出现“▽”符号,点击之即可出现日历下拉列表。有时不希望出现日历下拉列表,只需设置Column.ButtonStyle属性为 cbsNone即可,此方法同样适用于其它组件不以特殊外观显示的情况。
5、3D或平面外观效果 设置OptionsEh属性 中fixed, frozen, footer 和 data rows等属性表格外观为3D效果,设置flat为true则为平面外观效果.
procedure TForm1.B_expClick(Sender: TObject); var ExpClass:TDBGridEhExportClass; Ext:String; begin SaveDialog1.FileName := 'file1'; if (ActiveControl is TDBGridEh) then if SaveDialog1.Execute then begin case SaveDialog1.FilterIndex of 1: begin ExpClass := TDBGridEhExportAsText; Ext := 'txt'; end; 2: begin ExpClass := TDBGridEhExportAsCSV; Ext := 'csv'; end; 3: begin ExpClass := TDBGridEhExportAsHTML; Ext := 'htm'; end; 4: begin ExpClass := TDBGridEhExportAsRTF; Ext := 'rtf'; end; 5: begin ExpClass := TDBGridEhExportAsXLS; Ext := 'xls'; end; else ExpClass := nil; Ext := ''; end; if ExpClass <> nil then begin if UpperCase(Copy(SaveDialog1.FileName,Length(SaveDialog1.FileName)-2,3)) <> UpperCase(Ext) then SaveDialog1.FileName := SaveDialog1.FileName + '.' + Ext; SaveDBGridEhToExportFile(ExpClass,DBGridEh1,SaveDialog1.FileName,False); //其中false为局部数据 end; end; end;