Scrollable multiple column header in datagridview

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New; color:blue
<p style="margin-bottom:0.0001pt; line-height:normal <span id="x_result_box" lang="en <span>I<span>m working
<span>on a <span>custom <span>datagrivew to create a gantt chart.
<p style="margin-bottom:0.0001pt; line-height:normal My first problem concerns "scrollable" multiple column header<span id="x_result_box" lang="en <span>.
<p style="margin-bottom:0.0001pt; line-height:normal <span id="x_result_box" lang="en <span>The
<span>following code that <span>I have recovered <span>in <span>
other posts is ok for "not scrollable" multiple columns header datagridview.
<span id="x_result_box" lang="en <span>In fact, <span>if I <span>
use <span>the first <span>columns <span>of the <span>
grid <span>as a <span>fixed<span id="x_result_box" lang="en <span>(.frozen) the others orizzontally scrollable multiple columns header have problems:
<p style="margin-bottom:0.0001pt; line-height:normal
<p style="margin-bottom:0.0001pt; line-height:normal 1.<span id="x_result_box" lang="en <span>The text of drawstring, when <span id="x_result_box" lang="en <span><span>the size
<span>of the rectangle <span>is reduced, <span>"stepwise" <span>
decreases<span id="x_result_box" lang="en <span>and not more gradually;
<p style="margin-bottom:0.0001pt; line-height:normal 2. <span id="x_result_box" lang="en
<span>The text of drawstring, when <span id="x_result_box" lang="en <span>the size
<span>of the rectangle <span>is reduced, <span><span>decrease<span id="x_result_box" lang="en <span>s
<span id="x_result_box" lang="en <span><span>"one character
<span>at a time" and not more gradually<span id="x_result_box" lang="en <span>;<span id="x_result_box" lang="en <span><span id="x_result_box" lang="en <span>I ask someone some suggestions to correct
these problems.
<p style="margin-bottom:0.0001pt; line-height:normal
<p style="margin-bottom:0.0001pt; line-height:normal
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New; color:blue Private<span style="font-size:10pt; font-family:Courier New
<span style="color:blue Sub Form1_Load(<span style="color:blue ByVal sender
<span style="color:blue As System.Object, <span style="color:blue ByVal e
<span style="color:blue As System.EventArgs) <span style="color:blue Handles
<span style="color:blue MyBase.Load
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue Me.DataGridView1.Columns.Add(<span style="color:#a31515 "Kilometer",
<span style="color:#a31515 "From")
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue Me.DataGridView1.Columns.Add(<span style="color:#a31515 "Kilometer",
<span style="color:#a31515 "To")
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue Me.DataGridView1.Columns.Add(<span style="color:#a31515 "Summer",
<span style="color:#a31515 "Weekday")
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue Me.DataGridView1.Columns.Add(<span style="color:#a31515 "Summer",
<span style="color:#a31515 "Weekend")
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue Me.DataGridView1.Columns.Add(<span style="color:#a31515 "Winter",
<span style="color:#a31515 "Weekday")
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue Me.DataGridView1.Columns.Add(<span style="color:#a31515 "Winter",
<span style="color:#a31515 "Weekend")
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New; color:green
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue For j <span style="color:blue As <span style="color:blue
Integer = 6 <span style="color:blue To <span style="color:blue Me.DataGridView1.ColumnCount - 1
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue Me.DataGridView1.Columns(j).Width = 45
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue Next
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue Me.DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue Me.DataGridView1.ColumnHeadersHeight = <span style="color:blue
Me.DataGridView1.ColumnHeadersHeight * 2
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue Me.DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue AddHandler <span style="color:blue Me.DataGridView1.CellPainting,
<span style="color:blue AddressOf DataGridView1_CellPainting
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue AddHandler <span style="color:blue Me.DataGridView1.Paint,
<span style="color:blue AddressOf DataGridView1_Paint
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue AddHandler <span style="color:blue Me.DataGridView1.Scroll,
<span style="color:blue AddressOf DataGridView1_Scroll
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue AddHandler <span style="color:blue Me.DataGridView1.ColumnWidthChanged,
<span style="color:blue AddressOf DataGridView1_ColumnWidthChanged <br/>

<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue End <span style="color:blue Sub

<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New; color:blue Private<span style="font-size:10pt; font-family:Courier New
<span style="color:blue Sub DataGridView1_CellPainting(<span style="color:blue ByVal sender
<span style="color:blue As <span style="color:blue Object, <span style="color:blue
ByVal e <span style="color:blue As System.Windows.Forms.DataGridViewCellPaintingEventArgs)
<span style="color:blue Handles DataGridView1.CellPainting
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue If e.RowIndex = -1 <span style="color:blue
AndAlso e.ColumnIndex > -1 <span style="color:blue Then
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue Dim r2 <span style="color:blue As Rectangle = e.CellBounds
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
r2.Y += e.CellBounds.Height / 2
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
r2.Height = e.CellBounds.Height / 2
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
e.PaintBackground(r2, <span style="color:blue True)
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
e.PaintContent(r2)
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
e.Handled = <span style="color:blue True
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue End <span style="color:blue If
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue End <span style="color:blue Sub

<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New; color:blue Private<span style="font-size:10pt; font-family:Courier New
<span style="color:blue Sub DataGridView1_ColumnWidthChanged(<span style="color:blue ByVal sender
<span style="color:blue As <span style="color:blue Object, <span style="color:blue
ByVal e <span style="color:blue As System.Windows.Forms.DataGridViewColumnEventArgs)
<span style="color:blue Handles DataGridView1.ColumnWidthChanged
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue Dim rtHeader <span style="color:blue As Rectangle =
<span style="color:blue Me.DataGridView1.DisplayRectangle
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
rtHeader.Height = <span style="color:blue Me.DataGridView1.ColumnHeadersHeight / 2
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue Me.DataGridView1.Invalidate(rtHeader)
<span style="font-size:10pt; font-family:Courier New; color:green
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue End <span style="color:blue Sub
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New; color:blue
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue Private <span style="color:blue Sub DataGridView1_Paint(<span style="color:blue ByVal sender
<span style="color:blue As <span style="color:blue Object, <span style="color:blue
ByVal e <span style="color:blue As System.Windows.Forms.PaintEventArgs)
<span style="color:blue Handles DataGridView1.Paint
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>

<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue Dim header1 <span style="color:blue As
<span style="color:blue String() = {<span style="color:#a31515 "Kilometer",
<span style="color:#a31515 "Summer", <span style="color:#a31515 "Winter"}
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue Dim j <span style="color:blue As <span style="color:blue
Integer = 0
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue While j < 6
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue Dim r1 <span style="color:blue As Rectangle =
<span style="color:blue Me.DataGridView1.GetCellDisplayRectangle(j, -1, <span style="color:blue
True)
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue Dim w2 <span style="color:blue As <span style="color:blue
Integer = <span style="color:blue Me.DataGridView1.GetCellDisplayRectangle(j, -1,
<span style="color:blue True).Width
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
r1.X += 1
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
r1.Y += 1
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
r1.Width = r1.Width + w2 - 2
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
r1.Height = r1.Height / 2 - 2
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
e.Graphics.FillRectangle(<span style="color:blue New SolidBrush(<span style="color:blue Me.DataGridView1.ColumnHeadersDefaultCellStyle.BackColor), r1)
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue Dim format <span style="color:blue As
<span style="color:blue New StringFormat()
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
format.Alignment = StringAlignment.Center
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
format.LineAlignment = StringAlignment.Center
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
e.Graphics.DrawString(header1(j / 2), <span style="color:blue Me.DataGridView1.ColumnHeadersDefaultCellStyle.Font,
<span style="color:blue New SolidBrush(<span style="color:blue Me.DataGridView1.ColumnHeadersDefaultCellStyle.ForeColor), r1, format)
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
j += 2
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue End <span style="color:blue While<span style="font-size:10pt; font-family:Courier New; color:blue
<br/>

<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue End <span style="color:blue Sub
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New; color:blue
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue Private <span style="color:blue Sub DataGridView1_Scroll(<span style="color:blue ByVal sender
<span style="color:blue As <span style="color:blue Object, <span style="color:blue
ByVal e <span style="color:blue As System.Windows.Forms.ScrollEventArgs)
<span style="color:blue Handles DataGridView1.Scroll
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue Dim rtHeader <span style="color:blue As Rectangle =
<span style="color:blue Me.DataGridView1.DisplayRectangle
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
rtHeader.Height = <span style="color:blue Me.DataGridView1.ColumnHeadersHeight / 2
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue Me.DataGridView1.Invalidate(rtHeader)
<p style="margin-bottom:0.0001pt; line-height:normal <span style="font-size:10pt; font-family:Courier New <span>
<span style="color:blue End <span style="color:blue Sub

View the full article
 

Similar threads

D
Replies
0
Views
104
Donald Uko
D
K
Replies
0
Views
224
Khan345
K
D
Replies
0
Views
103
Donald Uko
D
I
Replies
0
Views
153
Innovators World Wide
I
Back
Top