如何根据数据库表中各字段的宽度动态修改tstringgrid中各列的宽度? 有时候读出来的值超出列宽度,又不能手工拉大宽度,有没有办法解决。

解决方案 »

  1.   

    with StringGrid do
          begin
          Color := clInfobk;
          DefaultRowHeight := 22;
          RowCount := 2;
          ColCount := 7;
          ColWidths[0] := 20;
          ColWidths[1] := 80;
          ColWidths[2] := 60;
          ColWidths[3] :=120;
          ColWidths[4] := 50;
          ColWidths[5] := 80;
          ColWidths[6] :=400;
          Cells[0,0] := '';
          Cells[1,0] := 'QQ号码';
          Cells[2,0] := '去留状态';
          Cells[3,0] := 'QQ昵称';
          Cells[4,0] := 'QQ性别';
          Cells[5,0] := '最近发言时间';
          Cells[6,0] := '信息更新情况';
          FixedColor := clMoneyGreen;
          FixedRows  := 1;
          FixedCols  := 1;
          Font.Color := clWindowText;//clNavy;
          end;这是从我一个程序里复制的一段
      

  2.   

    ColWidths[列index] := 列宽integer值;//上面就这个是你要的
      

  3.   

    如何根据数据库表中各字段的宽度动态修改tstringgrid中各列的宽度? 
    有时候读出来的值超出列宽度,又不能手工拉大宽度,有没有办法解决。
    ---------------------------------------
    自动换行显示。请参考:
    http://community.csdn.net/Expert/topic/5236/5236146.xml?temp=.4189875
      

  4.   

    StringGrid的设置比DBGrid的设置还要来得简单。如仍有问题。我等会给你写个StringGrid的范例
      

  5.   

    lihuasoft兄,我是指自动调整宽度,如果我知道,那我直接预先拉好不就行了;
    老冯,可不可以写代码实现鼠标停留时自动显示出来,这样的代码会不会少一些?等你的回复..
      

  6.   


    Q:  1、可不可以写代码实现鼠标停留时自动显示出来
    --------------------------------------------
    A:  这个我写了一个“点击StringGrid单元格时用Hint显示单元格内容”的代码,附后。
    Q:  2、lihuasoft兄,我是指自动调整宽度
    -------------------------------------------
    A:  这个,我们一起等老冯写好后,共同学习吧。
    { 代码功能:单击StringGrid单元格时,在 Hint 里显示此格Text }
    //Button1 : 往StringGrid里添加测试文本procedure TForm1.Button1Click(Sender: TObject);
    begin
       with StringGrid1 do
         begin
         Cells[1,1] := '号称礼仪(义)之邦的某个古国和她的民族正在面临一场礼仪(义)危机';
         Cells[1,2] := '中华人民共和国山东省';
         Cells[2,1] := '日本国静冈县';
         Cells[2,2] := '以上是测试数据';
         end;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      ShowHint := True;
      StringGrid1.Options := [goFixedHorzLine,goFixedVertLine,goVertLine,goHorzLine,goRangeSelect,goEditing,goAlwaysShowEditor];
    end;procedure TForm1.StringGrid1MouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    var
      HWND : integer;
    begin
      HWND := FindWindowEx(StringGrid1.Handle,0,Pchar('TInplaceEdit'),nil);
      FindControl(HWND).Hint := TInPlaceEdit(FindControl(HWND)).Text;
    end;
      

  7.   

    我继续吧:    想了另一个办法,根据单击的格子的文本长度,动态调整该列宽度。可能有点笨。procedure TForm1.Button1Click(Sender: TObject);//添加测试文本
    begin
       with StringGrid1 do
         begin
         Cells[1,1] := '号称礼仪(义)之邦的某个古国和她的民族正在面临一场礼仪(义)危机';
         Cells[1,2] := '中华人民共和国山东省';
         Cells[2,1] := '日本国静冈县';
         Cells[2,2] := '测试数据';
         end;
    end;procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
      ARow: Integer; var CanSelect: Boolean);
    var
       iTemp : Integer;
    begin
      with StringGrid1 do
         if Length(Trim( Cells[ACol,ARow] )) > 1 {为了应付某格空值} then
            ColWidths[ACol] := Font.Size * Length(Trim( Cells[ACol,ARow] ));
    end;
      

  8.   

    如何根据数据库表中各字段的宽度动态修改tstringgrid中各列的宽度?
    ======
    procedure TFormDemo.StringGridDrawCell(Sender: TObject; ACol,
      ARow: Integer; Rect: TRect; State: TGridDrawState);
    var
      WCell: Integer;
      WCol: Integer;
      SCell: string;
    begin
      SCell := StringGrid.Cells[ACol, ARow];
      WCol := StringGrid.ColWidths[ACol];
      WCell := StringGrid.Canvas.TextWidth(SCell);
      if WCell > WCol then
        StringGrid.ColWidths[ACol] := WCell;
      //下面的语句是重画,使之水平和垂直都居中(你也可以不重画, 保持原有对齐状态)
      StringGrid.Canvas.FillRect(Rect);
      DrawText(StringGrid.Canvas.Handle, PChar(SCell), Length(SCell), Rect, DT_Center or DT_VCenter or DT_SingleLine);
    end;
      

  9.   

    有时候读出来的值超出列宽度,又不能手工拉大宽度,有没有办法解决。
    ====
    自动换行procedure TFormDemo.StringGridDrawCell(Sender: TObject; ACol,
      ARow: Integer; Rect: TRect; State: TGridDrawState);
    var
      HCell: Integer;
      HRow: Integer;
      SCell: string;
    begin
      SCell := StringGrid.Cells[ACol, ARow];
      HRow := StringGrid.RowHeights[ARow];
      StringGrid.Canvas.FillRect(Rect);
      HCell := DrawText(StringGrid.Canvas.Handle, PChar(SCell), Length(SCell), Rect, DT_Center or DT_VCenter or DT_WORDBREAK );
      if HCell > HRow then
        StringGrid.RowHeights[ARow] := HCell;
    end;
      

  10.   

    //取得每个字符的平均宽度
    function AvgCharWidth(FHandle:Hwnd): Word;
    var
      Metrics: TTextMetric;
    begin
      GetTextMetrics(FHandle, Metrics);
      Result := Metrics.tmAveCharWidth;
    end;
    //计算列宽代码
    procedure TForm1.BitBtn1Click(Sender: TObject);
    var
      maxFieldValue_arr:array of integer;
      I:integer;
      StringGird_FontWidth,SelectFieldValue_Len:integer;
    begin
       StringGird_FontWidth:=AvgCharWidth(StringGrid1.Canvas.Handle);
       with table1 do
       begin
         open;
          setlength(maxFieldValue_arr,Fields.Count);
          self.StringGrid1.ColCount:=Fields.Count;
          self.StringGrid1.RowCount:=recordcount;
          while (not eof) do
          begin
            For I:=0 to Fields.Count-1 do
            begin
              SelectFieldValue_Len:=StringGird_FontWidth*Length(Fields[I].AsString);
              if SelectFieldValue_Len>maxFieldValue_arr[I] then
              begin
                 maxFieldValue_arr[I]:=SelectFieldValue_Len*2;
                 StringGrid1.ColWidths[I]:=SelectFieldValue_Len*2;
              end;
              if not Fields[I].IsNull then
              StringGrid1.Cells[I,recNo]:=Fields[I].AsString;
            end;
            next;
          end;
         close;
       end;
    end;
      

  11.   

    不好意思,考虑到单元格还有边框的宽度的问题,你最好呢把下面的计算列宽的代码调整一点
     if SelectFieldValue_Len>maxFieldValue_arr[I] then
              begin
                 maxFieldValue_arr[I]:=SelectFieldValue_Len*2+2;
                 StringGrid1.ColWidths[I]:=SelectFieldValue_Len*2+2;
              end;