3

I'm using Devexpress TcxGrid and I'm trying to get selected cell text. My TcxGrid is connected to some kind of DataSource - I think it is DataControler.

My goal is to get the text from cells in the entire row and place it in string divided with comas.

Jacek Kwiecień
  • 12,397
  • 20
  • 85
  • 157
  • If you have the grid source you may want to look at this function procedure TcxCustomGridTableView.CopyToClipboard(ACopyAll: Boolean). The result is on the clipboard but the output is what you are looking for. – Mark Elder Mar 26 '12 at 15:35

3 Answers3

4

If you want values with multiselection and from a TcxGridDbTableView: In my result i don't have a separation between rows.

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;
Ravaut123
  • 2,764
  • 31
  • 46
1

You need the text from all cells in selected rows?

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);
Agustin Seifert
  • 1,938
  • 1
  • 16
  • 29
1

The grid will have a DataControler descendant. You can cycle through the items in the DataController and, depending on how your grid is configured, the items in the DataController can correspond to individual 'columns' shown in your grid. That being said the items in a DataController stay in ther

This code will let you cycle through each column in the grid and build up the string based on the DataController values.

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;
  • 1
    The cxGrid can also end up in situations where the active selection is not a real data row. I use this anytime I am pulling out values. AFocusedRecord := SalesGridView.Controller.FocusedRecord; if (AFocusedRecord <> nil) and (AFocusedRecord.IsData = true) then Work with record here. – Mark Elder Mar 26 '12 at 14:23
  • Good point. There's lots of power with that grid and lots of subtleties as well. –  Mar 26 '12 at 15:25