1.我有一个表如下(表名:message,用dataset连接数据库)  name    sex    age
  a       man    18
  b       women  20
  c       man    18如何把他显示在stringgrid中去呢?
如果我只显示name=a的资料呢?(帮我写代码出来,好不好,我没有用过呢)2.如何调节列的宽度呢?(能不能根据数据库的字节大小来自动调节呢?)3.如果我有很多资料,那么Stringgrid的行数会不会自动增加的?请各位大哥帮帮忙了,谢谢了

解决方案 »

  1.   

    1
    StringGrid要自己填进去的,你爱填哪列填哪列,你用的可能是DBGrid吧.
    2
      StringGrid1.RowHeights[0] := 20;
      StringGrid1.ColWidths[0] := 100;
    3
     不会,要你自己控制行数,DBGrid会.
      StringGrid1.RowCount := 1000;
      

  2.   

    with StringGrid1 do
      begin
        ColCount :=4;
        RowCount :=2;
        FixedCols :=3;
        ColWidths[0] :=0;
        ColWidths[1] :=1;
        ColWidths[2] :=180;
        ColWidths[3] :=400;
        Cells[0,0] :='name';
        Cells[1,0] :='sex;
        Cells[2,0] :='age;
      end;   While Not Query.Eof do
        Begin
          StringGrid1.Cells[0,StringGrid1.RowCount-1] :=trim(Query.FieldByName('FIELD_TYPE').AsString);
          StringGrid1.Cells[1,StringGrid1.RowCount-1] :=trim(Query.FieldByName('FIELD_TYPE').AsString);
          .....
          Query.Next;
        end;
      

  3.   

    1.
    var
     i,j: integer
    with adodataset do
    begin
      close;
      CommonText:= 'select * from message where name = ''a''';
      active := True;
      stringgrid1.Clear;
      First;
      i := 1;
      while not Eof do
      begin
        for j := 0 to FieldCount - 1 do
        begin
           stringgrid1.Rows[i].Add(Fields.Fields[j].AsString);
        end;
        i := i + 1;
        Next;
      end;
        //显示标题
      for i := 0 to FieldCount - 1 do
      begin
        stringgrid1.Rows[0].Add(Fields.Fields[i].FieldName);
      end;  stringgrid1.AutoSize := True;//自动列宽
    end;
      

  4.   

    正确地设置StringGrid列宽而不截断任何一个文字
       方法是在对StringGrid填充完文本串后调用SetOptimalGridCellWidth过程。    -----------程序片断-------------------------------------------------       (*   $Header$   Module Name : General\BSGrids.pas   Main Program : Several.   Description : StringGrid support functions.       03/21/2000 enhanced by William Sorensen   *)     unit BSGrids;     interface     uses   Grids;     type   TExcludeColumns = set of 0..255;     procedure SetOptimalGridCellWidth(sg: TStringGrid;   ExcludeColumns: TExcludeColumns);   // Sets column widths of a StringGrid to avoid truncation of text.   // Fill grid with desired text strings first.   // If a column contains no text, DefaultColWidth will be used.   // Pass [] for ExcludeColumns to process all columns, including Fixed.   // Columns whose numbers (0-based) are specified in ExcludeColumns will not   // have their widths adjusted.     implementation     uses   Math; // we need the Max function     procedure SetOptimalGridCellWidth(sg: TStringGrid;   ExcludeColumns: TExcludeColumns);   var   i : Integer;   j : Integer;   max_width : Integer;   begin   with sg do   begin   // If the grid's Paint method hasn't been called yet,   // the grid's canvas won't use the right font for TextWidth.   // (TCustomGrid.Paint normally sets this, under DrawCells.)   Canvas.Font.Assign(Font);     for i := 0 to (ColCount - 1) do   begin   if i in ExcludeColumns then   Continue;     max_width := 0;     // Search for the maximal Text width of the current column.     for j := 0 to (RowCount - 1) do   max_width := Math.Max(max_width,Canvas.TextWidth(Cells[i,j]));     // The hardcode of 4 is based on twice the offset from the left   // margin in TStringGrid.DrawCell. GridLineWidth is not relevant.     if max_width > 0 then   ColWidths[i] := max_width + 4   else   ColWidths[i] := DefaultColWidth;     end; { for }   end;   end;     end. 
      

  5.   

    谢谢各位大哥了,不过我的stringgrid1好像没有autosize这个方法的,怎么回事呢?
      

  6.   

    谢谢了。问题已经解决了,就是autosize还有点问题。:)另外,各位大哥,可以问多一个问题吗?
    就是我用了一个DataTimepicker,比如读出得数据都是2004-10-28,我怎么可以得到200410这种形式得日期呢?感激不尽
      

  7.   

    在DataTimepicker的Format属性中填yyyyMM,
      

  8.   

    关于stringgrid还有点问题呢:
    比如有这样一个表:name    sex    age    height   study
      a       man    18     20       good
      b       women  20     18       bad
      c       man    18     19       cool
      a       man    21     22       very我这样写一个sql语句:
    select name,sex,sum(age) as age,sum(height) as height from message  where name='a'但需要作出这样一个判断,当sum(age) >sum(height) ,将sum(age) -sum(height) ,得到得差额插入到name='a'的那一行,age的那一列,而置height=0。如果sum(age) <sum(height) ,则将差额插入到name='a'的那一行,height的那一列,而置age=0.即是:
    ——————————————————————————————————————
    name      sex        age    height
    -------------------------------------------
    a         man         0      3
    -----------------------------------------------------------------------求求各位了,这个stringgrid对我来说太难了,:)