Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGridView - "Cell Selection Style" - Edit Cell

I'm working on a WinForm client with a DataGridView control. I notice users have to click once to select the cell and again to edit it. What is the way to change this to a single click edit mode? I thought I had seen something like this before but can't remember the name.

like image 704
BuddyJoe Avatar asked Sep 04 '09 13:09

BuddyJoe


2 Answers

In the DataGridView properties: EditMode -> EditOnEnter

like image 114
Meta-Knight Avatar answered Oct 29 '22 14:10

Meta-Knight


Well I have noticed a problem with EditMode.EditOnEnter
It biases a lot of DataGriView's default behavior, which is irritating. Among others, the edited cell stays in edited mode even when the EndEdit method is explicitly called (you are force to click on another control to make the datagridview cell lose its focus.)

The following piece of code works pretty fine as it makes you edit by single clicking on any cell and ending the edit by hitting enter or clicking outside the DGView (just as you would do in the default behavior)

Here you go :

    private void myDatagridView_MouseUp(object sender, MouseEventArgs e)
    { 
        if (e.Button == MouseButtons.Left)
        {
            hitTestInfo = myDatagridView.HitTest(e.X, e.Y);
            if (hitTestInfo.Type == DataGridViewHitTestType.Cell) 
                myDatagridView.BeginEdit(true);
            else 
                myDatagridView.EndEdit();
        }
    } 
like image 25
Mehdi LAMRANI Avatar answered Oct 29 '22 14:10

Mehdi LAMRANI