Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cell double click event in data grid view

I have two DataGridView events. I have a problem, when I double click on a cell, both the events i.e., cell click and cell double click events are being invoked. please provide me with answer why this occured and whats solution.

Thanks

like image 750
AhmedShawky Avatar asked Nov 19 '12 12:11

AhmedShawky


3 Answers

Apparently there is no way to that just by setting properties in of the DataGridView. So you can use Timer to count if there was any double click, if not just do whatever you do in yous single click event handler, check the code:

System.Windows.Forms.Timer t;
        public Form1()
        {
            InitializeComponent();

            t = new System.Windows.Forms.Timer();
            t.Interval = SystemInformation.DoubleClickTime - 1;
            t.Tick += new EventHandler(t_Tick);
        }

        void t_Tick(object sender, EventArgs e)
        {
            t.Stop();
            DataGridViewCellEventArgs dgvcea = (DataGridViewCellEventArgs)t.Tag;
            MessageBox.Show("Single");
            //do whatever you do in single click
        }

        private void dataGridView1_CellClick_1(object sender, DataGridViewCellEventArgs e)
        {
            t.Tag = e;
            t.Start();
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            t.Stop();
            MessageBox.Show("Double");
            //do whatever you do in double click
        }
like image 156
Nikola Davidovic Avatar answered Nov 15 '22 19:11

Nikola Davidovic


Its with Windows problem. as far i know they haven't added anything special to handle it.

You can handle this -

  • a) just make single click something you'd want to happen before a double click, like select.
  • b) if that's not an option, then on the click event, start a timer. On the timer tick, do the single click action. If the double-click event occurs first, kill the timer, and do the double click action.

The amount of time you set the time for should be equal to the system's Double-Click time (which the user can specify in the control panel). It's available from System.Windows.Forms.SystemInformation.DoubleClickTime.

  • Checkout this MSDN link- here

  • Also this discussion Stackoverflow question asked earlier

like image 1
Sunny Avatar answered Nov 15 '22 19:11

Sunny


You can use RowHeaderMouseClick event of grid

    private void dgv_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
 {

    }
like image 1
user2408317 Avatar answered Nov 15 '22 18:11

user2408317