DataGridView the right mouse button to select a row
Source: Internet
Author: User
private void Datagridview1_cellmousedown (object sender, DataGridViewCellMouseEventArgs e)
{
if (E.button = = mousebuttons.right)
{
if (e.rowindex >= 0)
{
Datagridview1.clearselection ();
Datagridview1.rows[e.rowindex]. Selected = true;
Datagridview1.currentcell = Datagridview1.rows[e.rowindex]. Cells[e.columnindex];
Contextmenustrip1.show (mouseposition.x, MOUSEPOSITION.Y);
}
}
}
}
The CurrentRow property in DataGridView is read-only and its index cannot be set dynamically, so you can only select rows with the left button in DataGridView, which allows you to locate the current row.
Now to implement the right-click function in DataGridView, the code is as follows:
private void Datagridview1_cellmousedown (object sender, DataGridViewCellMouseEventArgs e)
{
if (E.button = = Mousebuttons.right && e.rowindex >-1 && e.columnindex >-1)
{
dataGridView1.CurrentRow.Selected = false;
Datagridview1.rows[e.rowindex]. Selected = true;
}
}
The DataGridView Cellmousedown event is added as code, and without the comment code, you can implement the uncheck for the currently selected row and select the row for the right mouse click
There is a problem with the CurrentRow property that is still the previous value, even if the selected of the right-click Row is set to true, it cannot be changed.
You can change the properties of the CurrentRow at the same time after you log off the comment code, so it's much easier to encode later.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service