DataGridView通过右键选中行及更新Index
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) { if (e.Button == MouseButtons.Right) { if (e.RowIndex > -1) { if (!this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected) { this.dataGridView1.ClearSelection(); this.dataGridView1.Rows[e.RowIndex].Selected = true; this.dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]; } } } }
以上代码是CellMouseDown事件触发的
private void dataGridView1_MouseDown(object sender, MouseEventArgs e) { DataGridView.HitTestInfo rows = this.dataGridView1.HitTest(e.X, e.Y); if (e.Button == MouseButtons.Right) { if (rows.RowIndex > -1) { if (!this.dataGridView1.Rows[rows.RowIndex].Cells[rows.ColumnIndex].Selected) { this.dataGridView1.ClearSelection(); this.dataGridView1.Rows[rows.RowIndex].Selected = true; this.dataGridView1.CurrentCell = dataGridView1.Rows[rows.RowIndex].Cells[rows.ColumnIndex]; } } } }
这个是MouseDown事件触发
实现右击选中整行,并改变CurrentRow.Index为选中的那一行