Reputation: 1910
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?
Upvotes: 32
Views: 134126
Reputation: 11
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
Upvotes: 1
Reputation: 1647
For your purpose, there is a default event when the row header is double-clicked. Check the following code,
private void dgvCustom_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
//Your code goes here
}
Upvotes: 0
Reputation: 36
you can do this by : CellDoubleClick
Event
this is code.
private void datagridview1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
MessageBox.Show(e.RowIndex.ToString());
}
Upvotes: 0
Reputation: 4905
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:
Upvotes: 5
Reputation: 3214
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());
}
Upvotes: 35
Reputation: 2072
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
Upvotes: 6
Reputation: 5594
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
}
Upvotes: 4