locked
DGV - Change backcolor in upper left header cell (.rows(-1).columns(-1) RRS feed

  • Question

  • Using Visual Basic, is there any way to change the backcolor of the upper left header cell (.rows(-1).columns(-1)) of a DataGridView?

    I would like to use a different color to indicate to the user that he/she can right click a row or column header to access the Context Menu Strip.  Not all of my DGVs use a CMS, so I would like to highlight its availability for the specific DGV.

    Thank you in advance for your help!

    George

    Sunday, October 25, 2009 7:24 PM

Answers

  • Hi George,

    I don't think you can get that cell like that. I'm not sure whether there is a way to access that cell directly. But if you want to change the backcolor of that cell, here is an alternative way. 
    1. Add a label in that place
    2. Change the backcolor of the added label.
    You can try the following lines
            Dim headerLabel As New Label
            headerLabel.Text = String.Empty
            headerLabel.Location = New Point(2, 2)
            headerLabel.Width = DataGridView1.RowHeadersWidth - 3
            headerLabel.Height = DataGridView1.ColumnHeadersHeight - 3
            headerLabel.BackColor = Color.AliceBlue
            DataGridView1.Controls.Add(headerLabel)
    Hope this helps

    Regards
    Jeff Shan
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Proposed as answer by MaDFroG20091013 Tuesday, October 27, 2009 2:41 PM
    • Marked as answer by Jeff Shan Thursday, October 29, 2009 7:03 AM
    Tuesday, October 27, 2009 8:07 AM

All replies

  • Hi George,

    I don't think you can get that cell like that. I'm not sure whether there is a way to access that cell directly. But if you want to change the backcolor of that cell, here is an alternative way. 
    1. Add a label in that place
    2. Change the backcolor of the added label.
    You can try the following lines
            Dim headerLabel As New Label
            headerLabel.Text = String.Empty
            headerLabel.Location = New Point(2, 2)
            headerLabel.Width = DataGridView1.RowHeadersWidth - 3
            headerLabel.Height = DataGridView1.ColumnHeadersHeight - 3
            headerLabel.BackColor = Color.AliceBlue
            DataGridView1.Controls.Add(headerLabel)
    Hope this helps

    Regards
    Jeff Shan
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Proposed as answer by MaDFroG20091013 Tuesday, October 27, 2009 2:41 PM
    • Marked as answer by Jeff Shan Thursday, October 29, 2009 7:03 AM
    Tuesday, October 27, 2009 8:07 AM
  • Jeff -

    Sorry I haven't gotten back to you sooner.

    Your solution does exactly what I want - thank you very much!

    George
    Tuesday, November 3, 2009 4:12 PM