1

我正在尝试构建一个从数据库读取数据并使用 Firemonkey 中的 TGrid 将其显示为表的工具。我需要使用不同类型的列,例如 TCheckColumn 和 TPopupColumn,但找不到任何关于如何在 C++ Builder 中使用它们的好的指南或示例。

无论如何,我设法了解了 TStringColumn、TProgressColumn 的用法,用于在 TGrid 的事件 onGetValue 中设置单元格的值。

你们中有人知道如何为 TCheckColumn、TImageColumn 和 TPopupColumn 类型的列设置值吗?

谢谢丹尼尔

- -更新 - -

我设法使用了 TProgressColumn。这就是我在表单的构造函数中所做的:

// TStringColumn
Grid1->AddObject(new TStringColumn(this));
// TCheckColumn
TCheckColumn* c = new TCheckColumn(this);
Grid1->AddObject(c);
// TPopupColumn
// list of values
TStringList * l = new TStringList(NULL);
l->Add(L"First");
l->Add(L"Second");
l->Add(L"Third");
TPopupColumn* p = new TPopupColumn(this);
// adding the list to the PopupColumn
p->Items = l;
Grid1->AddObject(p);
// TProgressColumn
Grid1->AddObject(new TProgressColumn (this));
Grid1->RowCount = 3 ;

这是 Grid1GetValue 方法:

// TStringColumn
if(Col == 0) Value = TValue::From<String>(Row);
// TCheckColumn  !! Can't make it work
if(Col == 1) Value = TValue::From<Boolean>(true);
// TPopupColumn
if(Col == 2)    Value = TValue::From<int>(2);
// TProgressColumn
if(Col == 3)    Value = TValue::From<double>(50.0);

- -更新 - -

如果我使用 OnSetValue 方法保存列的值

void __fastcall TForm1::Grid1SetValue(...)
{
if(Col == 1) check = Value;
}

然后用 OnGetValue 方法设置它:

void __fastcall TForm1::Grid1GetValue(...)
{
// TCheckColumn  !! Can't make it work
if(Col == 1) Value = check;// TValue::From<Boolean>(true);
}

在我单击一个复选框后,所有其他复选框都会更改状态。所以组件可以正常工作......现在的重点是如何以正确的方式将 Value 设置为 true 或 false。

4

3 回答 3

1

TGris 不存储任何数据,您应该创建自己的数据存储。

示例:带有 TCheckColumn、TStringColumn 和 TPopupColumn 的 TGrid

type
  TField = record
    Checked:  Boolean;
    Name:     string;
    Column:   Byte;
  end;

var
  Fields: TList<TField>;

function SetField(const AChecked: Boolean; const AName: string; const AColumn: Byte): TField;
begin
  with Result do begin
    Checked := AChecked;
    Name    := AName;
    Column  := AColumn;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  I: Integer;
begin
  Fields := TList<TField>.Create;
  Fields.Add(SetField(True, 'Name', 1));
  Fields.Add(SetField(True, 'Login', 2));
  Fields.Add(SetField(True, 'Password', 3));
  for I := 1 to Fields.Count do
    PopupColumn1.Items.Add('Column ' + IntToStr(I));
  gdFields.RowCount := Fields.Count;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  Fields.Free;
end;

procedure TFormExport.gdFieldsGetValue(Sender: TObject; const Col, Row: Integer; var Value: TValue);
begin
  case gdFields.Columns[Col].TabOrder of
    0:  Value := Fields[Row].Checked;
    1:  Value := Fields[Row].Name;
    2:  Value := Fields[Row].Column - 1;
  end;
end;

procedure TFormExport.gdFieldsSetValue(Sender: TObject; const Col, Row: Integer; const Value: TValue);
var
  FRec: TField;
