Results 1 to 12 of 12

Thread: [RESOLVED] Center Of DGV Visible Area

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    404

    Resolved [RESOLVED] Center Of DGV Visible Area

    Hi,

    I want to display a label in the center of a Datagridview if there is no data to be shown, how can I find the exact center of the visible area of the datagridview?

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,449

    Re: Center Of DGV Visible Area

    You don't really need to. I've never done it so I might not have all the details but you should be able to handle the Paint event of the grid and then use GDI+ to draw the text. When you call DrawString, you can specify the entire grid area as the Rectangle in which to draw and you can specify that the text be centred both horizontally and vertically. It will then happen automatically, even if the grid gets resized.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    404

    Re: Center Of DGV Visible Area

    Hi,

    the draw string method of the graphics path only allows me to draw the string from a specific point which I set to width/2 and height/2 but this doesn't draw it anywhere near the center

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,449

    Re: Center Of DGV Visible Area

    Quote Originally Posted by Leary222 View Post
    Hi,

    the draw string method of the graphics path only allows me to draw the string from a specific point which I set to width/2 and height/2 but this doesn't draw it anywhere near the center
    No, that's not the case. The Graphics.DrawString method is overloaded and allows you to specify where and how the String is drawn in a variety of ways. There is one that has. among others, parameters of types RectangelF and StringFormat. The RectangleF can be based on the Size of the grid, and the StringFormat can specify both horizontal and vertical centering.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    404

    Re: Center Of DGV Visible Area

    Code:
    grfx.DrawString("No Data To Display", New Font("Arial", 12), Brushes.Black, New Rectangle(QuestionsDataGrid.Location, QuestionsDataGrid.Size), New StringFormat(StringAlignment.Center))
    I've tried the above now I cant even see the text within the visible datagrid, can you please help further?

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,449

    Re: Center Of DGV Visible Area

    The RectangleF you specify is relative to the grid itself, while the grid's Location is relative to its parent control. When you create the RectangleF you need to specify (0, 0) as the location of its upper-left corner.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    404

    Re: Center Of DGV Visible Area

    Brilliant, thanks for that, how can I apply it to all datagridviews throughout my application or would I need to add it manually to the paint event of each individual control

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,449

    Re: Center Of DGV Visible Area

    Quote Originally Posted by Leary222 View Post
    Brilliant, thanks for that, how can I apply it to all datagridviews throughout my application or would I need to add it manually to the paint event of each individual control
    The best option is to take advantage of the fact that VB.NET is an object-oriented language and use inheritance to build the functionality into the controls themselves. Create your own class that inherits DataGridView, override its OnPaint method and put the functionality in there. You might add a property for the text if it might vary from one control to another or hard-code the text if it won't change. You then simply use your control instead of a vanilla DataGridView and the rest happens automatically.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    404

    Re: Center Of DGV Visible Area

    Code:
        Public Class DGV
            Inherits DataGridView
            Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
                MyBase.OnPaint(e)
                If Me.RowCount = 0 Then
                    Using grfx As Graphics = e.Graphics
                        Dim a As New StringFormat
                        a.Alignment = StringAlignment.Center
                        a.LineAlignment = StringAlignment.Center
                        grfx.DrawString("No Data To Display", New Font("Arial", 12), Brushes.Black, New RectangleF(New Point(0, 0), Me.Size), a)
                    End Using
                End If
    
            End Sub
        End Class
    ive done the above, but how do I add this as a control, its not showing in the toolbox?

  10. #10
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,449

    Re: Center Of DGV Visible Area

    Quote Originally Posted by Leary222 View Post
    its not showing in the toolbox?
    After building your project, you should find a new section in the Toolbox for components defined in the current solution, in which your new control should appear.

    I'd recommend a better name than DGV though. If you don't want to bother with a name that specifically describes the new functionality, one common convention when extending an existing control is to simply add "Ex" to the end of the name, so you could call your control DataGridViewEx.

  11. #11
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,449

    Re: Center Of DGV Visible Area

    By the way, while it's not a big deal, it's more correct to use `Point.Empty` than `New Point(0, 0)` and, in this case, more correct still to use `PointF.Empty`. Also, you might also consider replacing this:
    Code:
    Dim a As New StringFormat
    a.Alignment = StringAlignment.Center
    a.LineAlignment = StringAlignment.Center
    with this:
    Code:
    Dim a As New StringFormat With {.Alignment = StringAlignment.Center,
                                    .LineAlignment = StringAlignment.Center}
    and at least use variable names that make some sense, e.g. `sf` for a StringFormat.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    404

    Re: Center Of DGV Visible Area

    thanks for your help and suggestions, i wanted to get the functionality to work before renaming the variables.

    however i wasn't aware of the way of initialising the string format the way you did above many thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width