GridView
Posted
#1
(In Topic #275)
Administrator

If word-wrapping isn't even possible in a GridView, then the above is a moot point and I'll go back to figuring out how to make simple things more complicated.
Posted
Guru

<IMG src="http://www.cogier.com/gambas/WrapText.png">
</IMG>
Posted
Administrator

I haven't had a chance to look at the code yet, but a quick run gave a different output than your pic shows. No big deal since I now know word-wrap and row height can be set on the fly. A little later today I'll go through the code and see how I can adapt it to my project and post my findings.
Thanks again, Charlie!
Posted
Administrator

Since I have only 1 field in my db table that would need wrapping, it was easy enough to set the row height for rows needing wrapped. And, since that field has a set max length of characters, the text only needed to be split into 2 lines.
This code populates the GridView. Lines 2 and 3 determine which rows need the height increased. I found on my system, 50 was the perfect row height for 2 lines. I added lines 2 - 8 and 14 to the existing code, and changed line 13.
Code (gambas)
- gridviewPages.Rows[iRow].Height = 50 ' set row height for wrapped text
- sText = MGlobal.hResData!page_desc
- WordWrap()
- sWordWrap = MGlobal.hResData!page_desc
- gridviewPages[iRow, 0].Text = MGlobal.hResData!id
- gridviewPages[iRow, 2].Text = MGlobal.hResData!page_name
- gridviewPages[iRow, 3].Text = MGlobal.hResData!sql_table_name
- gridviewPages[iRow, 4].Text = sWordWrap
- sWordWrap = ""
- Inc iRow
This routine does the wrapping. It finds the best spot to wrap so no words are split.
What the results look like….
Posted
Administrator

I made your WordWrap into a function that returns the 'wrapped' text.
This way it is universally and even recursively usable for all kinds of situations.
Nice one for in my library
Code (gambas)
- iCount = LengthToWrap
- Dec iCount
Usage in you example would be:
Code (gambas)
- gridviewPages.Rows[iRow].Height = 50 ' set row height for wrapped text
- sText = MGlobal.hResData!page_desc
- sWordWrap = WordWrap(sText, 45)
- sWordWrap = MGlobal.hResData!page_desc
- gridviewPages[iRow, 0].Text = MGlobal.hResData!id
- gridviewPages[iRow, 2].Text = MGlobal.hResData!page_name
- gridviewPages[iRow, 3].Text = MGlobal.hResData!sql_table_name
- gridviewPages[iRow, 4].Text = sWordWrap
- sWordWrap = ""
- Inc iRow
Want to split in 3 lines say at 45 and 90?
Use it recursively
Code (gambas)
- sWordWrap = WordWrap(WordWrap(sText, 90), 45)
And you can go on and on and on….
P.S. Wrote this code right here in editor window, so might have typos and such :shock:
gbWilly
- Gambas Dutch translator
- Gambas wiki content contributor
- Gambas debian/ubuntu package recipe contributor
- GambOS, a distro for learning Gambas and more…
- Gambas3 Debian/Ubuntu repositories
… there is always a Catch if things go wrong!
- Gambas Dutch translator
- Gambas wiki content contributor
- Gambas debian/ubuntu package recipe contributor
- GambOS, a distro for learning Gambas and more…
- Gambas3 Debian/Ubuntu repositories
… there is always a Catch if things go wrong!
Posted
Administrator

gbWilly said
Hi,
I made your WordWrap into a function that returns the 'wrapped' text.
This way it is universally and even recursively usable for all kinds of situations.
Nice one for in my library![]()
Nice! Glad my code was found useful and made even better with only slight modifications.
I like having code posted this way since it shows how it's applied in real-world usage.
Posted
Administrator

Anyone know what the default height of a row is? When I ran cogier's example, my output was much worse than his. I'm sure my font size had much to do with my results, but still, even when changing font size, a GridView row height should change accordingly, but not if you set the row height manually.
When we set row height manually, like what I needed to do (word wrapping) in my original post, font height no longer has a say in the height. If we set row height at say, 50, which works great on my system for 2 lines wrapped, it may not work at all on your system and may be too much or too little.
So if, for example, Gambas takes font height + padding above and below the font to determine row height, we would need to do the same manually with some code. But how do we know what the font size (in points?) a system is using? For the above code example to be truly useful, we would need to dynamically adjust row height based on the number of wrap lines being pushed to a cell * font points + padding above and below. I seem to recall a thread on the old Gambas Guru forum on how to find font size. But unfortunately, all that historical info died with a recent hard drive death.
Posted
Administrator

You want to know the default gridview height.
Check the example attached on how to get it.
This default height could be used as a unit for multiplying in case of multiple lines.
The value is given according to the font type and size used.
Feel free to experiment, I used Sans Serif in the attached example.
Example on grid height for different font sizes.
Enjoy…
gbWilly
- Gambas Dutch translator
- Gambas wiki content contributor
- Gambas debian/ubuntu package recipe contributor
- GambOS, a distro for learning Gambas and more…
- Gambas3 Debian/Ubuntu repositories
… there is always a Catch if things go wrong!
- Gambas Dutch translator
- Gambas wiki content contributor
- Gambas debian/ubuntu package recipe contributor
- GambOS, a distro for learning Gambas and more…
- Gambas3 Debian/Ubuntu repositories
… there is always a Catch if things go wrong!
Posted
Administrator

Code (gambas)
What I was after was a formula to automagically set row height when word wrapping, which I found out how. After a little bit of reading, I realized Desktop.Scale returns half the height of the system font currently in use. So doing this…
Code (gambas)
In the above code sample, if you take Desktop.Scale and double it (*2), then multiply that by the number of wrapped lines (*2), and then add some padding (+8), the result is near perfect padding (on my system). The padding remains constant with several wrapped lines.
Your code had no spelling errors and worked on the first run.
1 guest and 0 members have just viewed this.




