Gridview column header alignment

Post

Posted
Rating:
#1 (In Topic #343)
Regular
bill-lancaster is in the usergroup ‘Regular’
GridView.Rows[].TextAlign is a new feature in 3.14
Is it possible to have the text in a column header aligned center and have the column data aligned left?
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Administrator
sholzy is in the usergroup ‘unknown’
It is done similar to how the header is aligned.
From one of my database projects:

Code (gambas)

  1. For Each MGlobal.hResData
  2.   gvViewer[iRow, 0].Text = Str$(Format(DateAdd(CDate("1/1/1970"), gb.Second, MGlobal.hResData!date), mm/dd/yyyy hh:nn:ss  ddd")) ' convert from unix timestamp
  3.  gvViewer[iRow, 0].Alignment = Align.Right
  4.  gvViewer[iRow, 1].Text = MGlobal.hResData!name
  5.  gvViewer[iRow, 1].Alignment = Align.Right
  6.  gvViewer[iRow, 2].Text = MGlobal.hResData!location
  7.  gvViewer[iRow, 2].Alignment = Align.Right
  8.  gvViewer[iRow, 3].Text = MGlobal.hResData!leave
  9.  gvViewer[iRow, 3].Alignment = Align.Right
  10.  gvViewer[iRow, 4].Text = MGlobal.hResData!comment
  11.  gvViewer[iRow, 4].Alignment = Align.Right
  12.  Inc iRow
  13. Next

If you need to only align, for example, the cells in the 3rd column, you also have to align column cells 0 thru 2. (At least it wouldn't for me.)

sholzy
Gambas One Site Director

To report bugs in the Gambas IDE:
Official Gambas Bug Tracker
Online now: Yes Back to the top

Post

Posted
Rating:
#3
Regular
bill-lancaster is in the usergroup ‘Regular’
 Thanks Got2BeFree, I hope I've not misunderstood your sugestion but my question is about columns.
If I change the alignment of any cell in a column, all the cells in that column are changed
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Administrator
sholzy is in the usergroup ‘unknown’
<COLOR color="#FF0000">EDIT: Scratch reply below and my reply above. I just went back and reread your question for the hundredth time and just realized what you wrote is for the newest release. I haven't upgraded yet so just ignore me.  :lol:   

Without actually looking at the new command, I would venture a wild guess that it would work similar to what I've already described. I'm off to see if I'm able to upgrade so I can figure this out properly….
</COLOR>


The only way I've been able to align a cell differently from it's header is by changing a cell's alignment individually. It takes 2 different commands, one to align headers in one direction, and one to align the cells below that header in another direction.

The .Columns[x].Alignment = aligns the header and the cells of the whole column below that header. To change the alignment of the cells below the header, you will need to change the alignment of each cell in that column row by row ( GridView[x,x].Alignment = ). And, to change the alignment of each of the cells for a column, you also need to align the cells in the columns before it, you can't just align the cells in column 2 without aligning the cells in columns 0 and 1.

sholzy
Gambas One Site Director

To report bugs in the Gambas IDE:
Official Gambas Bug Tracker
Online now: Yes Back to the top

Post

Posted
Rating:
#5
Avatar
Guru
cogier is in the usergroup ‘Guru’
You need a Gridview on a form to run this but it works.

Code (gambas)

  1.  Dim iRow, iCol As Integer
  2.   Dim sText As String[] = ["One", "Two", "Three", "Four", "Five"]
  3.  
  4.   GridView1.Columns.Count = sText.Count
  5.   GridView1.Rows.Count = sText.Count
  6.  
  7.   For iCol = 0 To sText.Max
  8.     GridView1.Columns[iCol].Title = sText[iCol]
  9.     GridView1.Columns[iCol].Alignment = Align.Center
  10.   Next
  11.  
  12.   For iCol = 0 To sText.Max
  13.     For iRow = 0 To sText.Max
  14.       GridView1[iRow, iCol].Text = sText[iRow]
  15.       GridView1[iRow, iCol].Alignment = Align.Left  ''If you take this out all will be centred
  16.     Next
  17.   Next

<IMG src="http://www.cogier.com/gambas/GridView.png"> </IMG>
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Administrator
sholzy is in the usergroup ‘unknown’

cogier said

You need a Gridview on a form to run this but it works.

Code (gambas)

  1.  Dim iRow, iCol As Integer
  2.   Dim sText As String[] = ["One", "Two", "Three", "Four", "Five"]
  3.  
  4.   GridView1.Columns.Count = sText.Count
  5.   GridView1.Rows.Count = sText.Count
  6.  
  7.   For iCol = 0 To sText.Max
  8.     GridView1.Columns[iCol].Title = sText[iCol]
  9.     GridView1.Columns[iCol].Alignment = Align.Center
  10.   Next
  11.  
  12.   For iCol = 0 To sText.Max
  13.     For iRow = 0 To sText.Max
  14.       GridView1[iRow, iCol].Text = sText[iRow]
  15.       GridView1[iRow, iCol].Alignment = Align.Left  ''If you take this out all will be centred
  16.     Next
  17.   Next

<IMG src="http://www.cogier.com/gambas/GridView.png"> </IMG>

Your example is the same as what I had (mine less detail) before realizing what I was doing wasn't what Bill is wanting to know.

I believe what bill-lancaster is referring to is a new property to align the cell text in the newest release. I'm not able to upgrade yet to test, and I don't see the new property yet in the wiki to see how it works.

sholzy
Gambas One Site Director

To report bugs in the Gambas IDE:
Official Gambas Bug Tracker
Online now: Yes Back to the top

Post

Posted
Rating:
#7
Regular
bill-lancaster is in the usergroup ‘Regular’
 Thank you very much, that's perfect
Online now: No Back to the top

Post

Posted
Rating:
#8
Avatar
Guru
cogier is in the usergroup ‘Guru’
Looking into GridView.Rows[].TextAlign further I discovered this quote from the GitLab page here.

GridView: GridView.Rows[].TextAlign is a new property that allows to define the alignment of the row header text.

The alignment of the row numbers is 'Center' by default and the only alignment changes that seem to work are 'Left' and 'Right'. You also need to create a lot of rows so that you can see the result.

Here is some code to demonstrate this feature. Unless I have missed something it is not the most exciting feature!

Code (gambas)

  1. ''Requires a Gridview on the form
  2.  
  3. Public Sub Form_Open()
  4.  
  5.   Dim iRow, iCol As Integer
  6.   Dim sText As String[] = ["One", "Two", "Three", "Four", "Five"]
  7.  
  8.   GridView1.Header = GridView.Both
  9.   GridView1.Columns.Count = sText.Count
  10.   GridView1.Rows.Count = 10000
  11.  
  12.   For iCol = 0 To sText.Max
  13.     GridView1.Columns[iCol].Title = sText[iCol]
  14.     GridView1.Columns[iCol].Alignment = Align.Center
  15.   Next
  16.  
  17.   For iCol = 0 To sText.Max
  18.     For iRow = 0 To sText.Max
  19.       GridView1.Rows.Height = 100
  20.       GridView1[iRow, iCol].Text = sText[iRow]
  21.       GridView1[iRow, iCol].Alignment = Align.Left
  22.     Next
  23.   Next
  24.  
  25.   GridView1.Rows[0].TextAlignment = Align.Left
  26.   GridView1.Rows[2].TextAlignment = Align.Right
  27.   GridView1.Rows[3].TextAlignment = Align.Bottom
  28.   GridView1.Rows[4].TextAlignment = Align.BottomLeft
  29.   GridView1.Rows[5].TextAlignment = Align.BottomNormal
  30.   GridView1.Rows[6].TextAlignment = Align.BottomRight
  31.   GridView1.Rows[7].TextAlignment = Align.Top
  32.   GridView1.Rows[8].TextAlignment = Align.TopLeft
  33.   GridView1.Rows[9].TextAlignment = Align.TopRight
  34.   GridView1.Rows[10].TextAlignment = Align.TopNormal
  35.  
  36.  
<IMG src="http://www.cogier.com/gambas/GridView1.png"> </IMG>
Online now: No Back to the top
1 guest and 0 members have just viewed this.