简体   繁体   中英

vb.net print datagridview column alignment

My grid displays with the first 3 columns left aligned and the next 4 right aligned.

Using this code e.Graphics.DrawString(Stock.gdColours(j, i).Value, f, Brushes.Black, x, y + 4)

all columns print left aligned. Because I need columns 3 onward to be right aligned I added:

If j >2 Then 
    sf.Alignment = StringAlignment.Far 
Else 
    sf.Alignment = StringAlignment.Near

e.Graphics.DrawString(Stock.gdColours(j, i).Value, f, Brushes.Black, x, y + 4, sf)

But the result is that the alignment is correct but Colums 3 onward have all appeared from Column 2 onward.

Help would be appreciated.

The answer that worked for me is below. I was using X1, X2 and X3 as the column positions for near aligned strings. When I changed the middle column to be far aligned, I set the X position of the middle column to be just to the left of the third column.

         For intRow As Integer = 0 To _aryTopTable.GetLength(0) - 1
            'Draw each row element
            format.Alignment = StringAlignment.Near
            e.Graphics.DrawString(_aryTopTable(intRow, 0), _tableFont, Brushes.Black, X1, Y, format)
            format.Alignment = StringAlignment.Far
            e.Graphics.DrawString(_aryTopTable(intRow, 1), _tableFont, Brushes.Black, X3 - 10, Y, format)
            format.Alignment = StringAlignment.Near
            e.Graphics.DrawString(_aryTopTable(intRow, 2), _tableFont, Brushes.Black, X3, Y, format)
            'Advance Y
            Y = Y + _tableFont.Height + Yp
        Next

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM