Solved

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

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

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

Suggested Solutions

Title # Comments Views Activity
Why, and when, to use Windows Workflow 1 32
ConsoleSql 1 34
Connecting database in Xamarin? 6 53
Turn on intranet settings 1 41
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

16 Experts available now in Live!

Get 1:1 Help Now