[Gambas-user] gridview autoresize columns width by code

JUHASZ Robert robert1juhasz at ...626...
Wed Jun 23 15:44:04 CEST 2010


Hello Ricardo,

I use this function to display a result in a TableView. I just copy it
as is, you may find out how do I adjust the column width. Maybe not the
simplest but works well.

For the Boolean I show the picture of a checkbox, I attached if you
wanna try the code then add the pictures to your project or comment out
the part uses them.

Hope this helps.

Robi


'-----------------------------------------------------------------------------------------------
PUBLIC FUNCTION fillTV(tv AS TableView, rs AS Result, OPTIONAL
hideFirstCol AS Boolean = FALSE, OPTIONAL ignoreLastCols AS Integer = 0)
AS Boolean
'to fill the tv tableview with the data of the rs result

  DIM k, j AS Long
  DIM iC AS Integer = 0
  DIM c AS Long[2]
  DIM minW AS Integer, maxW AS Integer
  DIM colW AS NEW Integer[]
  DIM minH AS Integer, maxH AS Integer
  DIM colH AS NEW Integer[]
  
  DIM picF AS Picture = Picture.Load("picF.png")
  DIM picT AS Picture = Picture.Load("picT.png")
  
  minW = tv.Font.Width("X") * 5
  maxW = minW * 30
  
  minH = tv.Font.Height("H")
  maxH = minH * 10
    
  c[0] = Color.White
  c[1] = Color.LightBackground
  
  tv.Clear
  tv.Rows.count = rs.Count 
  tv.Columns.Count = rs.Fields.Count - ignoreLastCols
  tv.header = 3
  
  FOR k = 0 TO tv.columns.count - 1 
    tv.Columns[k].Text = rs.Fields[k].name
    colW.Add(tv.Font.Width(tv.Columns[k].Text))
  NEXT
   
  FOR j = 0 TO rs.count - 1
  colH.Add(minH)
    FOR k = 0 TO tv.Columns.Count - 1
      rs.MoveTo(j)
      IF rs.Fields[k].Type = db.Boolean THEN 
        tv[j, k].Picture = IIf(rs[rs.Fields[k].name], picT, picF)
      ELSE IF rs[rs.Fields[k].name] = "00/00/00" THEN 
        tv[j, k].text = ""
      ELSE
        tv[j, k].text = rs[rs.Fields[k].name]
      ENDIF
      tv[j, k].BackColor = c[iC]
      IF colW[k] < tv.Font.Width(tv[j, k].text) THEN colW[k] =
tv.Font.Width(tv[j, k].text)
      IF colH[j] < tv.Font.Height(tv[j, k].text) THEN colH[j] =
tv.Font.Height(tv[j, k].text)
    NEXT
    ic = 1 - ic
  NEXT
  
  FOR j = 0 TO tv.Rows.Count - 1
      tv.Rows[j].H = Max(minH, Min(colH[j], maxH)) + 7
  NEXT
  
  FOR k = 0 TO tv.Columns.Count - 1
      tv.Columns[k].W = Max(minW, Min(colW[k], maxW)) + 15
  NEXT
  IF hideFirstCol THEN 
    tv.Columns[0].W = 0
    tv.Columns[0].Resizable = FALSE
  ENDIF 
  tv.Refresh
  RETURN TRUE
  
  CATCH 
  RETURN FALSE
END
'-----------------------------------------------------------------------------------------------

-----Original Message-----
From: Ricardo Díaz Martín <oceanosoftlapalma at ...626...>
Reply-to: mailing list for gambas users
<gambas-user at lists.sourceforge.net>
To: mailing list for gambas users <gambas-user at lists.sourceforge.net>
Subject: [Gambas-user] gridview autoresize columns width by code
Date: Wed, 23 Jun 2010 12:24:49 +0200


Hi all,

I wonder if there is a way to auto-resize a column in a grid view by code
(the same when you double-cliked on run time to auto-size the width).

I always save columns width in database and when form is loading set values
for columns width. But now I need in one form to do this like if user
double-clicked on border of columns.

Thanks in advance,
Ricardo
------------------------------------------------------------------------------
ThinkGeek and WIRED's GeekDad team up for the Ultimate 
GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the 
lucky parental unit.  See the prize list and enter to win: 
http://p.sf.net/sfu/thinkgeek-promo
_______________________________________________
Gambas-user mailing list
Gambas-user at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/gambas-user
-------------- next part --------------
A non-text attachment was scrubbed...
Name: picT.png
Type: image/png
Size: 262 bytes
Desc: not available
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20100623/dce88aa8/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: picF.png
Type: image/png
Size: 157 bytes
Desc: not available
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20100623/dce88aa8/attachment-0001.png>


More information about the User mailing list