右键选中事件 winform treeView1 DataGridView

treeView1  右键选中事件

private void treeView1_MouseDown(object sender, MouseEventArgs e)

        {
            if (treeView1.GetNodeAt(e.X, e.Y) == null)
            {
                return;
            }
            if (e.Button == MouseButtons.Right)
            {
                treeView1.SelectedNode = treeView1.GetNodeAt(e.X, e.Y);
            }

        }


DataGridView右键选中事件

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];
                 }
             }
    
            if (e.Button == MouseButtons.Right && e.RowIndex > -1 && e.ColumnIndex > -1)
            {
                dataGridView1.CurrentRow.Selected = false;
                dataGridView1.Rows[e.RowIndex].Selected = true;
            }
            MessageBox.Show(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
        }

你可能感兴趣的:(右键选中事件 winform treeView1 DataGridView)