Moving Columns in a DBGrid seems to move the attached DataSet fields

后端 未结 3 1145
野的像风
野的像风 2021-02-19 23:40

I observed something last week that I did not expect, and will describe below. I am curious as to why this happens. Is it something internal to the TDataSet class, an artifact o

3条回答
  •  耶瑟儿~
    2021-02-20 00:17

    Apparently the behaviour is by design. In fact it is not related to the dbgrid. It is merely a side effect of a column setting a field index. For instance this statement,

    ClientDataSet1.Fields[0].Index := 1;

    will cause the output of the "Show ClientDataSet Structure" button to change accordingly, either there is a grid or not. The documentation for TField.Index states;

    "Change the order of a field's position in the dataset by changing the value of Index. Changing the Index value affects the order in which fields are displayed in data grids, but not the position of the fields in physical database tables."

    One should conclude the reverse should also be true and changing the order of fields in a grid should cause field indexes to be changed.


    The code causing this is in TColumn.SetIndex. TCustomDBGrid.ColumnMoved sets a new index for the moved column and TColumn.SetIndex sets the new index for that column's field.

    procedure TColumn.SetIndex(Value: Integer);
    [...]
            if (Col <> nil) then
            begin
              Fld := Col.Field;
              if Assigned(Fld) then
                Field.Index := Fld.Index;
            end;
    [...]
    

提交回复
热议问题