日期:2014-05-18  浏览次数:20924 次

如何让 右键点击dataGridView可以选中
在dataGridView中,只有点击鼠标左键才有效。
如何让点击鼠标右键dataGridView也有效,可以选中行呢。
谢谢!
有的话能不能给我个例子!


------解决方案--------------------
private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
int x = this.dataGridView1.HitTest(e.X,e.Y).RowIndex;
int y = this.dataGridView1.HitTest(e.X,e.Y).ColumnIndex;
if (x != -1 && y != -1)
this.dataGridView1.CurrentCell = this.dataGridView1.Rows[x].Cells[y];

}


}
------解决方案--------------------
探讨
private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
int x = this.dataGridView1.HitTest(e.X,e.Y).RowIndex;
int y = this.dataGridView1.HitTest(e.X,e.Y).ColumnIndex;
if (x != -1 && y != -1)
this.dataGridView1.CurrentCell = this.…

------解决方案--------------------
引用:
private void dataGridView1_MouseClick(object sender, MouseEventArgs e) 

if (e.Button == MouseButtons.Right) 

int x = this.dataGridView1.HitTest(e.X,e.Y).RowIndex; 
int y = this.dataGridView1.HitTest(e.X,e.Y).ColumnIndex; 
if (x != -1 && y != -1) 
this.dataGridView1.CurrentCell = this.…
------解决方案--------------------
private DataGridViewCell clickedCell;

private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
// If the user right-clicks a cell, store it for use by the shortcut menu.
if (e.Button == MouseButtons.Right)
{
DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);
if (hit.Type == DataGridViewHitTestType.Cell)
{
clickedCell =
dataGridView1.Rows[hit.RowIndex].Cells[hit.ColumnIndex];
}
}
}