Solved

Change click behavior on DataGridView cell

Posted on 2010-11-10
6
682 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

947 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

21 Experts available now in Live!

Get 1:1 Help Now