Solved

How do I fill the DataGridView blank space in VB?

Posted on 2008-06-24
8
1,134 Views
Last Modified: 2013-11-26
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
Comment
Question by:jerute
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 48

Expert Comment

by:jpaulino
ID: 21853485
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
 

Author Comment

by:jerute
ID: 21855248
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
 
LVL 48

Expert Comment

by:jpaulino
ID: 21855335
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jerute
ID: 21855509
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
 
LVL 48

Expert Comment

by:jpaulino
ID: 21855538
Ok, I will upload a sample ... give me a minute!
0
 
LVL 48

Accepted Solution

by:
jpaulino earned 500 total points
ID: 21855733
0
 

Author Comment

by:jerute
ID: 21855777
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
 
LVL 48

Expert Comment

by:jpaulino
ID: 21855920
Glad I could help!

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

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this post we will learn different types of Android Layout and some basics of an Android App.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question