天天看點

TAdvStringGrid用法

TAdvStringGrid(一)

******************************

技巧一

本文全部摘自網絡,出處很多,感謝原創作者及其它引用者。

文章介紹了TAdvStringGrid控件的一些常用的屬性事件和方法,主要用于統計資料和設計比較複雜的表格!核心部分:合并單元格的幾個函數和過程!

  1.可否進行編輯

  設定Options中goEditing設定為true。

  2.點選滑鼠時直接進入編輯,或允許、不允許改變行或列寬等特性,都可以在MouseActions中設定。

  3.設定Grid中,某一列的資料格式,預定義的格式有:

  enum TEditorType { edNormal, edSpinEdit, edComboEdit, edComboList, edEditBtn, edCheckBox, edDateEdit, edDateEditUpDown, edTimeEdit, edButton, edDataCheckBox, edNumeric, edPositiveNumeric, edFloat, edCapital, edMixedCase, edPassword, edUnitEditBtn, edLowerCase, edUpperCase, edFloatSpinEdit, edTimeSpinEdit, edDateSpinEdit, edNumericEditBtn, edFloatEditBtn, edCustom, edRichEdit, edNone, edUniEdit, edUniComboEdit, edUniComboList };

  通過OnGetEditorType()事件進行設定,例如:

  void __fastcall TForm1::editgridGetEditorType(TObject *Sender,int aCol, int aRow, TEditorType &aEditor)

{

  switch (aCol) {

    case 1:aEditor=edComboList; break;  //設定第一列為ComboBox類型輸入

    case 2:aEditor=edEditBtn; break;  //設定第二列為EditBtn類型輸入,彈出一個選擇框

    case 3:aEditor=edSpinEdit; break;  //設定第三列為SpinEdit類型輸入

    case 4:aEditor=edDateEdit; break;  //設定第四列為日期型輸入,彈出DatetimePicker。

  }

}

  4.TAdvStringGrid中每一種格式的輸入與輸出都要對應,否則載入時不能識别(因為各種類型的檔案之間存儲格式是不一樣的)。例如:

  如果儲存時使用TAdvStringGrid::SaveToFile(),那麼載入時就要相應的使用TAdvStringGrid::LoadFromFile();其它對應如下:

TAdvStringGrid:: LoadFromBinFile();??TAdvStringGrid:: SaveToBinFile();

TAdvStringGrid:: LoadFromCSV ();??TAdvStringGrid:: SaveToCSV();

等等。

  5.增加edComboEdit和edComboList的下拉清單:

方法:

TAdvStringGrid:: ClearComboString(void); //清除已有了下拉清單

TAdvStringGrid:: AddComboString(AnsiString S);

TAdvStringGrid::Combobox->Sorted = true;

通過Combobox可以通路内置的Combo類型的控件的屬性和方法。

  6.增加edCheckBox界面處理

  在Grid中資料載入之後,使用:

TAdvStringGrid:: void __fastcall AddCheckBox(int ACol, int ARow, bool State, bool Data)方法來添加CheckBox界面處理.

例如:

for(i=1;i<editgrid->RowCount;i++)

editgrid->AddCheckBox(5,i,FALSE,FALSE);

 

  7.通過TAdvStringGrid:: SpinEdit對象,可以通路内置的SpinEdit類的特性;

  同樣的道理,通過其它一些内置的對象,可以通路這些類的屬性,執行這些類的方法。

  8.TAdvStringGrid中的排序問題:

  首先設定排序參數:通過TAdvStringGrid:: SortSettings屬性(釋出TSortSettings類為一個屬性),通過這個屬性設定

然後調用TAdvStringGrid::QSort()方法應用SortSettings。

排序方向: SortSettings-> Direction=enum {sdAscending, sdDescending};

排序列:SortSettings->Column = 3;

  9.TAdvStringGrid::AutoSize屬性,自動設定列寬,不推薦使用,自動調整後,真的很難看。

  10.多行顯示

  bool TAdvStringGrid:: Multilinecells屬性,是否支援多行顯示,如果支援的話,可以用”#13”作為分隔符插入多行字元串,用多行顯示。

折中的方法,可以用WordWrap來支援多行顯示

  一般在設定和取消多行顯示時,同時,對TAdvStringGrid::DefaultRowHeight進行設定,将有良好的效果。

  11.欲設定某些行或列為Fixed風格的顯示,使用OnIsFixedCell()事件句柄來處理,調用TAdvStringGrid:: Repaint()方法來使設定生效。

例如:

If(colfix->Checked) { //colfix為一個TCheckBox

AdvStringGrid1->Repaint();

}

