1,105,534 Community Members

DataGridView cursor & headers

Member Avatar
Cap'nKirk
Junior Poster
130 posts since Feb 2010
Reputation Points: 10 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi,

I have a couple of questions regarding DataGridView.

I have a DGV that is created at runtime, it has 3 columns. I would like to change the mouse cursor to HAND on only 1 of these columns, is this possible, if so how?

Also how can I stop the ColumnHeaders from being resized by the client? i.e. I have set the height to 25 and I wish it to remain at 25.

Any help would be appreciated.

Kind regards..,

MT ;)

Have figured out the header resizing part:-

dGV.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;

Still stuck on the Cursor part though.

Member Avatar
Ryshad
Nearly a Posting Virtuoso
1,263 posts since Aug 2009
Reputation Points: 480 [?]
Q&As Helped to Solve: 247 [?]
Skill Endorsements: 14 [?]
 
0
 

Hi,

To the best of my knowledge the datagridview doesnt support a cursor property. You could acheive it manually by hooking up a CellMouseMove event handler, checking the current Column index and setting the cursor as applicable:

private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex >= 0 && e.ColumnIndex == 1) //change '1' to index of the column to show hand icon
                this.Cursor = Cursors.Hand;
            else
                this.Cursor = Cursors.Default;
        }
Member Avatar
Cap'nKirk
Junior Poster
130 posts since Feb 2010
Reputation Points: 10 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi,

To the best of my knowledge the datagridview doesnt support a cursor property. You could acheive it manually by hooking up a CellMouseMove event handler, checking the current Column index and setting the cursor as applicable:

private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex >= 0 && e.ColumnIndex == 1) //change '1' to index of the column to show hand icon
                this.Cursor = Cursors.Hand;
            else
                this.Cursor = Cursors.Default;
        }

Thank you for replying.

With the help of your sample code I was able to get it all working, see below:-

Code.DataGridView.CellMouseEnter += delegate(object sender, DataGridViewCellEventArgs e)
                {
                    if (e.ColumnIndex == 2)
                    {
                      Cursor = Cursors.Hand;
                    }
                    else
                      Cursor = Cursors.Default;
               };

I placed this code at the end of the construction loop for my runtime created controls just inside the end of the FormLoad procedure.

It works like a dream, in the snippet I have posted 'Code' is the name of the column for which I wanted the cursor to be a hand.

Once again thanks for replying, this helped me out.

Kind regards..,

MT ;)

Question Answered as of 3 Years Ago by Ryshad
Member Avatar
Ryshad
Nearly a Posting Virtuoso
1,263 posts since Aug 2009
Reputation Points: 480 [?]
Q&As Helped to Solve: 247 [?]
Skill Endorsements: 14 [?]
 
0
 

Glad it helped, but why did you declare the event handler like that? Code.DataGridView is a referene to the datagridview that contains Code...why not just hook up the datagridviews event handler in the designer?

Member Avatar
Cap'nKirk
Junior Poster
130 posts since Feb 2010
Reputation Points: 10 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Glad it helped, but why did you declare the event handler like that? Code.DataGridView is a referene to the datagridview that contains Code...why not just hook up the datagridviews event handler in the designer?

I think I understand what you mean. The reason I have done it this way is that all of my form controls are created dynamically at runtime, so I do not have the control in the designer.

Kind regards..,

MT ;)

Member Avatar
Ryshad
Nearly a Posting Virtuoso
1,263 posts since Aug 2009
Reputation Points: 480 [?]
Q&As Helped to Solve: 247 [?]
Skill Endorsements: 14 [?]
 
0
 

Aah, i see. In that case carry on ;)

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article