Results 1 to 5 of 5

Thread: [RESOLVED] Put ListBox over top cell in DataGridView column

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2011
    Posts
    85

    Resolved [RESOLVED] Put ListBox over top cell in DataGridView column

    I've got this working but there's something I don't understand about it. I have a DataGridView and a listbox. I want to position the listbox to be the same location as the topmost visible cell in the column the mouse is in. The end result will be that a context menu will be provided to show the user the distinct values in that column which is why I want the listbox to show over the column that it's displaying data for, just below the header cell. I'm testing right now using the CellMouseEnter event and the following code is working:
    Code:
    Private Sub dgvItems_CellMouseEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvItems.CellMouseEnter
    	If e.ColumnIndex >= 0 Then
    		Dim r As System.Drawing.Rectangle = dgvItems.GetCellDisplayRectangle(e.ColumnIndex, 0, True)
    		lstDistinctValues.Location = New System.Drawing.Point(r.Left + 9, dgvItems.Top + 69)
    		lstDistinctValues.Visible = True
    	End If
    End Sub
    But notice the offsets that I had to use to get the positioning just right. That's my question - why is using the location of the cell putting my textbox so far off from where I want it?

    Thanks!

    Edit: if I use the rectangle's Y position as the top of the ListBox, the ListBox ends up completely outside the DataGridView which is why I'm using the DataGridView's Y position.
    Last edited by BrainPain; May 26th, 2011 at 08:26 PM.

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Put ListBox over top cell in DataGridView column

    Try this out

    Code:
    If e.ColumnIndex >= 0 Then
        Dim CellPoint As Point = Me.dgvItems.GetCellDisplayRectangle(e.ColumnIndex, _ 
                                                                     e.RowIndex, _ 
                                                                     True).Location
        Dim GridPoint As Point = dgvItems.Location
        Dim PointLocation As Point
        PointLocation.X = GridPoint.X + CellPoint.X
        PointLocation.Y = dgvItems.Top - dgvItems.ColumnHeadersHeight
        Me.lstDistinctValues.Location = PointLocation
        lstDistinctValues.Visible = True
    End If

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2011
    Posts
    85

    Re: Put ListBox over top cell in DataGridView column

    Sorry, that's not it either. The x position looks about right but the y position is still quite a bit higher than the top of the DataGridView. I changed your y position from "- dgvitems.ColumnHeadersHeight" to "+ dgvItems.ColumnHeadersHeight" and it was still too high.
    I believe the cell's position is relative to the DataGridView that it's in and the ListBox position is relative to the form itself, so that's probably what accounts for the offsets I have to use. I'll probably leave my code as is since it's correctly positioning the ListBox, but do you know if I'll get the same rendering on different pcs and/or resolutions?

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Put ListBox over top cell in DataGridView column

    Quote Originally Posted by BrainPain View Post
    Sorry, that's not it either. The x position looks about right but the y position is still quite a bit higher than the top of the DataGridView. I changed your y position from "- dgvitems.ColumnHeadersHeight" to "+ dgvItems.ColumnHeadersHeight" and it was still too high.
    I believe the cell's position is relative to the DataGridView that it's in and the ListBox position is relative to the form itself, so that's probably what accounts for the offsets I have to use. I'll probably leave my code as is since it's correctly positioning the ListBox, but do you know if I'll get the same rendering on different pcs and/or resolutions?
    Perhaps I misinterperted, the following image shows two different examples where the ListBoxes go exactly where I asked them to go. See attached demo also.
    Attached Files Attached Files
    Last edited by kareninstructor; Jul 20th, 2011 at 01:00 PM.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2011
    Posts
    85

    Re: Put ListBox over top cell in DataGridView column

    Ah, ok maybe we did have a miscommunication. When I said
    I want the listbox to show over the column that it's displaying data for, just below the header cell
    I meant that I wanted the ListBox to be in front of the column, with it's top just below the header cell of the column. I hadn't really thought of having the ListBox show up above the column header cell instead of below it. Your way might be more appealing on the form. I'll check it out further. 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