請教各位高手,如何將記錄號加到dbgrid 中? Delphi / Windows SDK/API
http://www.delphi2007.net/DelphiDB/html/delphi_20061223091242150.html
下面我的dbgrid中顯示的代碼,如何將記錄號加到dbgrid 列中,謝謝了!
qq1:=tadoquery.Create(self);
qq1.Connection:=conn;
with qq1 do
begin
close;
sql.Clear;
sql.Add('select id,haoma as 號碼,guishu as 歸屬地,tongxunming as 通訊公司 from haoma ');
open;
end;
datasource1.DataSet:=qq1;
dbgrid1.DataSource:=datasource1;
id 不就是記錄號麼
id 只是資料庫中的一個自動編號,不一定是從1開始,按依次加1的順序往下走的
{覆蓋DrawCell事件,顯示行序號 NO }
procedure TDBGridEh.DrawCell(ACol:Integer;ARow:Integer;ARect:TRect;AState:TGridDrawState);
var
i,DBRow,lLeft:integer;
iCol,iRow:integer; //全域變數
begin
inherited;
{是否開啟顯示行號功能!}
if fShowRecNo=True then
begin
if Assigned(DataLink) and
DataLink.Active then //沒有open
begin
if (ARow>=1) and
(ACol=0) then{行標題之內}
begin
i := 0;//位移值
lLeft := 0;//置中顯示
DBRow := self.DataSource.DataSet.RecNo;//當前資料集所在的行標 rec no
if DataSource.DataSet.RecNo=-1 then{新增選項組}
begin
DBRow := DataSource.DataSet.RecordCount+1;//當前行標取記錄總數+1
end;
i := DBRow - self.Row ;//計算表格TopRow和資料集所在行的位移值(顯示實際資料集行號)
lLeft := (ARect.Right - ARect.Left) div 2 - Canvas.TextWidth(IntToSTr({DataLink.ActiveRecord}ARow+i)) div 2;//置中顯示
Canvas.TextRect(ARect,ARect.Left+lLeft,ARect.Top,IntToSTr({DataLink.ActiveRecord}ARow+i));//輸出行號
end;
end;
end;
end;
完整的有個控制項,使用非常簡單!
留下Email,我發給你.或加我QQ:67016879
1、在DBGRID中增加一列,列名的抬頭比如說是'序號'
2、代碼:
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if DataSource1.DataSet.RecNo > 0 then
begin
if WideUpperCase(Column.Title.Caption) = '序號' then
DBGrid1.Canvas.TextOut(Rect.Left + 2, Rect.Top, IntToStr(DataSource1.DataSet.RecNo));
end;
end;
好麻煩.
結貼