begin
  FRec := Fields[Row];
  case gdFields.Columns[Col].TabOrder of
    0:  FRec.Checked := Value.AsBoolean;
    1:  FRec.Name    := Value.AsString;
    2:  FRec.Column  := Value.AsInteger + 1;
  end;
  Fields[Row] := FRec;
end;

现在,在编辑 TGrid 后,数据存储中的所有数据都将被更改,但 TGrid 中可能存在错误 - 更改 PopupColumn 后从未收到 OnSetValue

于 2013-09-24T16:52:51.870 回答
0

I can't give C++ code but a Delphi example should be easy enough to translate.

You get and set all cell values the same way, by listening for the OnGetData and OnSetData events, get take/give values of type TValue (XE3 and later). It's just a case of returning the appropriate type in the TValue:

uses System.RTTI;

procedure Form1.Grid1GetValue(Sender: TObject;const Col, Row: Integer;var Value: TValue);
begin
  if Col = 1 then
    Value := TValue.From<Integer>(1)
  else if Col = 2 then
    Value := TValue.From<String>('Hello')
  else if Col = 3 then
    Value := Tvalue.From<Single>(1.0);
end;

procedure Form1.Grid1SetValue(Sender: TObject;const Col, Row: Integer;const Value: TValue);
begin
  if Col = 1 then
    I := Value.As<Integer>
  else if Col = 2 then
    St := Value.As<String>
  else if Col = 3 then
    Si := Value.As<Single>;
end;

As far as I can tell a popup menu can't accept or give data.

于 2013-03-21T20:27:51.590 回答
0

为了解决您的问题,请TCheckCell按以下方式重新定义类:

#include <FMX.Grid.hpp>
#include <boost/dynamic_bitset.hpp>

class CheckCellClass:public TCheckCell
{
public:
 __fastcall virtual CheckCellClass(System::Classes::TComponent*AOwner):TCheckCell(AOwner)
 {
 };

 virtual System::Rtti::TValue __fastcall GetData(void)
 {
  return TValue::From<bool>(this->IsChecked);
 };

 virtual void __fastcall SetData(const TValue&Value)
 {
  TValue V(Value);
  this->IsChecked=V.AsBoolean();
 };
};

//Redifine TCheckColumn class
class CheckColumnClass:public TCheckColumn
{
private:
 virtual Fmx::Controls::TStyledControl*__fastcall CreateCellControl(void)
 {
  CheckCellClass*Cell=new CheckCellClass(this);
  Cell->OnChange     =&(this->DoCheckChanged);
  return Cell;
 };

public:
__fastcall CheckColumnClass(System::Classes::TComponent*AOwner):TCheckColumn(AOwner)
 {
 };
};

//global Data for Save curent State Cell

boost::dynamic_bitset<unsigned char>FullDiscreteInputs;

Add To Grid In Constuctor
 FullDiscreteInputs.resize(100);
 DiscreteInputsGrid->RowCount=FullDiscreteInputs.size();    
 CheckColumnClass* DiscreteInPutsCheckColumn        =new CheckColumnClass(DiscreteInputsGrid);
 DiscreteInputsGrid->AddObject(CoilsCheckColumn);

void __fastcall TForm1::DiscreteInputsGridGetValue(TObject*Sender, const int Col, const int Row,TValue&Value)
{
 //...

 if(DiscreteInputsGrid->ColumnByIndex(Col)==DiscreteInPutsCheckColumn)
 {
 Value=TValue::From<bool>(FullDiscreteInputs[Row]);
 }
 //...
}

//---------------------------------------------------------------------------
void __fastcall TForm1::DiscreteInputsGridSetValue(TObject*Sender, const int Col, const int Row, const TValue&Value)
{
 TValue V(Value);
 if(DiscreteInputsGrid->ColumnByIndex(Col)==DiscreteInPutsCheckColumn)
 {
 FullDiscreteInputs[Row]=V.AsBoolean();
 }
}
//---------------------------------------------------------------------------
于 2015-05-15T07:36:04.507 回答