对于RecNo属性有时会返回-1的,如果你有这种特殊的爱好的话,给你一条sql语句试一下:) select identity(int,1,1) as 自增字段名,* into 目标表名 from 原始表名执行了上面的语句后,你在DBGrid中显示的时候,只要显示“目标表名”的内容即可,它与原始表中的数据是一样的select aa as '序号' from ( select indentity(int, 1, 1) as 'aa' from 原始表名 ) as 目标表名
Access不熟悉,但是下面的方法在大型数据库里面好用,你可以试一试: 临时创建一个表,自增长 然后select into 到这个表,产生临时数据,再select出来。 (为了提高速度,可以只是select into 主键,然后两个表jion)
以数据集的recno作为计算字段值即可
dbgrid好像有这个功能吧,如果我没有记错的话
这个问题很偏么?怎么知道的人好像没几个? to gzllich(刚从泥坑里出来): 能不能说具体一些。
我用SQl Ado 直接利用Recno重画
procedure TForm1.Query1CalcFields(DataSet: TDataSet); begin query1.fieldbyname('xuhao').asinteger:=dataset.recno;//xuhao为计算字段(integer)
在数据库表中新增一字段FieldNo, 代码如下:看了之后给分吧…………………… procedure TForm1.FormCreate(Sender: TObject); begin with adoquery1 do begin close; sql.Clear; sql.Add('select * from area'); open; end; end;procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect; Field: TField; State: TGridDrawState); begin if Field.FieldName = 'FieldNo' then TDBGrid(Sender).Canvas.TextOut(Rect.Left,Rect.Top,IntToStr(Field.DataSet.RecNo));end;
用 with adoquery1 do begin close; sql.Clear; sql.Add('SELECT ROWNUM aa,....... from tablename'); open; end; end;这样第一列就是序号了。
Access里的自动编不行,比如:输入后张三丰为3号,那么,在1号2号记录删除后,张三丰还是3号。
而我要的是:不管记录(顺序)如何变化,DBGrid的第一列都显示为123456789...
(我是用来排名的)。
select identity(int,1,1) as 自增字段名,* into 目标表名 from 原始表名执行了上面的语句后,你在DBGrid中显示的时候,只要显示“目标表名”的内容即可,它与原始表中的数据是一样的select aa as '序号' from
(
select indentity(int, 1, 1) as 'aa' from 原始表名
) as 目标表名
临时创建一个表,自增长
然后select into 到这个表,产生临时数据,再select出来。
(为了提高速度,可以只是select into 主键,然后两个表jion)
to gzllich(刚从泥坑里出来):
能不能说具体一些。
begin
query1.fieldbyname('xuhao').asinteger:=dataset.recno;//xuhao为计算字段(integer)
代码如下:看了之后给分吧……………………
procedure TForm1.FormCreate(Sender: TObject);
begin
with adoquery1 do
begin
close;
sql.Clear;
sql.Add('select * from area');
open;
end;
end;procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
begin
if Field.FieldName = 'FieldNo' then
TDBGrid(Sender).Canvas.TextOut(Rect.Left,Rect.Top,IntToStr(Field.DataSet.RecNo));end;
with adoquery1 do
begin
close;
sql.Clear;
sql.Add('SELECT ROWNUM aa,....... from tablename');
open;
end;
end;这样第一列就是序号了。