AhmedShawky
AhmedShawky

Reputation: 910

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

Upvotes: 6

Views: 14992

Answers (3)

user2408317
user2408317

Reputation: 19

You can use RowHeaderMouseClick event of grid

    private void dgv_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
 {

    }

Upvotes: 1

Nikola Davidovic
Nikola Davidovic

Reputation: 8656

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
        }

Upvotes: 1

Sunny
Sunny

Reputation: 648

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.

Upvotes: 1

Related Questions