DBGrid应用
在Delphi語(yǔ)言數(shù)據(jù)庫(kù)編程中,DBGrid是顯示數(shù)據(jù)的常用控件之一,下面來(lái)介紹一些DBGrid的用法。
1. 獲取DBGrid選中單元格的一些信息
procedure TForm2.DBGrid1CellClick(Column: TColumn); varstrValue:string;iCol,iRow:Integer;rRect:TRect; beginstrValue := DBGrid1.SelectedField.Value; //獲取選中單元格的值iCol := TDrawGrid(DBGrid1).Col; //獲取選擇單元格的列,第一列為1iRow := TDrawGrid(DBGrid1).Row; //獲取選擇單元格的行,第一行為1rRect := TDrawGrid(DBGrid1).CellRect(iCol,iRow); //獲取選擇單元格的矩形框//ShowMessage('行:'+IntToStr(iRow)+#13'列:'+IntToStr(iCol)+#13'值:'+strValue); end;2. 設(shè)置列名格式,一般列名只需設(shè)置一次(這里一次性繪制所有的列,以避免列數(shù)過(guò)多時(shí),顯示滾動(dòng)條后的列時(shí)再繪制時(shí)影響一些正常的操作,譬如按鍵盤(pán)方向鍵定位單元格時(shí)可能會(huì)光標(biāo)移到第一行),即可用全局變量進(jìn)行記錄下。
procedure TForm2.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;DataCol: Integer; Column: TColumn; State: TGridDrawState); vari:Integer; beginif bDrawTitle=False then //一般情況畫(huà)一次就夠了,不必重復(fù)畫(huà)beginfor i:=0 to DBGrid1.Columns.Count-1 do //一下子繪制所有的列beginif i mod 2 = 0 thenbeginDBGrid1.Columns[i].Title.Font.Color := clRed; //列名 字體顏色DBGrid1.Columns[i].Title.Color := clGreen; //列名 背景DBGrid1.Columns[i].Color := clBlue; //列數(shù)據(jù) 背景end;end;bDrawTitle := True;end;//將數(shù)據(jù)背景斑馬線(xiàn)顯示if CDS1.RecNo mod 2 = 0 thenbegin(Sender as TDBGrid).Canvas.Brush.Color := clGray;end;(Sender as TDBGrid).DefaultDrawColumnCell(Rect,DataCol,Column,State); end;3. 設(shè)置Grid單元格邊框
procedure TForm2.DBGrid1CellClick(Column: TColumn); variCol,iRow:Integer;rRect:TRect; beginiCol := TDrawGrid(DBGrid1).Col; //獲取選擇單元格的列,第一列為1iRow := TDrawGrid(DBGrid1).Row; //獲取選擇單元格的行,第一行為1rRect := TDrawGrid(DBGrid1).CellRect(iCol,iRow); //獲取選擇單元格的矩形框with DBGrid1.Canvas dobeginPen.Width := 4; //設(shè)置畫(huà)筆 寬帶Pen.Color := clRed; //設(shè)置畫(huà)筆 顏色MoveTo(rRect.Left,rRect.Top); //設(shè)置畫(huà)筆 起點(diǎn)LineTo(rRect.Right,rRect.Top); //畫(huà)線(xiàn)Pen.Width := 2;Pen.Color := clYellow;MoveTo(rRect.Right,rRect.Top);LineTo(rRect.Right,rRect.Bottom);end; end;轉(zhuǎn)載于:https://www.cnblogs.com/liuke1987/archive/2013/02/06/2907892.html
總結(jié)
- 上一篇: POJ 1014 Dividing(多重
- 下一篇: Extjs4.1:模式窗口的设置