Solved

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

Posted on 2006-06-27
2
1,889 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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
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: …

762 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

13 Experts available now in Live!

Get 1:1 Help Now