//事件處理句柄

void __fastcall TForm1::AdvStringGrid1IsFixedCell(TObject *Sender,

int Arow, int Acol, bool &isfixed)

{

if ((Acol==3) && (colfix->Checked))

isfixed=TRUE; else isfixed=FALSE;

}

  12.處理顯示風格,如添加貨币字首和實體量機關

  在TAdvStringGrid::OnGetFormat()事件處理句柄中處理。

aPrefix參數設定顯示字首;

aSuffix參數設定顯示字尾;

AStyle參數enum TSortStyle { ssAutomatic, ssAlphabetic, ssNumeric, ssDate, ssAlphaNoCase, ssAlphaCase, ssShortDateEU, ssShortDateUS, ssCustom, ssFinancial, ssAnsiAlphaCase, ssAnsiAlphaNoCase, ssRaw, ssHTML, ssImages, ssCheckBox, ssUnicode };設定排序風格;

  13.是否能對特定的單元格進行編輯

用TAdvStringGrid::OnCanEit()事件句柄來處理,

設定bool &canedit參數來達到能否編輯某些單元格的效果

  14.隐藏和顯示某些單元格

TAdvStringGrid:: UnHideColumn(int ACol)

TAdvStringGrid::HideColumn(int ACol);

  15.查找的實作:

TAdvStringGrid::Find()

TAdvStringGrid::FindNext();

兩個方法傳回TPoint類型,

Find()需要一個TFindParams類型的參數,enum TFindParameters { fnMatchCase, fnMatchFull, fnMatchRegular, fnDirectionLeftRight, fnMatchStart, fnFindInCurrentRow, fnFindInCurrentCol, fnIncludeFixed, fnAutoGoto, fnIgnoreHTMLTags, fnBackward, fnIncludeHiddenColumns };可通過自己設計一個查找窗體來實作。

例如:

//----------findfirst----------

void __fastcall TForm1::Findfirst1Click(TObject *Sender)

{

TFind *Find;

TFindParams findparams;

TPoint res;

Find = new TFind(Form1);

if (Find->ShowModal()==mrOk)

{

if (Find->chkcase->Checked) findparams << fnMatchCase;

if (Find->chkfull->Checked) findparams << fnMatchFull;

if (Find->chkregular->Checked) findparams << fnMatchRegular;

if (Find->dir->ItemIndex==1) findparams << fnDirectionLeftRight;

if (Find->where->ItemIndex==1) findparams << fnFindInCurrentCol;

if (Find->where->ItemIndex==2) findparams << fnFindInCurrentRow;

res = AdvStringGrid1->FindFirst(Find->findtext->Text,findparams);

if (res.x>=0)

{

AdvStringGrid1->Col=res.x;

AdvStringGrid1->Row=res.y;

}

else

ShowMessage("Text not found");

}

delete Find;

}

//----------findnext----------

void __fastcall TForm1::Findnext1Click(TObject *Sender)

{

TPoint res;

res = AdvStringGrid1->FindNext();

if ((res.x>=0) && (res.y>=0))

{

AdvStringGrid1->Col=res.x;

AdvStringGrid1->Row=res.y;

}

else

ShowMessage("Text not found");

}

//----------

  16.TAdvStringGrid:: Ints[int ACol][int ARow]以Integer類型的值來通路單元格,如果讀取失敗,會觸發異常.

  17.對特定單元格的對齊方式進行處理

  添加TAdvStringGrid::OnGetAlignment事件處理句柄。

  18.

  (1).添加圖示:TAdvStringGrid::AddIcon();

原型:void __fastcall AddIcon(int ACol, int ARow, Graphics::TIcon *aicon, TCellHAlign hal, TCellVAlign val)

  (2).添加旋轉字型:TAdvStringGrid:: AddRotated;

原型:void __fastcall AddRotated(int ACol, int ARow, short AAngle, AnsiString s);

  (3).從ImageList中取出圖象添加到單元格中:TAdvStringGrid:: AddImageIdx;

原型:void __fastcall AddImageIdx(int ACol, int ARow, int Aidx, TCellHAlign hal, TCellVAlign val);

參數Aidx為圖檔在ImageList中的索引.

  (4).向單元格中添加位圖:TAdvStringGrid:: AddBitmap;

原型:void __fastcall AddBitmap(int ACol, int ARow, Graphics::TBitmap *ABmp, bool Transparent, TCellHAlign hal, TCellVAlign val);

參數bool Transparent設定是否顯示透明.

  (5).自動添加編号:AutoNumberCol

TAdvStringGrid:: AutoNumberCol(int ACol)方法,對指定的列從1進行編号,不對Fixed行中的列編号;

