【问题标题】:How to insert data into Grid from record如何从记录中将数据插入到网格中
【发布时间】:2016-11-21 14:43:18
【问题描述】:

我有三个文件的记录:

type
    TItem = record

    Item    : String;
    Quantity: SmallInt;
    Price   : Currency;
 end;

我还有将值设置为记录的程序:

function TForm1.SetItem(item:string;quan:SmallInt;price:Currency):TItem;
   var It :TItem; 
   begin
          It.Item :=item;
          It.Quantity:= quan;
          It.Price:=price;
         Result :=It;
   end;

现在,我需要一个将 record TItem 插入 TStringGridTGrid 的过程,但我不知道该怎么做。 我的 TStringGrid 中也有三列:

1. col_Item     :string;
2. col_Quantity :SmallInt;
3. col_Price    :Currency;

每次当我调用程序 SetItem 时,我都需要将记录中的三个字段插入到这三列中:

结果应该是这样的:

ITEM      | Quantity  | Price   

Bread         1         1,5
Coca cola     1         3
Fanta         2         3

..等等。

【问题讨论】:

  • 你的过程 SetItem() 没用。它创建一个 TItem 类型的记录,用值填充它,然后将其丢弃而不传回。
  • @GuidoG 我有更新问题。这是错误。你能帮我看看我需要什么吗?
  • Firemonkey 应用程序?
  • @LURD 是的......

标签: delphi firemonkey delphi-10.1-berlin


【解决方案1】:

首先,网格 (TGrid) 不存储数据,因此您需要提供像 f.ex 这样的数据存储。 TDataArr = array of TItem;。当网格需要在单元格中显示数据时,它会调用OnGetValue() 事件:

procedure TForm4.Grid1GetValue(Sender: TObject; const Col, Row: Integer;
  var Value: TValue);
begin
  if Row > (Length(DataArr)-1) then exit;
  case Col of
    0: Value := DataArr[Row].Item;
    1: Value := DataArr[Row].Quantity;
    2: Value := DataArr[Row].Price;
  end;
end;

网格中的显示存在隐式转换为字符串。

当您在网格中编辑数据时,更改会触发OnSetValue 事件:

procedure TForm4.Grid1SetValue(Sender: TObject; const Col, Row: Integer;
  const Value: TValue);
begin
  if Row > (Length(DataArr)-1) then exit;
  case Col of
    0: DataArr[Row].Item := Value.AsString;
    1: DataArr[Row].Quantity := StrToInt(Value.AsString);
    2: DataArr[Row].Price := StrToCurr(Value.AsString);
  end;
end;

似乎没有其他方式的隐式转换,因此StrToInt(Value.AsString)StrToCurr(Value.AsString)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多