DGV上的鼠标事件,MouseDown 、MouseUp、CellMouseClick等事件可参考。
if (MouseButtons == MouseButtons.Right)
MessageBox.Show("OK");
// DataGridView 的 ContextMenuStrip 设定
DataGridView1.ContextMenuStrip = this.ContextMenuStrip1;
// 列的 ContextMenuStrip 设定
DataGridView1.Columns[0].ContextMenuStrip = this.ContextMenuStrip2;
// 列头的 ContextMenuStrip 设定
DataGridView1.Columns[0].HeaderCell.ContextMenuStrip = this.ContextMenuStrip2;
// 行的 ContextMenuStrip 设定
DataGridView1.Rows[0].ContextMenuStrip = this.ContextMenuStrip3;
// 单元格的 ContextMenuStrip 设定
DataGridView1[0, 0].ContextMenuStrip = this.ContextMenuStrip4;
对于单元格上的右键菜单的设定,优先顺序是: Cell > Row > Column > DataGridView
? CellContextMenuStripNeeded、RowContextMenuStripNeeded 事件
利用 CellContextMenuStripNeeded 事件可以设定单元格的右键菜单,尤其但需要右键菜单根据单元格值的变化而变化的时候。比起使用循环遍历,使用该事件来设定右键菜单的效率更高。但是,在DataGridView使用了DataSource绑定而且是VirtualMode的时候,该事件将不被引发。
// CellContextMenuStripNeeded事件处理方法
private void DataGridView1_CellContextMenuStripNeeded(object sender,
DataGridViewCellContextMenuStripNeededEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
if (e.RowIndex < 0)
{
// 列头的ContextMenuStrip设定
e.ContextMenuStrip = this.ContextMenuStrip1;
}
else if (e.ColumnIndex < 0)
{
// 行头的ContextMenuStrip设定
e.ContextMenuStrip = this.ContextMenuStrip2;
}
else if (dgv[e.ColumnIndex, e.RowIndex].Value is int)
{
// 如果单元格值是整数时
e.ContextMenuStrip = this.ContextMenuStrip3;
}
}
//同样,可以通过 RowContextMenuStripNeeded 事件来设定行的右键菜单。
// RowContextMenuStripNeeded事件处理方法
private void DataGridView1_RowContextMenuStripNeeded(object sender,
DataGridViewRowContextMenuStripNeededEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
// 当"Column1"列是Bool型且为True时、设定其的ContextMenuStrip
object boolVal = dgv["Column1", e.RowIndex].Value;
Console.WriteLine(boolVal);
if (boolVal is bool && (bool)boolVal)
{
e.ContextMenuStrip = this.ContextMenuStrip1;
}
}
//CellContextMenuStripNeeded 事件处理方法的参数中、「e.ColumnIndex=-1」表示行头、「e.RowIndex=-1」表示列头。RowContextMenuStripNeeded则不存在「e.RowIndex=-1」的情况。
//引用地址 http://qianshao.blog.51cto.com/935360/201791
//How do you select a datagridview row on a right-click?
// Clear all the previously selected rows
foreach (DataGridViewRow row in yourDataGridView.Rows)
{
row.Selected = false;
}
// Get the selected Row
DataGridView.HitTestInfo info = yourDataGridView.HitTest( e.X, e.Y );
// Set as selected
yourDataGridView.Rows[info.RowIndex].Selected = true;
//You can use JvR's code in the MouseDown event of your DataGridView.
private void SubClassedGridView_MouseDown(object sender, MouseEventArgs e)
{
// Sets is so the right-mousedown will select a cell
DataGridView.HitTestInfo hti = this.HitTest(e.X, e.Y);
// Clear all the previously selected rows
this.ClearSelection();
// Set as selected
this.Rows[hti.RowIndex].Selected = true;
}
private void dataGridView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
dataGridView.CurrentCell = dataGridView[e.ColumnIndex, e.RowIndex];
}
}
控制右键菜单出现的位置为鼠标点击的地方呢?
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
int r = this.dataGridView1.HitTest(e.X, e.Y).RowIndex;
int c = this.dataGridView1.HitTest(e.X, e.Y).ColumnIndex;
if (r >= 0 && c >= 0) //点中Cell
{
DataGridViewCell hitcell = this.dataGridView1.Rows[r].Cells[c];
Point pscreen = this.dataGridView1.PointToScreen(e.Location);
Point pForm = this.PointToClient(pscreen);
this.textBox1.Text = pscreen.ToString(); //相对屏幕的坐标
this.textBox2.Text = pForm.ToString(); //相对当前Form的坐标
}
else //没点中Cell
{
}
}