用途:用于産生首列的自動編号。

  (6).向單元格中添加多個圖象:TAdvStringGrid::AddMultiImage

原型:void __fastcall AddMultiImage(int ACol, int ARow, int Dir, TCellHAlign

hal, TCellVAlign val);

事實上隻是向系統聲明一下,這個單元格将放置多個圖象,圖象的添加通過GridImages來添加。例如:

AdvStringGrid2->AddMultiImage(5,1,0,haBeforeText,vaCenter);

AdvStringGrid2->CellImages[5][1]->Add(0);

AdvStringGrid2->CellImages[5][1]->Add(1);

  (7).如果TAdvStringGrid:: EnableHTML屬性啟用,那麼,單元格中輸入HTML源代碼,它将以HTML格式顯示出來,例如:

AdvStringGrid2->Cells[7][1]="Easy HTML

formatting";

AdvStringGrid2->Cells[7][2]="Including <IMG src=" "\x022" "idx:0" "\x022" ">

AdvStringGrid2->Cells[7][3]="Enjoy

----------

miniHTML";

AdvStringGrid2->Cells[7][4]="

AdvStringGrid2->Cells[7][5]="125 <SUP>9</SUP>/<SUB>16</SUB>";

這些内容在TAdvStringGrid中将以HTML格式顯示出來。

  (8).通過TAdvStringGrid:: RichEdit屬性可以調用TAdvStringGrid的内置的RichEdit類,是一個RichEdit編輯器,可以使用它處理RTF格式文檔,然後使用TAdvStringGrid::RichToCell方法寫入單元格.

原型:void __fastcall RichToCell(int Col, int Row, Comctrls::TRichEdit *Richeditor);

可以把用TAdvStringGrid::RichEdit::Clear()把RichEdit清空,再重新添加内容,然後再添加到單元格中。

可以通過TAdvStringGrid::RichEdit::SelAttributes來調整RichEdit的格式。

  (9).通過TAdvStringGrid:: AddProgress方法來在單元格中添加進度條

原型:void __fastcall AddProgress(int ACol, int ARow, Graphics::TColor FGColor, Graphics::TColor BKColor);

單元格的整數值就是目前進度值,可以使用TAdvStringGrid::Ints[col,row]來通路這個值。

  (10).通過TAdvStringGrid::AddComment方法來添加注釋。

原型:void __fastcall AddComment(int ACol, int ARow, AnsiString comment);

添加注釋之後,會在單元格的右上角顯示一個小的紅色的三角号,例如:

AdvStringGrid2->AddComment(8,3,"This is a custom" "\x00D" "comment for this cell");

AdvStringGrid2->Cells[8][3]="Cell with

comment";

  (11).通過TAdvStringGrid增強型的HTML文法解析,添加内部單元格之間的連結,例如:

AdvStringGrid2->Cells[8][4]=" AdvStringGrid2->Cells[8][5]=" (12).通過TAdvStringGrid:: AddButton來在單元格中添加按鈕

原型:void __fastcall AddButton(int ACol, int ARow, int bw, int bh, AnsiString Caption, TCellHAlign hal, TCellVAlign val);

當添加的按鈕被按下時,OnButtonClick事件被觸發(?經測試,不能觸該事件)。

  19.在TAdvStringGrid::OnGetCellsColor()事件處理句柄中,可以對單元格的顔色和字型進行設定.

  21.添加可收縮結點,外觀上看起來類似于分組,

方法:

TAdvStringGrid::FixedCols=0;

TAdvStringGrid::FixedColWidth=20;

TAdvStringGrid::AddNode(2,4);

TAdvStringGrid::AddNode(7,2);

TAdvStringGrid::AddNode(13,4);

TAdvStringGrid::AddNode()方法原型:

void __fastcall AddNode(int ARow, int Span);第二個參數為分組的跨越度。

  22.展開所有結點:

TAdvStringGrid:: ExpandAll()方法

 收縮所有結點:TAdvStringGrid:: ContractAll()方法

 TAdvStringGrid::CellNode->NodeType=cnFlat;

 TAdvStringGrid::CellNode->NodeType=cn3D;

 TAdvStringGrid::CellNode->NodeType=cnGlyph;

以上三個屬性設定StringGrid以結點的方式顯示時的結點顯示風格。

  23.對TAdvStringGrid添加過濾

使用TAdvStringGrid中預定義的一種Filter類: TFilterData

例如:

TFilterData* fd;

AdvStringGrid3->Filter->Clear();

fd = AdvStringGrid3->Filter->Add();

fd->Condition = ComboBox1->Items->Strings[ComboBox1->ItemIndex];

