How do I fill the DataGridView blank space in VB?

Hi all.

I'm after a solution to my problem and if anyone can help I would dearly appreciate it.

I'm using a DataGridView component and commonly only fill a few lines. I don't like the blank gray area at the bottom and would rather it were filled with blank lines.

I'm aware that there isn't a built in function for this and I would have to draw them manually. I've found a post on the MSDN forum that shows how to do it but I don't understand C# too well and cannot get it to work. (http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1947368&SiteID=1)

Can anyone provide the means to do this in Visual Basic?

Many thanks in advance...
jeruteAsked:
Who is Participating?
 
Jorge PaulinoIT Pro/DeveloperCommented:
Try this way:
'
' Create a new class and paste this code
'
Class GridLineDataGridView
    Inherits DataGridView
 
    Protected Overloads Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        MyBase.OnPaint(e)
 
        Dim rowHeight As Integer = Me.RowTemplate.Height
        Dim h As Integer = Me.ColumnHeadersHeight + rowHeight * Me.RowCount
        Dim imgWidth As Integer = Me.Width - 2
        Dim rFrame As New Rectangle(0, 0, imgWidth, rowHeight)
        Dim rFill As New Rectangle(1, 1, imgWidth - 2, rowHeight)
        Dim rowHeader As New Rectangle(2, 2, Me.RowHeadersWidth - 3, rowHeight)
 
        Dim pen As New Pen(Me.GridColor, 1)
        Dim rowImg As New Bitmap(imgWidth, rowHeight)
        Dim g As Graphics = Graphics.FromImage(rowImg)
        g.DrawRectangle(pen, rFrame)
        g.FillRectangle(New SolidBrush(Me.DefaultCellStyle.BackColor), rFill)
        g.FillRectangle(New SolidBrush(Me.RowHeadersDefaultCellStyle.BackColor), rowHeader)
 
        Dim rowImgAAlternative As Bitmap = TryCast(rowImg.Clone(), Bitmap)
        Dim g2 As Graphics = Graphics.FromImage(rowImgAAlternative)
        rFill.X += Me.RowHeadersWidth - 1
        g2.FillRectangle(New SolidBrush(Me.AlternatingRowsDefaultCellStyle.BackColor), rFill)
 
        Dim w As Integer = Me.RowHeadersWidth - 1
        For j As Integer = 0 To Me.ColumnCount - 1
            g.DrawLine(pen, New Point(w, 0), New Point(w, rowHeight))
            g2.DrawLine(pen, New Point(w, 0), New Point(w, rowHeight))
            w += Me.Columns(j).Width
        Next
 
        Dim [loop] As Integer = (Me.Height - h) / rowHeight
        For j As Integer = 0 To [loop]
            Dim index As Integer = Me.RowCount + j
            If index Mod 2 = 0 Then
                e.Graphics.DrawImage(rowImg, 1, h + j * rowHeight)
            Else
                e.Graphics.DrawImage(rowImgAAlternative, 1, h + j * rowHeight)
            End If
        Next
 
    End Sub
 
End Class
 
 
'
' Build you project and then, from the toolbar, drag the GridLineDataGridView1 to your form.
' This code is just a sample and you should replace by your datasource (on form load)
'
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim dt As New DataTable()
        dt.Columns.Add("c1")
        dt.Columns.Add("c2")
        dt.Columns.Add("c3")
        dt.Columns.Add("c4")
        For j As Integer = 0 To 3
            dt.Rows.Add("c" + j.ToString(), "d" + j.ToString(), "hhh", "eee")
        Next
 
        Me.GridLineDataGridView1.DataSource = dt
        Me.GridLineDataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
        Me.GridLineDataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Orange
    End Sub

Open in new window

0
 
jeruteAuthor Commented:
Thanks for your response, but I think I'm missing something, or misunderstanding your instructions.

I have created my DataGridView component on my form and pasted your code, as it stands below:

But nothin is happening beyond the 4 columns and rows being filled in...

Regards



