Delphi StringGrid 实例2:1、获取 StringGrid 的行数、列数; 2、给单元赋值

StringGrid 实例2:1、获取 StringGrid 的行数、列数; 2、给单元赋值.

实例2:

本例功能:

1、获取 StringGrid 的行数、列数;

2、给单元赋值.

运行效果图:

//示例代码:

//获取总列数,和总行数 

procedure TForm12.Button1Click(Sender: TObject);

var

     cCount,rCount: Integer;

begin

     cCount := StringGrid1.ColumnCount; {获取总列数}

     rCount := StringGrid1.RowCount; {获取总行数}

     Caption := Format('总列数: %d ,总行数: %d', [cCount, rCount]); {显示在标题}

end;

//给每一个单元格赋值 

     procedure TForm12.Button2Click(Sender: TObject);

var

     c,r: Integer;

begin

     for c := 0 to StringGrid1.ColumnCount - 1 do

        for r := 0 to StringGrid1.RowCount - 1 do

           StringGrid1.Cells[c,r] := Format('%d,%d', [c,r]);

end;

end.

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DelphiStringGrid 控件中,合并列需要以下步骤: 1. 确定要合并的列范围,例如从第二列到第四列。 2. 在 StringGrid 的 OnDrawCell 事件中编写代码,合并指定列范围的单元格。 3. 在合并单元格时,需要注意调整合并后单元格的宽度和对齐方式。 下面是一个示例代码,可以将第二列到第四列的单元格合并为一个单元格: ```delphi procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); begin if (ACol = 1) and (ARow = 0) then begin StringGrid1.Canvas.FillRect(Rect); StringGrid1.Canvas.TextOut(Rect.Left + 2, Rect.Top + 2, '列标题'); StringGrid1.Canvas.FillRect(Rect); end else if (ACol >= 2) and (ACol <= 4) and (ARow = 0) then begin StringGrid1.Canvas.FillRect(Rect); StringGrid1.Canvas.TextOut(Rect.Left + 2, Rect.Top + 2, '合并单元格'); StringGrid1.Canvas.FillRect(Rect); StringGrid1.ColWidths[ACol] := 0; StringGrid1.ColWidths[ACol + 1] := 0; StringGrid1.ColWidths[ACol + 2] := 0; StringGrid1.ColWidths[ACol] := Rect.Right - Rect.Left + 1; StringGrid1.Cells[ACol, ARow] := '合并单元格'; StringGrid1.Cells[ACol + 1, ARow] := ''; StringGrid1.Cells[ACol + 2, ARow] := ''; StringGrid1.Canvas.Font.Style := [fsBold]; StringGrid1.Canvas.TextOut(Rect.Left + 2, Rect.Top + 2, '合并单元格'); StringGrid1.Canvas.Font.Style := []; StringGrid1.Canvas.Brush.Color := clBtnFace; StringGrid1.Canvas.FillRect(Rect); StringGrid1.Canvas.Pen.Color := clGray; StringGrid1.Canvas.MoveTo(Rect.Left, Rect.Top); StringGrid1.Canvas.LineTo(Rect.Right, Rect.Top); StringGrid1.Canvas.MoveTo(Rect.Left, Rect.Bottom); StringGrid1.Canvas.LineTo(Rect.Right, Rect.Bottom); end; end; ``` 这段代码中,我们在 StringGrid 的 OnDrawCell 事件中判断当前单元格是否需要合并,如果是则进行合并操作。其中,我们需要调整合并后单元格的宽度和对齐方式,以保证合并后的单元格显示效果正确。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值