Draw lines around a group of cells in column in a datagridivew

divjoy

Well-known member
Joined
Aug 25, 2013
Messages
129
Programming Experience
1-3
Hi, Im using VS 2013.

I have a datagridview which represents a diary with times from 9:00am increasing every 15 minutes down the left most column. Each other column represents a day of the week Mon to Fri.

I want to group a number of cells together in each column with a box around them to indicate they are together and text flowing down all cells.

I know you need to use e.graphics but not sure how to do this either. Haven't found any good examples on line.

All help appreciated.

kind regards
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,439
Location
Norway
Programming Experience
10+
A quick sample that draws two cells as one:
VB.NET:
Dim c1 = Me.DataGridView1.Item(1, 2)
Dim sampletext = "this is a longer sample string spanning cell and then some"
Dim style = c1.InheritedStyle
Dim r1 = Me.DataGridView1.GetCellDisplayRectangle(1, 2, False)
Dim r2 = Me.DataGridView1.GetCellDisplayRectangle(1, 3, False)
Dim u = Rectangle.Union(r1, r2)

Using pen As New Pen(Color.Red, 2), back As New SolidBrush(style.BackColor)
    'clear interior
    Dim inner As New Rectangle(u.X + pen.Width, u.Y + pen.Width, u.Width - pen.Width * 2, u.Height - pen.Width * 2)
    e.Graphics.FillRectangle(back, inner)
    'draw text
    Dim flags As TextFormatFlags = TextFormatFlags.TextBoxControl Or TextFormatFlags.WordBreak Or TextFormatFlags.EndEllipsis
    TextRenderer.DrawText(e.Graphics, sampletext, style.Font, inner, style.ForeColor, style.BackColor, flags)
    'draw border
    Dim border As New Rectangle(u.X, u.Y, u.Width - pen.Width, u.Height - pen.Width)
    e.Graphics.DrawRectangle(pen, border)
End Using
 
Top Bottom