菜鸟问题,数据库设计中如何用StringGrid替换DBGrid? 很简单,自己控制行和列。因为在 TStringGrid 中每个 Cell 中的值都是 String, 处理对齐方式和字体显示方式都得自己控制。虽然是麻烦点,对于数据量少的用它还是蛮方便的。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 for I := 1 to StringGrid.ColCount//是从1开始for J := 1 to StringGrid.rowconnt//嘻嘻一样StringGrid.Cells[I,J] := FieldByName('..').AsString;编程 1、你先用 TQuery 或 TADOQuery 打开记录集;2、读取数据 with Query1 do While not Eof do begin // 读取记录 // ... ... Next; end;3、对于数据的显示在 StringGrid 的 OnDrawCell 事件中写程序,给你个例子。// 记录表格的 OnDrawCell 事件过程procedure TfrmShowRecord.strgridRecordDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);var strValue: String; intMargin, intHeight, intWidth: Integer;begin with Sender as TStringGrid do begin // 画背景 if (ARow > 0) and (ACol > 0) then begin if ARow mod 2 = 0 then Canvas.Brush.Color := $00F4FFFE else Canvas.Brush.Color := clWhite; Canvas.FillRect(Rect); end; // 计算显示在矩形框中的左上角位置 strValue := Cells[ACol, ARow]; Canvas.Font := Font; intWidth := Canvas.TextWidth(strValue); if (ARow = 0) or (ACol = 0) then begin intMargin := (Rect.Right - Rect.Left - intWidth) div 2; if intMargin < 0 then intMargin := 0; end else begin intMargin := 2; // 根据列的对齐方式设置显示模式 // 把对齐方式存放在数组中与列项相对应。 if FarrColAlignments <> Nil then case FarrColAlignments[ACol - 1] of taLeftJustify: intMargin := 2; taRightJustify: intMargin := Rect.Right - Rect.Left - intWidth - 2; taCenter: intMargin := (Rect.Right - Rect.Left - intWidth) div 2; end; end; intHeight := (Rect.Bottom - Rect.Top - Canvas.TextHeight(strValue)) div 2; // 在矩形框中写值 Canvas.TextRect(Rect, Rect.Left + intMargin, Rect.Top + intHeight, strValue); end;end; webbrowse网页里的文件加载前如何取得其消息 如何取? 版本察看 请问用DELPHI怎么实现查看某IP的7000端口是否开放? 求intraweb8.0.22 for delphi7的序列号 TQuickRep怎样添加到面板中去? 一个Delphi的诡异问题 关于如何拦截主窗口关闭的消息? 请问一个关于ADODataSet1动态参数的问题,顶有分哈 BCB版没人答我,只好来这里问了。关于三层的问题 循环播放wav声音文件(在后台播放) 各位帮忙看看,求大家了,困扰3天了
for J := 1 to StringGrid.rowconnt//嘻嘻一样
StringGrid.Cells[I,J] := FieldByName('..').AsString;
编程
2、读取数据
with Query1 do
While not Eof do
begin
// 读取记录
// ... ...
Next;
end;
3、对于数据的显示在 StringGrid 的 OnDrawCell 事件中写程序,给你个例子。// 记录表格的 OnDrawCell 事件过程
procedure TfrmShowRecord.strgridRecordDrawCell(Sender: TObject; ACol,
ARow: Integer; Rect: TRect; State: TGridDrawState);
var
strValue: String;
intMargin, intHeight, intWidth: Integer;
begin
with Sender as TStringGrid do
begin
// 画背景
if (ARow > 0) and (ACol > 0) then
begin
if ARow mod 2 = 0 then
Canvas.Brush.Color := $00F4FFFE
else
Canvas.Brush.Color := clWhite;
Canvas.FillRect(Rect);
end;
// 计算显示在矩形框中的左上角位置
strValue := Cells[ACol, ARow];
Canvas.Font := Font;
intWidth := Canvas.TextWidth(strValue);
if (ARow = 0) or (ACol = 0) then
begin
intMargin := (Rect.Right - Rect.Left - intWidth) div 2;
if intMargin < 0 then
intMargin := 0;
end
else
begin
intMargin := 2;
// 根据列的对齐方式设置显示模式
// 把对齐方式存放在数组中与列项相对应。
if FarrColAlignments <> Nil then
case FarrColAlignments[ACol - 1] of
taLeftJustify:
intMargin := 2;
taRightJustify:
intMargin := Rect.Right - Rect.Left - intWidth - 2;
taCenter:
intMargin := (Rect.Right - Rect.Left - intWidth) div 2;
end;
end;
intHeight := (Rect.Bottom - Rect.Top - Canvas.TextHeight(strValue)) div 2;
// 在矩形框中写值
Canvas.TextRect(Rect, Rect.Left + intMargin, Rect.Top + intHeight, strValue);
end;
end;