关于Tstringgrid的多行选择问题!!(100分)

K

kk2000

Unregistered / Unconfirmed
GUEST, unregistred user!
我自己测试了一下!发现把Tstringgrid的options 的gorowselect 设置
为true时,只能通过按<shift>+mouse来进行多选!但这样得到的结果是只能
连续的选择多行!
1. 难道不能任意进行选择吗?
2.还有就是怎么样去判断Tstringgrid的行是否被选中了??
 
1、TStringGrid只能选择连续的行。
2、StringGrid1.Selection.Top可以得到选择的最上面一行
StringGrid1.Selection.Bottom 得到选择的最下面一行
 
helloqiner:非常感谢你的回答!
对于第一个问题真的没有办法解决了吗?
她已经困惑我好久了?
 
stringgrid没有这个功能,想用的话,自己在内存中维护一个二维数组,自己写代码吧!
 
使用标准控件是无法完成跨行多选的操作的,如果必须实现的话只能使用第三方控件。
例如:TAdvStringGrid 、Bksgrid等
自己去下吧。
 
请问helloqiner,网址,谢谢
 
如果要任意选择的话,你只能在鼠标相应事件中动态的生成一个Trect,以stringgrid中的selecttion属性来选择。
procedure TForm1.Button1Click(Sender: TObject);
var
myRect: TGridRect;
begin
myRect.Left := 3;
myRect.Top := 1;
myRect.Right := 2;
myRect.Bottom := 4;
DrawGrid1.Selection := myRect;
end;
 
phoenixPower: 谢谢! 这样如果能做到了 任意多选!(我还没有测试),要
是判断是那行被选中的又该怎么判?
 
helloqiner前辈:我现在用的TAdvStringGrid(2.4)发现也没有可以任意选多行的功能,请您再此指教!另外有个小问题,我原来是用TAdvListView,现在想换成TAdvStringGrid,但是TAdvStringGrid行是需要设定的,不像TAdvListView,只要把GridFulls=True,则无论是否有数据都可以有网格,请问TAdvStringGrid是否有通过属性来进行设定呢?
 
你可以自己写代码啊
比如:选择一航就变个颜色
同时用数组记录下已选行的ROW值
//**单选着色
if (stgr_mzsjhx.Cells[1,ARow]=s2)and(stgr_mzsjhx.Cells[3,ARow]=s3) then
begin
stgr_mzsjhx.canvas.Brush.Color:=$0080ff80;
stgr_mzsjhx.Canvas.Font:=f_mzsjhx.Font;
stgr_mzsjhx.Canvas.Font.Color:=clblack;
stgr_mzsjhx.Canvas.TextRect(Rect,Rect.left+2,Rect.top+2,stgr_mzsjhx.Cells[ACol,ARow])
end;
 
用集合或数组等数据结构保存选中信息,自己通过画颜色的方式来实现吧
 
判断选择哪一行只是指指向那一行的指针而已,你拭一拭,可以做到的!至于用记录还是数组那就随你的喜好啦!
 
PhoenixPower:我按你的方法,可是还是连续的选择,不能间断选择?
我是这样写的:
var
myRect: TGridRect;
begin
myRect.Left :=1;
myRect.Top :=stringgrid1.Row;
myRect.Right :=12;
myRect.Bottom := 1;
stringgrid1.Selection:= myRect;
end;

如果把merect.Bottom:=stringgrid1.row;
又是只能选择单行了!
真的不懂怎么写了!
 
stringgrid1.row为当前的焦点所在的行的index,如果只选择了一行,即为当前所选行的index;如果选择了多行,则为鼠标开始按下处的行。
上面的代码只能选择一个范围,不能跨行选择。
stringgrid没有提供跨行选择功能。可以自己画配合一个索引数组,不难。在OnDrawCell中画。
 
var
A: array[0..500] of integer;
I:integer=0;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
J:integer;
begin
if (Arow<>0) and (Acol<>0) then
begin
for J:= 0 to (I-1) do
begin
stringgrid1.Canvas.Brush.Color:=clAqua;
stringgrid1.Canvas.TextRect(rect,rect.Left,rect.Top,stringgrid1.Cells[Acol,A[J]]);
end;
end;
end;
但效果不理想呀!怎么改进呢?
 
你拭一下这样吧!把你不需要选择的记录用sql语句在数据源中虑调,然后把整个stringgrid中需要的记录选中。
如果这样还不行的化,我也没折了!
 
现在我有一个想法:是这样的在Tstringgrid真加多一列,用来标志
用户选择的列,然后根据这列的标志去重画Tstringgrid,但是
我想隐藏该标志列!不知道可不可以隐藏起来!
又是怎么隐藏呢?
 
To kk2000:
你上面的代码有点问题,执行了不少冗余操作,而且可能会有重画错误,你可以在选择多列后切换到别的程序,把该窗体覆盖,再切换回来看看。
稍作修改就可以了:
if (Arow<>0) and (Acol<>0) then
begin
for J:= 0 to (I-1) do
if (ARow = A[j]) then
begin
stringgrid1.Canvas.Brush.Color:=clAqua;
stringgrid1.Canvas.TextRect(rect,rect.Left,rect.Top,stringgrid1.Cells[Acol,ARow]);
end;
end;
 
设置标志列是个方法,隐藏列也很简单,比如把最后一列设为标志列,在创建时加入下面这句即可:
StringGrid1.ColWidths[StringGrid1.ColCount - 1 ] := -StringGrid1.GridLineWidth;
 
多谢各位了!现在已经得到很好的解决!
 
顶部