• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1296
  • Last Modified:

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...
0
jerute
Asked:
jerute
  • 5
  • 3
1 Solution
 
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
 
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 
Jorge PaulinoIT Pro/DeveloperCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now