预先在 DBGrid 加一列,baiTitle为 “ID”。然du后:在事件中添加“DBGridDisplayDrawColumnCell”参考我的内容自己zhi改吧!我也dao是看别人的,实现了!
图
procedure TForm1.DBGridDisplayDrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); begin if (DataSource.DataSet.RecNo <> 0) then if ( UpperCase(Column.Title.Caption) = ‘ID‘ ) then DBGridDisplay.Canvas.TextOut(Rect.Left + 4, Rect.Top + 2, IntToStr(DataSource.DataSet.RecNo)); end;
重新编号保存后不会有bai多笔同样的编号?
还是du说你只是想在dbgride 上显示? 只想zhi在dbgride 上显示好像有一个dao三方控件。。
好像也有代码的。
var
Form1: TForm1;
i:integer;
bflag,bflag2:boolean;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
adoquery1.Open;
end;
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
rect1:TRect;
sValue :string;
ss:boolean;
begin
if DataCol = 0 then //设置在第一列
begin
if DBGrid1.DataSource.DataSet.state<>dsInsert then
i:= DBGrid1.DataSource.DataSet.recno;
Rect1 := Rect;
with dbgrid1 do
begin
Canvas.Brush.Color := clBtnFace;
sValue := IntToStr(i);
Canvas.FillRect(Rect1);
Canvas.Pen.Width := 1;
Canvas.Pen.Color := clWhite;
Canvas.MoveTo(Rect1.Left, Rect1.Bottom);
Canvas.LineTo(Rect1.Left, Rect1.Top);
Canvas.LineTo(Rect1.right, Rect1.Top);
Canvas.Pen.Color := clBtnShadow;
Canvas.LineTo(Rect1.right, Rect1.Bottom - 1);
Canvas.LineTo(Rect1.Left, Rect1.Bottom - 1);
Rect1.Top := Rect1.Top +1;
if State = [gdSelected, gdFocused] then
begin
if bflag=true then
begin
sValue:=inttostr(datasource.DataSet.RecordCount +1);
i:=datasource.DataSet.RecordCount +1;
ss:=true;
end;
Canvas.Font.Color := clred;
end
else
begin
Canvas.Font.Color := clgreen;
end;
DrawText(Canvas.Handle, PChar(sValue), Length(sValue), Rect1, DT_CENTER);
end;
if i<=dbgrid1.DataSource.DataSet.RecordCount then
begin
inc(i);
end
else
begin
if (ss<>true) then
i:=dbgrid1.DataSource.DataSet.RecordCount+3 -TDrawGrid(DBgrid1).RowCount ;
end;
end
else bflag2:=false;
end;
procedure TForm1.DataSource1StateChange(Sender: TObject);
begin
if dbgrid1.DataSource.DataSet.State <>dsbrowse then
begin
i:=dbgrid1.DataSource.DataSet.RecordCount+3 -TDrawGrid(DBgrid1).RowCount ;
bflag := true;
end
else
bflag:=false;
end;
procedure TForm1.DBGrid1ColEnter(Sender: TObject);
begin
if (bflag=true)and (dbgrid1.SelectedField.FieldNo <>1) then
begin
i:=dbgrid1.DataSource.DataSet.RecordCount+3 -TDrawGrid(DBgrid1).RowCount;
dbgrid1.Refresh;
end;
end;
procedure TForm1.DBGrid1ColExit(Sender: TObject);
begin
if (bflag=true)and (dbgrid1.SelectedField.FieldNo =1) then
begin
i:=dbgrid1.DataSource.DataSet.RecordCount+1;
dbgrid1.Refresh;
end;
end;
end.