C # DataGridView. What should I do when right-clicking RowHeader and right-clicking the menu ?,
private
void
dataGridView1_CellClick(
object
sender, DataGridViewCellEventArgs e)
{
if
( e.ColumnIndex==-1)
{
MessageBox.Show(
"This is the line Header"
);
}
}
private
void
dataGridView1_RowHeaderMouseClick(
object
sender, DataGridViewCellMouseEventArgs e)
{
if
(e.Button==MouseButtons.Right)
{
// Add a shortcut menu
}
}
Certainly, the code upstairs is not tested. Note: You need to right-click the title line and a shortcut menu appears.
Besides, CellClick does not return the right-click response. Therefore, the right-click response is only available in the CellMouseClick event. (RowHeaderMouseClick also responds, but I don't know why, my VS2005 does not respond, so it is not recommended)
Private void dgv_CellMouseClick (object sender, DataGridViewCellMouseEventArgs e)
{
// Right-click or not
If (e. Button = MouseButtons. Right)
{
// Obtain the row and column information of the click. Related Function query MSDN
DataGridView. HitTestInfo hitinfo = dgv_verify.HitTest (e. X, e. Y );
// If RowIndex <0, it is the title line.
If (hitinfo. RowIndex <0)
{
// If you only need to specify the column display menu, add the hitinfo. ColumnIndex judgment.
ContextMenuStrip1.Show (MousePosition. X, MousePosition. Y );
}
}
}
========================================================== ======================================
The above code has been tested and runs normally under VS2005 and VS2008.
Ask me another question. From: http://bbs.csdn.net/topics/370259768