fd->Column = 1;

fd = AdvStringGrid3->Filter->Add();

fd->Con_dition=ComboBox2->Items->Strings[ComboBox2->ItemIndex];

fd->Column = 3;

AdvStringGrid3->FilterActive=true;

  24.自動調整列寬AutoSizeColumns

原型:void __fastcall AutoSizeColumns(const bool DoFixedCols, const int Padding)

用第二個參數來指定自動調整後需要額外增加的空間

  25.TAdvStringGrid的列印。

  使用TAdvPreviewDialog連接配接TAdvStringGrid來預覽.這個控件中提供了接口可以直接對界面進行本地化。

  使用TAdvStringGrid的Print()方法來列印。列印設定調整TAdvStringGrid::TPrintSettins類的相關屬性。

**************************

技巧二

procedure TForm1.FormCreate(Sender: TObject);

begin

AdvStringGrid1.FixedRows:=2;

AdvStringGrid1.SaveFixedCells := false;

AdvStringGrid1.MergeCells(1,0,2,1);

AdvStringGrid1.MergeCells(3,0,2,1);

AdvStringGrid1.Cells[1,0] := '<p align="center"> <b>Identification</b></p>';

AdvStringGrid1.Cells[1,1] := 'Brand';

AdvStringGrid1.Cells[2,1] := 'Type';

AdvStringGrid1.Cells[3,1] := 'CC';

AdvStringGrid1.Cells[4,1] := 'Pk';

AdvStringGrid1.Cells[3,0] := '<p align="center"><b>Power</b></p>';

AdvStringGrid1.BtnEdit.ButtonWidth := 24;

end;

一、Navigation——AdvancelInsert:控制當光标在最後一個CELL時是否可以按回車插入新行

二、Navigation——AdvanceOnEnter:控制按回車是否自動移到下一個CELL

三、Navigation——AllowDeleteRow:控制是否按Delete鍵删除目前行

四、Navigation——AppendOnArrowDown:控制按下箭頭是否可以新增一行

五、EnableWheel:=true時,一次滾動多行,EnableWheel:=False時,一次滾動一行。

六、SearchFooter—Visible:=TRUE時,會在ADVStringGrid的底部顯示出搜尋框

七、Options—goTabs:控制是否可用TAB鍵将光标移到下一CELL

八、AutoNumAlign:=True則數字類型資料在CELL裡會自動右對齊

九、ADVStringGrid.AutoNumberCol(0);表示第0列按順序顯示數字,即1,2,3……

十、FloatingFooter—Visible:為TRUE時,即在ADVStringGrid底部顯示求和列,要想讓求和列顯示小數,還需要将FloatFormat屬性設定為:%g

十一、當合并行後,如果想使文字垂直居中,可以先設定ADVStringGrid的 MultilineCells 設定為 True,然後在文字前面加 #13 換行來實行

十二、改變固定列的顯示樣式:ControlLook——FixedGradientFrom(起始色)——FixedGradientFrom(結束色)

十三、改變單元格的背景色,可在OnGetCellColor事件中寫代碼實行(前提需将FLAT設定為TRUE):

procedure Tfrm_dingdan.strgridGetCellColor(Sender: TObject; ARow,

ACol: Integer; AState: TGridDrawState; ABrush: TBrush; AFont: TFont);

begin

if ARow=0 then     //如果不加這一句,則是以下對應的整列,我這裡是隻改變固列,是以要把ARow設為0

    begin

      Case ACol of

        1: ABrush.Color:=RGB(227,249,248);

        2: ABrush.Color:=RGB(250,232,193);

        3: ABrush.Color:=RGB(227,249,248);

        4: ABrush.Color:=RGB(250,232,193);

        12: ABrush.Color:=RGB(227,249,248);

        14: ABrush.Color:=RGB(250,232,193);

        24: ABrush.Color:=RGB(227,249,248);

        48: ABrush.Color:=RGB(250,232,193);

        51: ABrush.Color:=RGB(227,249,248);

      End;

    END;

end;

十四、如果在錄入的過程中,要對某列做格式化,比如保留幾位小數,可用ADVStringGrid的GetFloatFormat事件中操作。比如:

procedure Tfrm_dingdan.strgridGetFloatFormat(Sender: TObject; ACol,

ARow: Integer; var IsFloat: Boolean; var FloatFormat: String);

begin

case ACol of

    4: floatformat:='%.0f';     //第4列保留0位小數

    5: floatformat:='%.3f';     //第5列保留3位小數

    6: floatformat:='%.6f';    //第6列保留6位小數

    7: floatformat:='%.5f';    //第7列保留5位小數

