Find the row index on mouse click in datagridview

Ndebata
Posted by Ndebata under Windows Forms category on | Points: 40 | Views : 3624
Register mouse click event for the datagridview
private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
int _theight = 0;
string _Location = "";
if (e.Y >= _theight && e.Y <= _theight + dataGridView1.ColumnHeadersHeight)
_Location= "Header";
else
{
_Location = "";
_theight = _theight + dataGridView1.ColumnHeadersHeight;
foreach (DataGridViewRow item in dataGridView1.Rows)
{
if (e.Y >= _theight && e.Y <= _theight + item.Height)
{
_Location = "Row Index: " + item.Index;
break;
}
else
_theight = _theight + item.Height;
}
if(_Location== "")
_Location="Outside";
}
MessageBox.Show(_Location);
}

Comments or Responses

Login to post response