最近看一个类似于GBGrid的例子,每次向这个Grid中添加新条目的时候,新添加的行始终在Grid的最上边,前边添加的都向下滚动。
比如新添的是A这一行,下一条新添的B这一行,B就在A的上边,A就变成第二行。依此类推。不知道我表述清楚了没这是个什么控件?或者是常用的DBGrid,StringGrid等等的设置不对?困惑ing 

解决方案 »

  1.   

    这取决于grid的排序,这个可以自己控制,或者使用dxdbgrid或者cxdbgrid就使dev的那套组件,可以实现
      

  2.   

    用Delphi自带的控件不能实现嘛?另外,这个类似Grid的控件,Row是不定的。添加的时候,RowCount也是动态增加的。
      

  3.   

    一般用Append就是添加在最后,用Insert就是在前面,比如:当前选中行A,你用Insert B,B就在A的上面,如果要总是第一行,你就先移动到第一行,代码:  dbgrd1.DataSource.DataSet.DisableControls;
      try
        dbgrd1.DataSource.DataSet.First;
        dbgrd1.DataSource.DataSet.Insert;
      finally
        dbgrd1.DataSource.DataSet.EnableControls;
      end;
      

  4.   

    這個要看實際狀況了,參考:
    For Paradox tables with primary indexes, the record is inserted into the dataset in a position based on its index.  For Paradox tables without primary indexes, the record is inserted into the dataset at the current position.  For dBASE, FoxPro, and Access tables, the record is physically appended to the dataset at the end. If an index happens to be active, the new record may appear in a position relative to the index, but the record is still actually stored at the end of the table.  For SQL databases, the physical location of the insert is implementation-specific. For indexed tables, the index is updated with the new record information.  After the new record is applied back to the database server, its physical location is database-specific. For indexed tables, the index is updated with the new record information.  
      

  5.   

    插入就在当前行上边;
    追加就在最后一行追加;
    所有DBGrid都符合这一规则。
    包括TDBGrid, TDBGridEh, TcxGrid, TdxGrid等。
    当然后两种有更特殊的方法,直接设置属性就可以在首行添加。
    其他的可参考#3楼代码
      

  6.   

    首先谢谢各位关注。好象是我没表述清楚。这么说吧,我用的StringGrid控件,采集信号写入里边。这个StringGrid初始只有一行。
    然后在陆续采集、写入的过程中,采用  Value:= 采集函数返回值;(Single)
      StringGrid1.RowCount:= Count+1;
      StringGrid1.Cells[0,Count]:= FormatFloat('0.000',Value);
      StringGrid1.Row:= Count;这样的话,添加的数据条目就是上边说的Append的结果,逐条添加在所有条目的后边;
    而我想要的是上边说的Insert的结果,新的条目始终在最前边。这个是不需要保存数据库的,所以,DataSource,DataSet什么的都不需要。应该怎么做?越弄越迷糊了
      

  7.   

    StringGrid1.Rows.Insert(0,你的數據);
      

  8.   

    方法有兩種:
    第一、自己控制數據移動unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, Grids;type
      TForm1 = class(TForm)
        StringGrid1: TStringGrid;
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
        procedure InsertRow(aRow: Integer);
        procedure MoveRowUp(vGrid:TStringGrid; vrow:integer);
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    begin
      stringGrid1.RowCount := stringGrid1.RowCount +1;
      MoveRowUp(stringgrid1,2);//插入到第二行
    end;procedure TForm1.MoveRowUp(vgrid: TStringGrid; vrow:integer);
    var
      s: string;
      i:integer;
    begin
      if vrow=1 then exit;
      for i:=0 to vgrid.colcount-1 do
      begin
        s:=vgrid.cells[i,vrow-1];
        vgrid.cells[i,vrow-1]:=vgrid.cells[i,vrow];
        vgrid.cells[i,vrow]:=s;
      end;
      vgrid.row:= vrow;
      vgrid.repaint;
    end;
    end.
    第二種,修改控件,利用MoveRow(位于protected),如下:type
      TStringGridEx = class(TStringGrid)
      protected
        procedure InsertRow(ARow: Longint);
      end;implementationprocedure TStringGridEx.InsertRow(ARow: Longint);
    var
      GemRow: Integer;
    begin
      GemRow := Row;
      while ARow < FixedRows do Inc(ARow);
      RowCount := RowCount + 1;
      MoveRow(RowCount - 1, ARow);
      Row := GemRow;
      Rows[Row].Clear;
    end;end.
      

  9.   

    没难道就没有个简单的办法或者类似的控件能解决这个问题?只是一个显示,不想弄得这么复杂难道真的要去修改控件?就是个数据显示:采集一条,显示成:
    [12:13:15][正向]7.872V下一条采集到了以后,显示变成:
    [12:13:16][正向]7.881V
    [12:13:15][正向]7.872V再来一条,显示就是:
    [12:13:18][正向]7.867V
    [12:13:16][正向]7.881V
    [12:13:15][正向]7.872V要能选中某一行,需要做个弹出右键菜单的。我自己觉得这个类似一个StringGrid或者DBGrid,楼上有大侠说是可以用Dev系列的,我也在想,应该就是一个可以在属性里边设置的控件吧
      

  10.   

    要排序的。即使是用DEV,也要設置排序。只是DEV可以直接在Column設置排序。若不設定排序,也要自己可以控制位置,比如先view.GotoFirst;然后dataController.Insert;你認為前面那點代碼復雜嗎?分出一個procdure調用,也不會復雜到哪里去啊。若是從效能上考慮,數據非常多的情況下,這樣移動row,確實會耗時。除非你換控件?
      

  11.   

    按照大虾的方式,自己写了个,控制数据移动,小数据量的还没什么问题,不知道数据量大了会是什么效果,正在测试。
    但是发现个问题就是最下边一行始终是空白的,不太好看procedure TForm1.InsertRow(vGrid:TStringGrid; Str:Array of String);
    var
      i,j: Integer;
    begin
      if vGrid.RowCount=1 then
      begin
        for i:=0 to 2 do
          vGrid.Cells[i,0]:= Str[i];
      end
      else
      begin
        for j:=0 to vGrid.ColCount-1 do
          for i:=vGrid.RowCount-1 downto 1 do
          begin
            vGrid.Cells[j,i]:= vGrid.Cells[j,i-1];
          end;
        for i:=0 to 2 do
          vGrid.Cells[i,0]:= Str[i];
      end;
      vGrid.RowCount:= vGrid.RowCount+1;
    end;
      

  12.   

    唉,还是得借助全局变量来实现
    procedure TForm1.InsertRow(vGrid:TStringGrid; Str:Array of String);
    var
      i,j: Integer;
    begin
      if One=1 then
      begin
        for i:=0 to 2 do
          vGrid.Cells[i,0]:= Str[i];
        One:= 2;
      end
      else
      begin
        vGrid.RowCount:= vGrid.RowCount+1;
        for j:=0 to vGrid.ColCount-1 do
          for i:=vGrid.RowCount-1 downto 1 do
          begin
            vGrid.Cells[j,i]:= vGrid.Cells[j,i-1];
          end;
        for i:=0 to 2 do
          vGrid.Cells[i,0]:= Str[i];
      end;
      vGrid.Row:= 0;
    end;
    谢谢大家了。