我正在使用Devexpress TcxGrid,並試圖獲取選定的單元格文本。我的TcxGrid連接到某種數據源 - 我認爲它是DataControler。如何從TcxGrid中獲取選定的單元格文本?
我的目標是從整個行中的單元格中獲取文本,並將其置於用逗號分隔的字符串中。
我正在使用Devexpress TcxGrid,並試圖獲取選定的單元格文本。我的TcxGrid連接到某種數據源 - 我認爲它是DataControler。如何從TcxGrid中獲取選定的單元格文本?
我的目標是從整個行中的單元格中獲取文本,並將其置於用逗號分隔的字符串中。
如果你想多選,並從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;
您需要選定行中所有單元格的文本?
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);
該網格將有一個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;
cxGrid也可能在活動選擇不是真正的數據行的情況下結束。任何時候我都會用這個價值。 AFocusedRecord:= SalesGridView.Controller.FocusedRecord; if(AFocusedRecord <> nil)和 (AFocusedRecord.IsData = true)然後在這裏使用記錄。 – 2012-03-26 14:23:49
好點。這個網格有很多功能,還有很多細節。 – 2012-03-26 15:25:55
如果你有網格源,你可能想看看這個函數過程TcxCustomGridTableView.CopyToClipboard(ACopyAll:Boolean)。結果在剪貼板上,但輸出結果就是您要查找的內容。 – 2012-03-26 15:35:08