2016-11-21 60 views
1

數據插入到網格我有三個記錄歸檔:如何從記錄

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; 

現在,我需要插入一個過程記錄TItemTStringGridTGrid我不知道該怎麼做。 我也有三列在我的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 

..等等。

+2

您的過程SetItem()是無用的。它創建一個TItem類型的記錄,用值填充它,並將其丟棄而不傳回。 – GuidoG

+0

@GuidoG我有更新的問題。這是錯誤的。你能幫助我清楚我需要什麼嗎? – Dejan

+0

Firemonkey應用程序? –

回答

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; 

有一個隱式轉換爲字符串在網格中的DISPLY。

當你在網格中編輯數據,改變觸發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)

+0

謝謝.................. – Dejan

+0

不客氣! –