简体   繁体   English

如何在C ++ Builder或Delphi中将数据输入TGrid单元格

[英]How to enter data into TGrid Cells in C++ Builder or Delphi

I have some difficulties in entering some data into TGrid Cells, can someone give me some code example with commentary on how to insert data into a TGrid cell? 我在向TGrid单元格输入一些数据时遇到了一些困难,有人可以给我一些代码示例以及如何将数据插入TGrid单元格的注释吗? To be more precise in C++ Builder when I'm using a StringGrid I can use 更准确地说,当我使用StringGrid时,我可以使用C ++ Builder

StringGrid1->Cells[1][0] = "Hello world";

which will insert in the cell of the second column of the first row the "hello world" message. 这将在第一行的第二列的单元格中插入“hello world”消息。 How can I do the same with TGrid? 我怎样才能对TGrid做同样的事情? And how can I use TCheckColumn? 我怎样才能使用TCheckColumn? I have many diffulties because I cannot find any good documentation. 我有很多困难,因为我找不到任何好的文件。
I'm looking but there is no guide on this anywhere. 我在寻找,但在任何地方都没有这方面的指南。

TL;DR: TL; DR:
You need to store the data in your own data structure, and pass it to the displayed grid via the OnGetValue event. 您需要将数据存储在您自己的数据结构中,并通过OnGetValue事件将其传递给显示的网格。


I found the answer in the link to MonkeyStyler provided by @nolaspeaker in the comments. 我在评论中找到了@nolaspeaker提供的MonkeyStyler 链接中的答案。

TGrid does not store any data internally. TGrid内部不存储任何数据。

You need to store the data yourself. 您需要自己存储数据。 When a cell in your grid is displayed, the OnGetValue(Sender: TObject; const Col, Row: Integer; var Value: TValue) event is fired. 当显示网格中的单元格时,将OnGetValue(Sender: TObject; const Col, Row: Integer; var Value: TValue)事件。

It is up to you to implement an event handler for this, and return the data for the given cell. 您可以为此实现事件处理程序,并返回给定单元格的数据。

For example, suppose you have a very simple grid that only shows "hello" in every cell of the first column and "world" in every cell of the second column. 例如,假设您有一个非常简单的网格,它只在第一列的每个单元格中显示“hello”,在第二列的每个单元格中显示“world”。
Your OnGetValue event would look like this: 您的OnGetValue事件将如下所示:

procedure MyOnGetValueHandler(Sender: TObject; const Col, Row: Integer; var Value: TValue);
begin
  if Col = 0 then
    Value := 'hello'
 else 
 if Col = 1 then
    Value := 'world';
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM