You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在TMS WEB Core中为TWebTableControl(tblCoupons)加载数据?

TWebTableControl(tblCoupons)加载数据示例与操作指导

基础静态数据加载示例

先通过RowCount和ColCount定义表格的行列数,再用Cells[列索引, 行索引]赋值(注意索引从0开始,第0行默认作为表头):

procedure TForm1.WebFormCreate(Sender: TObject);
begin
  // 设置表格列数(示例为3列:优惠券ID、名称、有效期)
  tblCoupons.ColCount := 3;
  // 设置表头内容
  tblCoupons.Cells[0, 0] := '优惠券ID';
  tblCoupons.Cells[1, 0] := '优惠券名称';
  tblCoupons.Cells[2, 0] := '有效期';
  
  // 设置表格总行数(表头+3条数据,共4行)
  tblCoupons.RowCount := 4;
  
  // 填充第1条数据(行索引1)
  tblCoupons.Cells[0, 1] := 'C001';
  tblCoupons.Cells[1, 1] := '满100减20';
  tblCoupons.Cells[2, 1] := '2024-12-31';
  
  // 填充第2条数据(行索引2)
  tblCoupons.Cells[0, 2] := 'C002';
  tblCoupons.Cells[1, 2] := '全场8折';
  tblCoupons.Cells[2, 2] := '2024-11-30';
  
  // 填充第3条数据(行索引3)
  tblCoupons.Cells[0, 3] := 'C003';
  tblCoupons.Cells[1, 3] := '新人立减50';
  tblCoupons.Cells[2, 3] := '2024-10-31';
end;

从数据集动态加载数据示例

如果需要从TWebDataSet这类数据集加载数据,可通过循环遍历数据集行来填充:

procedure TForm1.LoadCouponsFromDataSet;
var
  RowIdx: Integer;
begin
  if not WebDataSet1.Active then Exit;
  
  // 清空表格原有内容
  tblCoupons.Clear;
  
  // 定义列数与表头
  tblCoupons.ColCount := 3;
  tblCoupons.Cells[0, 0] := '优惠券ID';
  tblCoupons.Cells[1, 0] := '优惠券名称';
  tblCoupons.Cells[2, 0] := '有效期';
  
  // 初始化数据行索引(从1开始,跳过表头行)
  RowIdx := 1;
  WebDataSet1.First;
  while not WebDataSet1.Eof do
  begin
    // 新增一行用于容纳当前数据
    tblCoupons.RowCount := RowIdx + 1;
    // 填充当前行数据
    tblCoupons.Cells[0, RowIdx] := WebDataSet1.FieldByName('CouponID').AsString;
    tblCoupons.Cells[1, RowIdx] := WebDataSet1.FieldByName('CouponName').AsString;
    tblCoupons.Cells[2, RowIdx] := WebDataSet1.FieldByName('ExpireDate').AsString;
    
    WebDataSet1.Next;
    Inc(RowIdx);
  end;
end;

关键注意事项

  • Cells[Col, Row]的参数顺序是列在前,行在后,索引均从0开始
  • 动态添加数据前,需先更新RowCount确保表格有足够行容纳新数据
  • 调用Clear方法可快速清空表格所有内容,便于重新加载数据

内容的提问来源于stack exchange,提问作者Moshelly

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 15:03:13