Start New Discussion within our Software Development Community

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.

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;
        }

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 ;)

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?

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 ;)

This question has already been answered. Start a new discussion instead.