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;
![QQ截图20141125105703 QQ截图20141125105703](https://i-blog.csdnimg.cn/blog_migrate/c7ee44c3b573e629d062c32a258aa0ac.png)
2.2
IsFixedCell()
void __fastcall TForm1::strGrid1IsFixedCell(TObject *Sender, int ARow, int ACol, bool &IsFixed) { if (ACol==5||ACol==2) { IsFixed = true; } }
![QQ截图20141125110649 QQ截图20141125110649](https://i-blog.csdnimg.cn/blog_migrate/e4ba95e6bd0bf8ea9e11df96af5a075c.png)
3.常用属性设置
http://www.cnblogs.com/JackSun/archive/2010/12/16/1908119.html
是否能对特定的单元格进行编辑
用TAdvStringGrid::OnCanEit()事件句柄来处理,
设置bool &canedit参数来达到能否编辑某些单元格的效果
隐藏和显示某些单元格
TAdvStringGrid:: UnHideColumn(int ACol)
TAdvStringGrid::HideColumn(int ACol);
strGrid1->Options = strGrid1->Options<<goEditing;//可编辑 strGrid1->Options = strGrid1->Options>>goEditing;//只读 strGrid1->Options = strGrid1->Options<<goTabs; //控制是否可用TAB键将光标移到下一CELL strGrid1->Options =strGrid1->Options<<goColSizing;
…
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详解