|
I writing a program in which the users enter data into a DataGridView and then click a button to print out the data in grid form.
Since there is no way (that I could find) to print the grid directly, I am recreating the grid by hand.
I using for loops withGraphics.DrawLine to draw the grid lines, and Graphics.DrawString to print the actual data.
My problem is, I need to right justify the printing of some of the data.
Is it possible to create a label, make it right justified, load the data to it and print that?
There has to be a way to right justify, but I can't seem to find it. | | JimGuyer Wednesday, September 23, 2009 8:39 PM | Use a DrawString overload that takes a StringFormat and set the LineAlignment. - Marked As Answer byAland LiMSFT, ModeratorFriday, September 25, 2009 11:51 AM
- Marked As Answer bynobugzMVP, ModeratorWednesday, September 23, 2009 11:06 PM
- Unmarked As Answer byJimGuyer Thursday, September 24, 2009 9:42 PM
- Proposed As Answer byAland LiMSFT, ModeratorFriday, September 25, 2009 7:27 AM
-
| | JohnWein Wednesday, September 23, 2009 10:55 PM | Hi JimGuyer,
The code snippet below shows how to adjust the alignment with StringFormat:
'Create a string format.
Dim strFormat As New StringFormat
'Set vertical align to center.
strFormat.Alignment = StringAlignment.Center
'Set horizontal align to right.
strFormat.LineAlignment = StringAlignment.Far
'Draw the string with the string format.
e.Graphics.DrawString(sPageNo, DataGridViewToPrint.Font, _
Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(sPageNo, DataGridViewToPrint.Font, _
e.MarginBounds.Width).Width) / 2, e.MarginBounds.Top + e.MarginBounds.Height + 31, strFormat)
You can get more about StringFormat’s LineAlignment property from: http://msdn.microsoft.com/en-us/library/system.drawing.stringformat.linealignment.aspx
Regards, Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. - Marked As Answer byJimGuyer Friday, September 25, 2009 11:40 AM
-
| | Aland Li Friday, September 25, 2009 7:26 AM | Use a DrawString overload that takes a StringFormat and set the LineAlignment. - Marked As Answer byAland LiMSFT, ModeratorFriday, September 25, 2009 11:51 AM
- Marked As Answer bynobugzMVP, ModeratorWednesday, September 23, 2009 11:06 PM
- Unmarked As Answer byJimGuyer Thursday, September 24, 2009 9:42 PM
- Proposed As Answer byAland LiMSFT, ModeratorFriday, September 25, 2009 7:27 AM
-
| | JohnWein Wednesday, September 23, 2009 10:55 PM | Could you give me a code example? I found this
e.Graphics.DrawString(sPageNo, DataGridViewToPrint.Font, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(sPageNo, DataGridViewToPrint.Font, e.MarginBounds.Width).Width) / 2, e.MarginBounds.Top + e.MarginBounds.Height + 31)
Which would work but it is wordy. Basically, you take the length of the cell and subtract the length of the string to get a starting X-coordinate. I am not familair with LineAlingment or how to invoke it. | | JimGuyer Thursday, September 24, 2009 9:45 PM | If you don't have help files installed on your development machine, you can use the online MSDN library. Type "graphics" in the search box at the top of this page and follow the link to DrawString members. | | JohnWein Thursday, September 24, 2009 10:07 PM | Hi JimGuyer,
The code snippet below shows how to adjust the alignment with StringFormat:
'Create a string format.
Dim strFormat As New StringFormat
'Set vertical align to center.
strFormat.Alignment = StringAlignment.Center
'Set horizontal align to right.
strFormat.LineAlignment = StringAlignment.Far
'Draw the string with the string format.
e.Graphics.DrawString(sPageNo, DataGridViewToPrint.Font, _
Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(sPageNo, DataGridViewToPrint.Font, _
e.MarginBounds.Width).Width) / 2, e.MarginBounds.Top + e.MarginBounds.Height + 31, strFormat)
You can get more about StringFormat’s LineAlignment property from: http://msdn.microsoft.com/en-us/library/system.drawing.stringformat.linealignment.aspx
Regards, Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. - Marked As Answer byJimGuyer Friday, September 25, 2009 11:40 AM
-
| | Aland Li Friday, September 25, 2009 7:26 AM | Excellent!!!
Thank-you Aland. That is what I needed.
:-) | | JimGuyer Friday, September 25, 2009 11:41 AM |
|