AdvStringGrid常用操作
AdvStringGrid1.Options:=[goEditing]; //设置单元格是否可编辑
AdvStringGrid1.MouseActions.DirectEdit:=True; //设置点击单元格即进入编辑状态
procedure TForm1.AdvStringGrid1CanEditCell(Sender: TObject; ARow, ACol: Integer; var CanEdit: Boolean);
var
i: Integer;
begin
for i := 0 to AdvStringGrid1.ColCount do
begin
CanEdit := False;
end;
if ACol = 1 then
CanEdit := True;
if ACol = 7 then
CanEdit := True;
end;
//设置单元格对齐方式 procedure TForm1.AdvStringGrid1GetAlignment(Sender: TObject; ARow, ACol: Integer; var HAlign: TAlignment; var VAlign: TVAlignment); begin if ACol = 1 then begin HAlign := taCenter; //TAlignment = (taLeftJustify, taRightJustify, taCenter); VAlign := vtaCenter; //TVAlignment = (vtaTop,vtaCenter,vtaBottom); end; end;
//添加CheckBox procedure TForm1.FormCreate(Sender: TObject); var i: Integer; flag: Boolean; begin //添加CheckBox for i := 1 to AdvStringGrid1.RowCount - 1 do begin AdvStringGrid1.AddCheckBox(1, i, False, false); end; //判断CheckBox是否选中 for i := 1 to AdvStringGrid1.RowCount - 1 do begin AdvStringGrid1.GetCheckBoxState(1, i, flag); if flag then begin //如果选中CheckBox进行操作 end; end; end;
//AdvStringGrid1.Options:=[goEditing]; //设置单元格是否可编辑 //AdvStringGrid1.MouseActions.DirectEdit := True; //设置点击单元格即进入编辑状态 //为 AdvStringGrid1增加下拉列表选择框 procedure TForm1.AdvStringGrid1GetEditorType(Sender: TObject; ACol, ARow: Integer; var AEditor: TEditorType); var sqlStr: string; begin if ACol = 2 then //设置特定列为下拉列表框类型 begin sqlstr := 'select * from dictfeeitem'; with ADOQuery1 do begin Close; SQL.Clear; SQL.Add(sqlStr); Open; end; if ADOQuery1.RecordCount > 0 then begin ADOQuery1.First; aEditor := edComboList; //设置编辑框为下拉列表 AdvStringGrid1.ClearComboString; //清空下拉列表内容 while not ADOQuery1.Eof do begin AdvStringGrid1.AddComboString(ADOQuery1.FieldByName('feeitem').AsString); //增加下拉列表框内容 ADOQuery1.Next; end; end; end; end;
//滚动条到最后一条记录
SendMessage(AdvStringGrid1.Handle,WM_VSCROLL,SB_BOTTOM,0);
//根据内容改变单元格的颜色 procedure TForm1.Button1Click(Sender: TObject); var i: Integer; begin AdvStringGrid1.RowCount := 7; AdvStringGrid1.Cells[1, 1] := '-11'; AdvStringGrid1.Cells[1, 2] := '1'; AdvStringGrid1.Cells[1, 3] := '19'; AdvStringGrid1.Cells[1, 4] := '-8'; AdvStringGrid1.Cells[1, 5] := '0'; AdvStringGrid1.Cells[1, 6] := '11'; for i := 1 to AdvStringGrid1.RowCount - 1 do begin if StrToInt(AdvStringGrid1.Cells[1, i]) <= 0 then AdvStringGrid1.FontColors[1, i] := clRed else AdvStringGrid1.FontColors[1, i] := clBlack; end; end;
本文来自博客园,作者:liessay,转载请注明原文链接:https://www.cnblogs.com/liessay/p/5691175.html