Share via

datagridview get column header position

AndyNakamura 51 Reputation points
2021-02-04T16:59:37.117+00:00

I have a DataGridView and some textboxes that I use for filtering the various columns.
I want to position the textboxes so that they lay above the column that they filter.

I can do this by using the datagridview.getCellDisplayRectangle to get the 1st row in the column I'm interested in.
'BIND FIXTURE TEXTBOX TO COLUMN 3.
rect = DV1.GetCellDisplayRectangle(3, 0, False)
x = rect.X ' + DV1.Left
y = rect.Y '+ DV1.Top
Width = rect.Width
height = rect.Height

    With TxtFix
        .SetBounds(x, DV1.Top - 22, Width, height)
        .Visible = True
    End With
    'LABEL
    With LblFix
        .SetBounds(x, DV1.Top - 36, Width, height)
    End With

However, if there are no rows after filtering then an exception is thrown.
So I want to get the position of the cell header instead as that is always there.
But I can't find out how to get it.
I've tried, amongst other things:
Rectheader = DV1.Columns(3).HeaderCell.ContentBounds()
But that just gives the dimensions of the header cell. No position data

Developer technologies | VB
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

4 answers

Sort by: Most helpful
  1. AndyNakamura 51 Reputation points
    2021-02-06T08:58:57.923+00:00

    Keying off! That's a new one on me:-)

    In the end I just got the various column widths: Dvg1.Columns(x).width &
    the Dgv.Left position + Dvg.RowHeaderWidth
    So the Left position of the 1st column (Px1) in x is Dgv.Left + Dvg.RowHeaderWidth
    The position of the second column is
    Px2 = Px1 + Dvg1.Columns(0).width
    Px3 = Px2 + Dvg1.Columns(1).width
    etc
    The y location of the textboxes is Dgv1.top - 22 (To sit the TextBox just above the Column Header
    Then called this in the Dgv_Resize event
    This still works when there are no rows in the Dgv
    It seems ok with 18 columns

    Was this answer helpful?

    0 comments No comments

  2. AndyNakamura 51 Reputation points
    2021-02-05T12:49:58.667+00:00

    Hi Karen,
    I was asking about getting the position of the column header, not about filtering a DataGridView.
    Unless I didn't read it carefully enough that's what the article seems to be about.

    Was this answer helpful?


  3. Karen Payne MVP 35,606 Reputation points Volunteer Moderator
    2021-02-04T19:58:40.523+00:00

    There is a viable solution that requires only several lines of code and if needed has several ways to customize.

    DgvFilterManager filterManager = new DgvFilterManager(dataGridView1);
    

    https://www.codeproject.com/Articles/33786/DataGridView-Filter-Popup

    Was this answer helpful?

    0 comments No comments

  4. AndyNakamura 51 Reputation points
    2021-02-04T17:02:58.537+00:00

    Sorry, 'Cell header' should be 'Column Header'

    So I want to get the position of the cell header instead as that is always there.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.