Solved

Change click behavior on DataGridView cell

Posted on 2010-11-10
6
681 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now