end;

end;

十五、Options—goRangeSelect:控制是否可以選擇多行,goRowSelect控制選中整行

、AdvStringGrid做多表頭

在onIsFixedCell事件中傳回true的就是表頭

--------------------------------------------------------------------

2、怎樣實作AdvStringGrid的列寬自動按内容調整?

   怎樣實作AdvStringGrid的列寬自動按列标題寬度調整?

   autosize屬性的作用是什麼?

autosize,可以根據内容調整列寬。

自動按标題列調整,自己在其RESIZE事件裡對COLWIDTHS[I]指派就行了。

procedure AutoSizeColumns(const DoFixedCols: Boolean; const Padding: Integer);

例:AdvStringGrid1.AutoSizeColumns(False,16);

AdvStringGrid1.AutoSizeColumns(False,16);

第一參數:是否為固定列;第二個參數,文字後面留的空格數

所有單元格自動調整,由第一個參數設定是否含固定單元,

如要固定行折行顯示應将其行高設為自動調整即:

AdvStringGrid1.AutoSizeRow(0);

--------------------------------------------------------------------- 

3、AdvStringGrid插入checkbox列

把advstringgrid的option屬性中的goEditing 設為true

在from create事件中加入

for i:=1 to advstringgrid.rowcount-1 do

    advstringgrid.AddCheckBox(1,i,false,false);   //其中1表示所在列數

   GetCheckBoxState(col,row) 可以查詢的

   Bchek:Boolean;

   AdvSGrid.GetCheckBoxState(Col,Arow,Bchek);

---------------------------------------------------------------------

4、stringGrid中的行或列和并

grdList.MergeCells(0,0,2,2);

   grdList.Cells[0,0]:='123456';

   advstringgrid.mergecol(3,4);

   advstringgrid..MergeCells(0,0,2,2);

5、 

with advStringGrid1 do   //引用單元格時, 列數在前,行數在後.

MergeCells(0,0,1,3);    

//合并單元格.前兩參數為列數行數. 後兩參數分别為要合并的列數和行數

Cells[1,0] := Format('分度線( %s )',[m_sMeasureRangeUnit]);;

          MergeCells(3,JieDianStart - 1,1,1);

          Cells[3,JieDianStart - 1] := '動作方式';

          ColWidths[3] := ColWidths[3] + 10;

          Colors[3,JieDianStart - 1] := FixedColor;

          FontNames[3,JieDianStart - 1] := FixedFont.Name;

          FontSizes[3,JieDianStart - 1] := FixedFont.Size;

          FontStyles[3,JieDianStart - 1] := FixedFont.Style;

前面是我在程式中拉的一段.

控件常用事件:

//單元格可否被修改

onCanEditCell(Sender: TObject; ARow, ACol: Integer; var CanEdit: Boolean);

begin

if ACol = 0 then

    CanEdit := false; //false不能修改

end;

//對齊方式

AdvstringGrid設定标題行對齊方式 

procedure Tfrmmain.AdvStringGrid1GetAlignment(Sender: TObject; ARow,

ACol: Integer; var HAlign: TAlignment; var VAlign: TVAlignment);

begin

if ARow=0 then

begin

     HAlign := taCenter;

     VAlign := vtaCenter;

end; 

AdvstringGrid好像沒有直接提供設定列标題對齊的方式,但可以在事件中來設定對齊,用到的事件是GetAlignment事件,隻需要在Arow參數為0時,把HAlign設定為taCenter、VAlign設定為Vtacenter就可以實作水準垂直劇中對齊。

onGetAlignment(Sender: TObject; ARow,

ACol: Integer; var HAlign: TAlignment; var VAlign: TVAlignment);

begin

     HAlign := taCenter;

     VAlign := vtaCenter;

end;

//是否固定列 ,即标題列, 在表中非開頭行需要固定标題列時很有用

onIsFixedCell(Sender: TObject; ARow,

ACol: Integer; var IsFixed: Boolean);

begin

     if(ARow = 10) then

     begin

          IsFixed := True;

     end;

end;

-------------------------------------------------------------------------------

6、如何在AdvStringGrid中為每個單元格内的字型定制顔色

在OnDrawCell事件(看名字這個控件應該有這個事件把,呵呵!)寫如下代碼:

(Sender as TAdvStringGrid).Canvas.Font.Color:=clNavy;

7、cell中内嵌combobox

OnGetEditorType事件

if ACol = 1 then

begin

        aEditor := edComboList;

        grdMain.ClearComboString;

        grdMain.AddComboString('借');

        grdMain.AddComboString(' 貸');

end;