关于stringgrid和combobox的问题,在线等待!(50分)

W

wzqin

Unregistered / Unconfirmed
GUEST, unregistred user!
各位高手:
我想实现在stringgrid每行的第一列加一个combobox, stringgrid记录是从外面
读出来的, 因此行数不确定,请问有什么办法可以实现,最好给出代码,谢谢!
 
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
StringGrid1: TStringGrid;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
iRow: Integer;
ComboBox: TComboBox;
begin
for iRow := 0 to StringGrid1.RowCount - 1 do
begin
ComboBox := TComboBox.Create(Self);
ComboBox.Top := StringGrid1.Top + StringGrid1.DefaultRowHeight * iRow + 1;
ComboBox.Left := StringGrid1.Left;
ComboBox.Width := StringGrid1.DefaultColWidth;
Self.InsertControl(ComboBox);
end;
end;

end.
 
谢谢qianwt的热心答复! 我想再问一个问题:如何取各Combobox的值?
 
你可以用ComboBox数组,不就可以了
 
我是用的数组,但不知为什么StringGrid第一行总取不到,第二三四行都正确,
代码如下,请多指教,我是刚学的:
其中StringGrid.RowCount=5
StringGrid.FixedCols=0
StringGrid.FixedRows=1

unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
StringGrid1: TStringGrid;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
iRow: Integer;
ComboBox: array[0..10] of TComboBox;
begin
for iRow := 1 to StringGrid1.RowCount - 1 do
begin
ComboBox[iRow] := TComboBox.Create(Self);
ComboBox[iRow].Top := StringGrid1.Top + StringGrid1.DefaultRowHeight * iRow + 5;
ComboBox[iRow].Left := StringGrid1.Left+2;
ComboBox[iRow].Width := StringGrid1.DefaultColWidth - 1;
Self.InsertControl(ComboBox[iRow]);
ComboBox[iRow].Items.Add('eeeee');
ComboBox[iRow].Items.Add('uuuuu');
ComboBox[iRow].Items.Add('ppppp');
ComboBox[iRow].Items.Add('ccccc');
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var iRow:integer;
begin
for iRow := 1 to StringGrid1.RowCount-1 do
begin
if (Components[iRow] is TCombobox) then
StringGrid1.Cells[1,iRow] := TComboBox(Components[iRow+1]).Text
end;
end;

end.
 
StringGrid.FixedRows=1
stringgrid的第一行就是标题行
 
顶部