Solved

DataGridView with DataGridViewComboBoxColumn: How to single click for dropdown list?

Posted on 2006-06-27
2
1,898 Views
Last Modified: 2008-01-09
Hello,

I have  DGV on a form that is working perfectly well, except I want a single click on the cell to begin editing rather than clicking two times.  For example, I would like a single click on a DataGridViewComboBoxColumn's cell to cause the dropdown list to appear and a single click on a DataGridViewTextBoxColumn to permit the user to start editing the contents.  I have the written the following code, which is attached to the MouseUp event, but I am not certain what I should have it do to the cell to achieve my goals.  I've tried setting focus on mouse up, but to no avail.

Thanks in advance for the help!

Mike

        private void dataGridView_MouseUp(object sender, MouseEventArgs e)
        {
            DataGridView.HitTestInfo hti = dataGridView.HitTest(e.X, e.Y);

            try
            {
                if (hti.Type == DataGridViewHitTestType.Cell)
                {
                    //  What to do here????
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
0
Comment
Question by:mjgardne
2 Comments
 
LVL 5

Accepted Solution

by:
nickhoggard earned 50 total points
ID: 17008345
Hi,

I just wrote a solution for this at work today, but I dont have it here sorry.

The basic idea was to catch the DataGridView.EditControlShowing event, check the control (e.Control) and see if it can be cast to a ComboBox.  If it can then add an event handler for the GotFocus event and in that event handler call ComboBox.DroppedDown  = true

within the EditControlShowing event handler:
if gettype(combobox).IsAssignableFrom(e.Control.GetType) then
    removehandler ctype(e.Control, ComboBox).GotFocus, addressof _GotFocus
    addhandler ctype(e.Control, ComboBox).GotFocus, addressof _GotFocus
end if

private sub _GotFocus(sender as object, e as eventargs)
    ctype(e.control, combobox).DroppedDown = true
end sub

Hope this helps

Cheers

Nick

0
 

Author Comment

by:mjgardne
ID: 17013693
Here is the answer that I've discovered and it works perfectly!  No extra event handling code required to achieve the results that I required...

On form_load set:

dataGridView.EditMode = DataGridViewEditMode.EditOnEnter;
 
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get id from json Data with NewtonSoft 3 49
C# guarantee sql connection close 6 39
C# Comparing String Arrays. 5 39
SSRS subreport with parameter 3 29
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

813 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

17 Experts available now in Live!

Get 1:1 Help Now