Solved

Change click behavior on DataGridView cell

Posted on 2010-11-10
6
686 Views
Last Modified: 2012-05-10
In a DataGridView, when the user wants to edit a text cell, it requires 3 clicks: one to select the cell, one to put it into edit mode (with whole cell highlighted) and one to remove highlighting and put the pointer at the clicked character.

I know how to add an event handler so that I can get the clicked cell. But what do I do next?
0
Comment
Question by:BlearyEye
6 Comments
 
LVL 7

Expert Comment

by:mkobrin
ID: 34100499
I'm not sure if this the best option for you, but one way is to create a textbox dynamically on your click event, size it to the same size as the cell, populate it with the contents of the cell , and then position it exactly where the cell is, and on the leave event of the textbox, update the contents of the cell with the contents of the textbox, before hiding or disposing of the textbox. Be sure to handle a cancel option for the user.
0
 
LVL 1

Author Comment

by:BlearyEye
ID: 34100731
My question was incomplete but mkobrin got the idea: I want to reduce this to a single click.

The textbox suggestion sounds complex and error-prone ...
0
 
LVL 16

Expert Comment

by:kris_per
ID: 34101125

You need to sub-class DataGridView and handle the mouse down message for this. Code for OneClickEditDataGridView class is here =>
http://social.msdn.microsoft.com/Forums/en/winformsdatacontrols/thread/8004651f-80a4-4b36-bb56-1518aea66b73

Hope this helps.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Assisted Solution

by:Zhaolai
Zhaolai earned 500 total points
ID: 34104916
In the CellClick event, add BeginEdit(), like this:

    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        DataGridView1.BeginEdit(False)
    End Sub

Open in new window

0
 
LVL 1

Accepted Solution

by:
BlearyEye earned 0 total points
ID: 34108250
Zhaolai: that reduces it to two mouse clicks (one to select & enter edit mode without selection and another to position the cursor). But it is a lot less confusing than the default.

I found this link on MSDN: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.beginedit.aspx
That's a lot more complex than your proposal. Any thoughts on the difference?
0
 
LVL 1

Author Closing Comment

by:BlearyEye
ID: 34195074
Zhaolai's solution pointed me in the right direction but it was incomplete and I came up with the correct reference after that.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question