关于cxGrid多选 和 RzListBox取值的问题 (最迟12月9日结贴)

lzg827 2005-12-08 07:29:38
1)我用的是cxGrid
假设在数据集中每行有个字段为ID
想同时选中多行,并把选中的每一行的ID字段依次存在一个动态数组里去

2)用的是RzListBox
每一行的item用记录类型与其他数据关联起来
如:
PMyItem=^Tem_Item;
Tem_Item=record
ID: string;
Name:string;
end;

var
p:PMyItem

…………


New(P);
P.ID:=Need_ID;
P.Name:=Need_Name; //Need_ID,Need_Name是从别的地方传过来的值

RzListBox.Items.AddObject(p.Name,TObject(P));

现在已经往RzListBox中加入数据了
怎样依次取出每个item关联的ID,并保存到一个动态数组中去?
恳求各位路过的大侠,随手留下珍贵的代码。
小弟献上微薄的分数和绵绵的敬意
...全文
320 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
herman~~ 2005-12-09
  • 打赏
  • 举报
回复
学习
xiaocuo_zrf 2005-12-09
  • 打赏
  • 举报
回复
cxGrid的多选我一般都是用这个方法:
//--------------- .Pas文件
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxEdit,
DB, cxDBData, DBTables, cxGridLevel, cxGridCustomTableView,
cxGridTableView, cxGridDBTableView, cxClasses, cxControls,
cxGridCustomView, cxGrid, cxLookAndFeelPainters, cxGridRows, cxContainer,
cxCheckBox, cxDataStorage;

