Solved

How do I fill the DataGridView blank space in VB?

Posted on 2008-06-24
8
1,066 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
  • 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

759 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now