Confused...
Public Class Form1
 
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Dim dt As New DataTable()
		dt.Columns.Add("c1")
		dt.Columns.Add("c2")
		dt.Columns.Add("c3")
		dt.Columns.Add("c4")
		For j As Integer = 0 To 3
			dt.Rows.Add("c" + j.ToString(), "d" + j.ToString(), "hhh", "eee")
		Next
 
		Me.dgv1.DataSource = dt
		Me.dgv1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
		Me.dgv1.AlternatingRowsDefaultCellStyle.BackColor = Color.Orange
	End Sub
 
End Class
'
' Create a new class and paste this code
'
Class GridLineDataGridView
	Inherits DataGridView
 
	Protected Overloads Overrides Sub OnPaint(ByVal e As PaintEventArgs)
		MyBase.OnPaint(e)
 
		Dim rowHeight As Integer = Me.RowTemplate.Height
		Dim h As Integer = Me.ColumnHeadersHeight + rowHeight * Me.RowCount
		Dim imgWidth As Integer = Me.Width - 2
		Dim rFrame As New Rectangle(0, 0, imgWidth, rowHeight)
		Dim rFill As New Rectangle(1, 1, imgWidth - 2, rowHeight)
		Dim rowHeader As New Rectangle(2, 2, Me.RowHeadersWidth - 3, rowHeight)
 
		Dim pen As New Pen(Me.GridColor, 1)
		Dim rowImg As New Bitmap(imgWidth, rowHeight)
		Dim g As Graphics = Graphics.FromImage(rowImg)
		g.DrawRectangle(pen, rFrame)
		g.FillRectangle(New SolidBrush(Me.DefaultCellStyle.BackColor), rFill)
		g.FillRectangle(New SolidBrush(Me.RowHeadersDefaultCellStyle.BackColor), rowHeader)
 
		Dim rowImgAAlternative As Bitmap = TryCast(rowImg.Clone(), Bitmap)
		Dim g2 As Graphics = Graphics.FromImage(rowImgAAlternative)
		rFill.X += Me.RowHeadersWidth - 1
		g2.FillRectangle(New SolidBrush(Me.AlternatingRowsDefaultCellStyle.BackColor), rFill)
 
		Dim w As Integer = Me.RowHeadersWidth - 1
		For j As Integer = 0 To Me.ColumnCount - 1
			g.DrawLine(pen, New Point(w, 0), New Point(w, rowHeight))
			g2.DrawLine(pen, New Point(w, 0), New Point(w, rowHeight))
			w += Me.Columns(j).Width
		Next
 
		Dim [loop] As Integer = (Me.Height - h) / rowHeight
		For j As Integer = 0 To [loop]
			Dim index As Integer = Me.RowCount + j
			If index Mod 2 = 0 Then
				e.Graphics.DrawImage(rowImg, 1, h + j * rowHeight)
			Else
				e.Graphics.DrawImage(rowImgAAlternative, 1, h + j * rowHeight)
			End If
		Next
 
	End Sub
 
End Class

Open in new window

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Jorge PaulinoIT Pro/DeveloperCommented:
You added a new class and pasted the class code ? (Class GridLineDataGridView)

You have to:
1 - Create a new class
2 - Build your project
3 - Drag from the toolbox the new control
4 - fill the new control
0
 
jeruteAuthor Commented:
Yup.

I have created a new class (Class1.vb) and pasted the code exactly as you wrote it.

I have dragged the DataGridView component onto the form (Form1.vb). The component is called dgv1 (System.Windows.Forms.DataGridView).

The form code is working fine because it draws the orange and white alternating boxes and inserts the specified data, but the onpaint sub doesn't get called...
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
Ok, I will upload a sample ... give me a minute!
0
 
jeruteAuthor Commented:
Ah, I see my error. I was dragging a DataGridView component onto the form, rather than a GridLineDataGridView component (which I can't find anywhere, but Hey! It works.)

Many thanks, it's just what I'm after.

Regards.
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
Glad I could help!

After you build the application (with the class created) the control should appear on the top of the toolbox.
0
All Courses

From novice to tech pro — start learning today.