了解AdvStringGrid
生活随笔
收集整理的這篇文章主要介紹了
了解AdvStringGrid
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.利用AdoConnect+AdoQuery鏈接SqlServer查詢數據
int i,j,col,row;UnicodeString strSQL;strSQL = " select * from viewBasicLensProblem";adoquery->Close();adoquery->SQL->Clear();adoquery->SQL->Add(strSQL);adoquery->Open();col = qryBasic->FieldCount;row = qryBasic->RecordCount;strGrid1->ColCount = col;strGrid1->RowCount = row+1;for (i =1; i < row+1; i++){for ( j =0; j< col; j++){strGrid1->Cells[j][i] = qryBasic->Fields->Fields[j]->AsString;}qryBasic->Next();}2.AdvStringGrid合并行和列
strGrid1->MergeCols(3,4); //合并列strGrid1->MergeCells(0,0,2,2); //合并行/*MergeCols(col,row);mergeCells(col,row,c,d) 單元格合并函數col 單元格的列號row 單元格的行號c 從單元格[col,row]起,向下合并的列數d 從單元格[col,row]起,向下合并的行數*/ 附:重繪表格 strGrid->OnDrawCell()void __fastcall TForm1::strGrid1DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect,TGridDrawState State) {//重繪int c1 = 0, r1 = 0, c2 =1 , r2 =1 ;if (ACol>=c1 && ACol<=c2 && ARow>=r1 && ARow <= r2){TRect rect1 = strGrid1->CellRect(c1,r1);TRect rect2 = strGrid1->CellRect(c2,r2);TRect rect = TRect(rect1.left,rect1.top,rect2.left-1,rect2.bottom-1);TStringGrid *grid =(TStringGrid*)Sender;TCanvas*canvas = grid->Canvas;canvas->FillRect(rect);canvas->Pen->Color = clBlack;canvas->MoveTo(rect.left,rect.top);canvas->LineTo(rect.right,rect.bottom);canvas->MoveTo(rect.left,rect.bottom);canvas->LineTo(rect.right,rect.top);canvas->TextOut(rect.left + rect.Width()/2-canvas->TextWidth("CombineGrids")/2,rect.top + rect.Height()/2 - canvas->TextHeight("CombineGrids")/2,"CombineGrids");} }void __fastcall TForm1::strGrid1DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect,TGridDrawState State) {//隔行換背景色if(ARow>0){if(ARow%2 == 0){strGrid1->Canvas->Brush->Color = clWhite;strGrid1->Canvas->FillRect(Rect);strGrid1->Canvas->Font->Color = clBlue;}else{strGrid1->Canvas->Brush->Color = clCream;strGrid1->Canvas->FillRect(Rect);}DrawText(strGrid1->Canvas->Handle, strGrid1->Cells[ACol][ARow].c_str(),-1, (RECT*)&Rect, DT_SINGLELINE | DT_VCENTER | DT_CENTER);}if(ARow==0)//對標題行操作 {int wwd=strGrid1->Canvas->TextWidth(strGrid1->Cells[ACol][ARow]);//獲取文本長度int colwd=strGrid1->ColWidths[ACol];//獲取列寬colwd+=2; //一般開始留幾個像素int ww=(colwd-wwd)/2; //文本開始位置if(ww<0)ww=0;strGrid1->Canvas->Brush->Color=clWhite;strGrid1->Canvas->Brush->Style=bsSolid;strGrid1->Canvas->Rectangle(Rect.Left+2,Rect.Top+2,Rect.Right,Rect.Bottom);strGrid1->Canvas->TextOut(Rect.Left+ww,Rect.Top+4,strGrid1->Cells[ACol][ARow]);} }? 2.1多表頭
//多表頭strGrid1->Multilinecells = true;strGrid1->MergeCells(0,0,2,1);strGrid1->Alignments[0][0] = taCenter;strGrid1->Cells[0][0] = "中國";strGrid1->Cells[0][1] = "福建";strGrid1->Cells[1][1] = "北京"; //多表頭strGrid1->FixedRows = 2; //固定行數strGrid1->SaveFixedCells = false;strGrid1->MergeCells(1,0,2,1);strGrid1->MergeCells(3,0,2,1);strGrid1->Cells[1][0]="Title1";strGrid1->Cells[1][1]="Brand";strGrid1->Cells[2][1]="Type";strGrid1->Cells[3][1]= "CC";strGrid1->Cells[4][1]= "Pk";strGrid1->Cells[3][0]= "Title2";strGrid1->BtnEdit->ButtonWidth = 24;2.2
IsFixedCell()
void __fastcall TForm1::strGrid1IsFixedCell(TObject *Sender, int ARow, int ACol, bool &IsFixed){if (ACol==5||ACol==2){IsFixed = true;} }3.常用屬性設置
http://www.cnblogs.com/JackSun/archive/2010/12/16/1908119.html
是否能對特定的單元格進行編輯?
????? 用TAdvStringGrid::OnCanEit()事件句柄來處理,
????? 設置bool &canedit參數來達到能否編輯某些單元格的效果
隱藏和顯示某些單元格
????? TAdvStringGrid:: UnHideColumn(int ACol)
????? TAdvStringGrid::HideColumn(int ACol);
4.OnGetEditorType()更改cell樣式
void __fastcall TForm1::strGrid1GetEditorType(TObject *Sender, int ACol, int ARow,TEditorType &AEditor) {switch(ACol){case 1:AEditor = edComboList; break;case 2:AEditor = edEditBtn; break;case 3:AEditor = edSpinEdit; break;case 4:AEditor = edDateEdit; break;}//增加edComboEdit和edComboList的下拉列表:strGrid1->ClearComboString();strGrid1->AddComboString("List1");strGrid1->AddComboString("List2");strGrid1->AddComboString("List3");strGrid1->Combobox->Sorted = true; //排序 } //增加Checkbox//設置StringGrid->Options->goEditing = true;//stringGrid->enableGraphics = true;//OnFormCreate()for (int i = 1; i < strGrid1->RowCount-1; i++){strGrid1->AddCheckBox(0,i,false,false);//0表示col[0] }bool bChecked;//讀取checkbox狀態for (int j=0; j<strGrid1->RowCount-1; j++){strGrid1->GetCheckBoxState(1,j,bChecked);if (bChecked){ShowMessage("已勾選");}else{ShowMessage("未勾選");}}//設置checkbox狀態for (int i=1; i<strGrid1->RowCount-1; i++){if (bChecked){strGrid1->SetCheckBoxState(0,i,false);}else{strGrid1->SetCheckBoxState(0,i,true);}}5.結點和過濾
void __fastcall TForm1::btn1Click(TObject *Sender) {//添加結點strGrid1->FixedCols = 0;strGrid1->FixedColWidth = 20;strGrid1->AddNode(2,3);strGrid1->AddNode(4,2);//strGrid->AddNode(int ARow,int ASpan) Span--分組的跨越度 }void __fastcall TForm1::btn2Click(TObject *Sender) {//打開所有結點strGrid1->ExpandAll();strGrid1->CellNode->NodeType = cnFlat; // strGrid1->CellNode->NodeType = cn3D; // strGrid1->CellNode->NodeType = cnGlyph;//收縮所有節點strGrid1->ContractAll(); } //--------------------------------------------------------------------------- void __fastcall TForm1::btn3Click(TObject *Sender) {//過濾TFilterData *fd;strGrid1->Filter->Clear();fd = strGrid1->Filter->Add();fd->Condition = cbb1->Items->Strings[cbb1->ItemIndex];//fd->Column = 1;if (cbb2->ItemIndex !=NULL){fd = strGrid1->Filter->Add();fd->Condition=cbb2->Items->Strings[cbb2->ItemIndex];}//fd->Column = 3;strGrid1->FilterActive=true; }6.fastReport詳解
轉載于:https://www.cnblogs.com/marlbora/p/4118736.html
總結
以上是生活随笔為你收集整理的了解AdvStringGrid的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【原创】leetCodeOj ---Co
- 下一篇: 数据类型_插入数据_选取数据_修改数据—