How to get selected cell text from TcxGrid?

Jacek Kwiecień picture Jacek Kwiecień · Mar 26, 2012 · Viewed 28.2k times · Source

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.

Answer

Ravaut123 picture Ravaut123 · Mar 26, 2012

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;