Gridview column header alignment
Posted
#1
(In Topic #343)
Regular

Is it possible to have the text in a column header aligned center and have the column data aligned left?
Posted
Administrator

From one of my database projects:
Code (gambas)
- gvViewer[iRow, 0].Alignment = Align.Right
- gvViewer[iRow, 1].Text = MGlobal.hResData!name
- gvViewer[iRow, 1].Alignment = Align.Right
- gvViewer[iRow, 2].Text = MGlobal.hResData!location
- gvViewer[iRow, 2].Alignment = Align.Right
- gvViewer[iRow, 3].Text = MGlobal.hResData!leave
- gvViewer[iRow, 3].Alignment = Align.Right
- gvViewer[iRow, 4].Text = MGlobal.hResData!comment
- gvViewer[iRow, 4].Alignment = Align.Right
- Inc iRow
- 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.)
Posted
Regular

If I change the alignment of any cell in a column, all the cells in that column are changed
Posted
Administrator

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.
Posted
Guru

Code (gambas)
- GridView1.Columns.Count = sText.Count
- GridView1.Rows.Count = sText.Count
- GridView1.Columns[iCol].Title = sText[iCol]
- GridView1.Columns[iCol].Alignment = Align.Center
- GridView1[iRow, iCol].Text = sText[iRow]
<IMG src="http://www.cogier.com/gambas/GridView.png">
</IMG>
Posted
Administrator

cogier said
You need a Gridview on a form to run this but it works.Code (gambas)
GridView1.Columns.Count = sText.Count GridView1.Rows.Count = sText.Count GridView1.Columns[iCol].Title = sText[iCol] GridView1.Columns[iCol].Alignment = Align.Center GridView1[iRow, iCol].Text = sText[iRow]
<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.
Posted
Regular

Posted
Guru

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)
- ''Requires a Gridview on the form
- GridView1.Columns.Count = sText.Count
- GridView1.Rows.Count = 10000
- GridView1.Columns[iCol].Title = sText[iCol]
- GridView1.Columns[iCol].Alignment = Align.Center
- GridView1.Rows.Height = 100
- GridView1[iRow, iCol].Text = sText[iRow]
- GridView1.Rows[3].TextAlignment = Align.Bottom
- GridView1.Rows[4].TextAlignment = Align.BottomLeft
- GridView1.Rows[5].TextAlignment = Align.BottomNormal
- GridView1.Rows[6].TextAlignment = Align.BottomRight
- GridView1.Rows[7].TextAlignment = Align.Top
- GridView1.Rows[8].TextAlignment = Align.TopLeft
- GridView1.Rows[9].TextAlignment = Align.TopRight
- GridView1.Rows[10].TextAlignment = Align.TopNormal
</IMG>
1 guest and 0 members have just viewed this.

