Gridview column header alignment

Post Reply
bill-lancaster
Posts: 76
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

Gridview column header alignment

Post by bill-lancaster » Wednesday 04th December 2019 3:45pm

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?

User avatar
Got2BeFree
Posts: 75
Joined: Saturday 26th November 2016 2:52am
Location: Lost

Re: Gridview column header alignment

Post by Got2BeFree » Wednesday 04th December 2019 7:37pm

It is done similar to how the header is aligned.
From one of my database projects:
For Each MGlobal.hResData
  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
  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.)
sholzy

I'm wondering around lost in the past, not knowing where the present is.

bill-lancaster
Posts: 76
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

Re: Gridview column header alignment

Post by bill-lancaster » Thursday 05th December 2019 7:40am

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

User avatar
Got2BeFree
Posts: 75
Joined: Saturday 26th November 2016 2:52am
Location: Lost

Re: Gridview column header alignment

Post by Got2BeFree » Thursday 05th December 2019 2:31pm

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


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

I'm wondering around lost in the past, not knowing where the present is.

User avatar
cogier
Site Admin
Posts: 360
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Gridview column header alignment

Post by cogier » Thursday 05th December 2019 6:23pm

You need a Gridview on a form to run this but it works.
 Dim iRow, iCol As Integer
  Dim sText As String[] = ["One", "Two", "Three", "Four", "Five"]

  GridView1.Columns.Count = sText.Count
  GridView1.Rows.Count = sText.Count

  For iCol = 0 To sText.Max
    GridView1.Columns[iCol].Title = sText[iCol]
    GridView1.Columns[iCol].Alignment = Align.Center
  Next

  For iCol = 0 To sText.Max
    For iRow = 0 To sText.Max
      GridView1[iRow, iCol].Text = sText[iRow]
      GridView1[iRow, iCol].Alignment = Align.Left  ''If you take this out all will be centred
    Next
  Next
Image

User avatar
Got2BeFree
Posts: 75
Joined: Saturday 26th November 2016 2:52am
Location: Lost

Re: Gridview column header alignment

Post by Got2BeFree » Thursday 05th December 2019 7:04pm

cogier wrote:
Thursday 05th December 2019 6:23pm
You need a Gridview on a form to run this but it works.
 Dim iRow, iCol As Integer
  Dim sText As String[] = ["One", "Two", "Three", "Four", "Five"]

  GridView1.Columns.Count = sText.Count
  GridView1.Rows.Count = sText.Count

  For iCol = 0 To sText.Max
    GridView1.Columns[iCol].Title = sText[iCol]
    GridView1.Columns[iCol].Alignment = Align.Center
  Next

  For iCol = 0 To sText.Max
    For iRow = 0 To sText.Max
      GridView1[iRow, iCol].Text = sText[iRow]
      GridView1[iRow, iCol].Alignment = Align.Left  ''If you take this out all will be centred
    Next
  Next
Image
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

I'm wondering around lost in the past, not knowing where the present is.

bill-lancaster
Posts: 76
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

Re: Gridview column header alignment

Post by bill-lancaster » Friday 06th December 2019 8:28am

Thank you very much, that's perfect

User avatar
cogier
Site Admin
Posts: 360
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Gridview column header alignment

Post by cogier » Friday 06th December 2019 3:14pm

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!
''Requires a Gridview on the form

Public Sub Form_Open()

  Dim iRow, iCol As Integer
  Dim sText As String[] = ["One", "Two", "Three", "Four", "Five"]

  GridView1.Header = GridView.Both
  GridView1.Columns.Count = sText.Count
  GridView1.Rows.Count = 10000

  For iCol = 0 To sText.Max
    GridView1.Columns[iCol].Title = sText[iCol]
    GridView1.Columns[iCol].Alignment = Align.Center
  Next

  For iCol = 0 To sText.Max
    For iRow = 0 To sText.Max
      GridView1.Rows.Height = 100
      GridView1[iRow, iCol].Text = sText[iRow]
      GridView1[iRow, iCol].Alignment = Align.Left
    Next
  Next

  GridView1.Rows[0].TextAlignment = Align.Left
  GridView1.Rows[2].TextAlignment = Align.Right
  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

End

Image

Post Reply