type
TForm1 = class(TForm)
cxGrid1: TcxGrid;
cxGrid1DBTableView1: TcxGridDBTableView;
cxGrid1DBTableView1CustNo: TcxGridDBColumn;
cxGrid1DBTableView1Company: TcxGridDBColumn;
cxGrid1DBTableView1Addr1: TcxGridDBColumn;
cxGrid1DBTableView1City: TcxGridDBColumn;
cxGrid1DBTableView1State: TcxGridDBColumn;
cxGrid1DBTableView1Country: TcxGridDBColumn;
cxGrid1Level1: TcxGridLevel;
Table1: TTable;
DataSource1: TDataSource;
cxGrid1DBTableView1DBColumn1: TcxGridDBColumn;
procedure FormCreate(Sender: TObject);
procedure cxGrid1DBTableView1MouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure cxGrid1DBTableView1DBColumn1CustomDrawCell(
Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure cxGrid1DBTableView1CustomDrawCell(
Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
private
AList: TList;
function CheckList(ARecord: TcxCustomGridRecord): Boolean;
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
AList := TList.Create;
end;

function TForm1.CheckList(ARecord: TcxCustomGridRecord): Boolean;
begin
Result := AList.IndexOf(Pointer(ARecord.RecordIndex)) <> - 1;
end;

procedure TForm1.cxGrid1DBTableView1MouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
AHitTest: TcxCustomGridHitTest;
AGridRecord: TcxCustomGridRecord;
begin
if Sender is TcxGridSite then
begin
with TcxGridSite(Sender).GridView do
AHitTest := ViewInfo.GetHitTest(X, Y);
if (AHitTest.HitTestCode = htCell) and (TcxGridDBColumn(TcxGridRecordCellHitTest(AHitTest).Item).DataBinding.FieldName = '') then
AGridRecord := TcxGridRecordCellHitTest(AHitTest).GridRecord
else
Exit;
end;
if (AGridRecord <> nil) then
if CheckList(AGridRecord) then
AList.Remove(Pointer(AGridRecord.RecordIndex))
else
AList.Add(Pointer(AGridRecord.RecordIndex));
end;

procedure TForm1.cxGrid1DBTableView1DBColumn1CustomDrawCell(
Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
if AViewInfo.EditViewInfo is TcxCustomCheckBoxViewInfo then
TcxCustomCheckBoxViewInfo(AViewInfo.EditViewInfo).State
:= TcxCheckBoxState(CheckList(AViewInfo.GridRecord));
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
AList.Free;
end;

procedure TForm1.cxGrid1DBTableView1CustomDrawCell(
Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
with AViewInfo do
if CheckList(GridRecord) then
ACanvas.Brush.Color := clHighlight
else
ACanvas.Brush.Color := clWindow;
ACanvas.Font.Color := clBlack;
end;

end.
//---------------cxGrid的Dfm属性
object cxGrid1: TcxGrid
Left = 0
Top = 0
Width = 688
Height = 446
Align = alClient
TabOrder = 0
object cxGrid1DBTableView1: TcxGridDBTableView
OnMouseDown = cxGrid1DBTableView1MouseDown
NavigatorButtons.ConfirmDelete = False
OnCustomDrawCell = cxGrid1DBTableView1CustomDrawCell
DataController.DataSource = DataSource1
DataController.DetailKeyFieldNames = 'CustNo'
DataController.KeyFieldNames = 'CustNo'
DataController.Summary.DefaultGroupSummaryItems = <>
DataController.Summary.FooterSummaryItems = <>
DataController.Summary.SummaryGroups = <>
OptionsSelection.MultiSelect = True
object cxGrid1DBTableView1CustNo: TcxGridDBColumn
DataBinding.FieldName = 'CustNo'
end
object cxGrid1DBTableView1DBColumn1: TcxGridDBColumn
Caption = 'CheckColumn'
PropertiesClassName = 'TcxCheckBoxProperties'
Properties.DisplayUnchecked = 'False'
OnCustomDrawCell = cxGrid1DBTableView1DBColumn1CustomDrawCell
Options.Editing = False
Options.Filtering = False
Options.IncSearch = False
Options.ShowEditButtons = isebNever
Options.Grouping = False
Options.Sorting = False
Width = 123
end
object cxGrid1DBTableView1Company: TcxGridDBColumn
DataBinding.FieldName = 'Company'
end
object cxGrid1DBTableView1Addr1: TcxGridDBColumn
DataBinding.FieldName = 'Addr1'
end
object cxGrid1DBTableView1City: TcxGridDBColumn
DataBinding.FieldName = 'City'
end
object cxGrid1DBTableView1State: TcxGridDBColumn
DataBinding.FieldName = 'State'
end
object cxGrid1DBTableView1Country: TcxGridDBColumn
DataBinding.FieldName = 'Country'
end
end
object cxGrid1Level1: TcxGridLevel
GridView = cxGrid1DBTableView1
end
end
阿三 2005-12-09
  • 打赏
  • 举报
回复
如果你用CxGrid那么可以将数据集这个字段的ID隐藏起来。
选中多行:
for i:=0 to TVGrid1.Controller.SelectedRowCount-1 do
begin
//存入数组
end;
hanlin2004 2005-12-09
  • 打赏
  • 举报
回复
我一般是这样取cxGrid选中的纪录的值的:

function TFrameAccount.GetSelectedID:variant;
var
AccID: array of string;
i, n: Integer;
Index: Integer;
begin
n := cxGrid1DBTableView1.DataController.GetSelectedCount;

for i:=0 to n - 1 do
begin
Index := cxGrid1DBTableView1.DataController.GetSelectedRowIndex(i);
setlength(AccID, length(AccID) + 1);
AccID[High(AccID)] :=
cxGrid1DBTableView1.DataController.GetRowValue(
cxGrid1DBTableView1.DataController.GetRowInfo(Index)
,0); //这里的0是Grid中列的索引,0表示第一个列绑定的字段
end;
result := AccID;
end;
僵哥 2005-12-08
  • 打赏
  • 举报
回复
更正一下:
ListBox1.Items.Objects[II-1]:=nil; //此处的II-1应当为II

僵哥 2005-12-08
  • 打赏
  • 举报
回复
下面的已经加入注释:
-------------------------
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type

PMyItem=^Tem_Item;
//声明一个结构(开始)
Tem_Item=record
ID: string;
Name:string;
end;
//声明一个结构(结束)

TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
Edit5: TEdit;
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

//计数器
i:integer;
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
//结构体变量声明
p:PMyItem;
begin
//分配内存
new(p);
//模拟一个ID
p.ID:=IntToStr(i);
//模拟一个名称
p.Name:='Name'+IntToStr(i);
//添加入ListBox的Object列表当中进行管理
ListBox1.Items.AddObject(p.Name,TObject(p));
//计数器自增
inc(i);

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
//初始化计数器为1
i:=1;
end;

procedure TForm1.FormDestroy(Sender: TObject);
var
//用于环境用的自增/自减计数变量,此处做为下标使用
II:Integer;
begin
//取得分配出来的结构体数量
II:=ListBox1.Items.Count-1;

//从最后一个起删除掉结构体(释放内存)
while II>=0 do
begin
//释放内存
Dispose(PMyItem(listbox1.Items.Objects[II]));
//预置空指针
ListBox1.Items.Objects[II-1]:=nil;
//下标前移
dec(II);
end;
end;

procedure TForm1.ListBox1Click(Sender: TObject);
begin
//模拟取得ID
Edit5.Text:=PMyItem(listbox1.Items.Objects[listbox1.ItemIndex]).ID;
end;

end.
僵哥 2005-12-08
  • 打赏
  • 举报
回复
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
PMyItem=^Tem_Item;
Tem_Item=record
ID: string;
Name:string;
end;

TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
Edit5: TEdit;
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
i:integer;
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
p:PMyItem;
begin
new(p);
p.ID:=IntToStr(i);
p.Name:='Name'+IntToStr(i);
ListBox1.Items.AddObject(p.Name,TObject(p));
inc(i);

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
i:=1;
end;

procedure TForm1.FormDestroy(Sender: TObject);
var
II:Integer;
p:PMyItem;
begin
II:=ListBox1.Items.Count;
while II>0 do
begin
p:=PMyItem(listbox1.Items.Objects[II-1]);
Dispose(p);
dec(II);
end;
end;

procedure TForm1.ListBox1Click(Sender: TObject);
begin
Edit5.Text:=PMyItem(listbox1.Items.Objects[listbox1.ItemIndex]).ID;
end;

end.
僵哥 2005-12-08
  • 打赏
  • 举报
回复
//RzListBox.Items.AddObject(p.Name,TObject(P));

procedure TForm1.Button1Click(Sender: TObject);
begin
ListBox1.Items.AddObject(Edit1.Text,TObject(Edit1));
ListBox1.Items.AddObject(Edit2.Text,TObject(Edit2));
ListBox1.Items.AddObject(Edit3.Text,TObject(Edit3));
ListBox1.Items.AddObject(Edit4.Text,TObject(Edit4));
end;

//取出ID,下面用取出Object代替,并用TEdit类代替楼主的PMyItem
procedure TForm1.ListBox1Click(Sender: TObject);
begin
Edit5.Text:=TEdit(listbox1.Items.Objects[listbox1.ItemIndex]).Text;
end;
lzg827 2005-12-08
  • 打赏
  • 举报
回复
分不够再加

狂加
lzg827 2005-12-08
  • 打赏
  • 举报
回复
大侠,救救我吧

5,927

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