3

我正在使用 Devexpress TcxGrid 并且正在尝试获取选定的单元格文本。我的 TcxGrid 连接到某种 DataSource - 我认为它是 DataControler。

我的目标是从整行的单元格中获取文本,并将其放在用逗号分隔的字符串中。

4

3 回答 3

4

如果您想要具有多选和来自 TcxGridDbTableView 的值:在我的结果中,我没有行之间的分隔。

function GetSelectedValuesFrmGrid: String;
var
  intSelectLoop,
  intRowLoop: Integer;
  oTableView: TcxGridDbTableView;
  strValue: Variant;
  oList: TStringList;
begin
  Result:= '';
  // Kind Of TableView 
  if <TcxGrid>.ActiveView is TcxGridDbTableView then
  begin
    oTableView:= <TcxGrid>.ActiveView as TcxGridDbTableView;
    oList:=  TStringList.Create();
    try
      for intSelectLoop:= 0 to oTableView.Controller.SelectedRowCount-1 do
      begin
        for intRowLoop:= 0 to oTableView.Controller.SelectedRows[intSelectLoop].ValueCount-1 do
        begin
          strValue:= oTableView.Controller.SelectedRows[intSelectLoop].Values[intRowLoop];
          // Value can be Null
          if VarIsNull(strValue) then
          begin
            strValue:= '';
          end;
          oList.Add(strValue);
        end;
      end;
      Result:=  oList.CommaText;
    finally
      oList.Free;
    end;
  end;
end;
于 2012-03-26T14:27:41.050 回答
1

您需要选定行中所有单元格的文本吗?

for I := 0 to cxGridDBTableView.Controller.SelectedRowCount -1 do
    for J := 0 to cxGridDBTableView.Controller.SelectedRows[I].ValueCount -1 do
      SelectedRowStr := SelectedRowStr + VarToStr(cxGrid1DBTableView1.Controller.SelectedRows[I].Values[J]) + ',';
SelectedRowStr := Copy(SelectedRowStr,1,length(SelectedRowStr)-1);
于 2012-03-26T13:56:19.360 回答
1

网格将有一个 DataControler 后代。您可以循环浏览 DataController 中的项目,并且根据网格的配置方式,DataController 中的项目可以对应于网格中显示的各个“列”。话虽如此,DataController 中的项目仍保留在其中

此代码将让您循环浏览网格中的每一列,并根据 DataController 值构建字符串。

var
    i: Integer;
    DC: TcxCustomDataController;
    s: string;
begin
    s := '';
    DC := <yourgrid>.DataController;
    for i := 0 to <yourgrid>.ColumnCount -1 do begin
        s := s + vartostr(DC.Values[DC.FocusedRecordIndex, <yourgrid>.Columns[i].Index]) + ',';
    end;
    if Length(s) > 0 then
        s := Copy(s,1,Length(s)-1);
end;
于 2012-03-26T14:05:08.743 回答