Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# datagridview doubleclick on row with FullRowSelect

I have a datagridview in my C# application and the user should only be able to click on full rows. So I set the SelectionMode to FullRowSelect.

But now I want to have an Event which is fired when the user double clicks on a row. I want to have the row number in a MessageBox.

I tried the following:

 this.roomDataGridView.CellContentDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.roomDataGridView_CellCont‌ ​entDoubleClick); 

 private void roomDataGridView_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
 {
      MessageBox.Show(e.RowIndex.ToString());
 }

Unforunately nothing happens. What am I doing wrong?

like image 374
Metalhead89 Avatar asked Dec 04 '12 15:12

Metalhead89


5 Answers

In CellContentDoubleClick event fires only when double clicking on cell's content. I used this and works:

    private void dgvUserList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        MessageBox.Show(e.RowIndex.ToString());
    }
like image 101
cihadakt Avatar answered Oct 06 '22 04:10

cihadakt


Don't manually edit the .designer files in visual studio that usually leads to headaches. Instead either specify it in the properties section of your DataGridRow which should be contained within a DataGrid element. Or if you just want VS to do it for you find the double click event within the properties page->events (little lightning bolt icon) and double click the text area where you would enter a function name for that event.

This link should help

http://msdn.microsoft.com/en-us/library/6w2tb12s(v=vs.90).aspx

like image 29
Pseudonym Avatar answered Oct 06 '22 04:10

Pseudonym


You get the index number of the row in the datagridview using northwind database employees tables as an example:

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'nORTHWNDDataSet.Employees' table. You can move, or remove it, as needed.
            this.employeesTableAdapter.Fill(this.nORTHWNDDataSet.Employees);

        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            var dataIndexNo = dataGridView1.Rows[e.RowIndex].Index.ToString();
            string cellValue = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();

            MessageBox.Show("The row index = " + dataIndexNo.ToString() + " and the row data in second column is: "
                + cellValue.ToString());
        }
    }
}

the result will show you index number of record and the contents of the second table column in datagridview:

enter image description here

like image 35
Ashraf Sada Avatar answered Oct 06 '22 04:10

Ashraf Sada


This will work, make sure your control Event is assigned to this code, it has probably been lost, I also noticed that Double click will only work if the cell is not empty. Try double clicking on a cell with content, don't mess with the designer

 private void dgvReport_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
 {

   //do something


 }
like image 44
CR41G14 Avatar answered Oct 06 '22 04:10

CR41G14


I think you are looking for this: RowHeaderMouseDoubleClick event

private void DgwModificar_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) {
...
}

to get the row index:

int indice = e.RowIndex

like image 30
Said El Bakkali Avatar answered Oct 06 '22 05:10

Said El Bakkali