在窗体中添加DataGridView控件和ConTextMenuStrip1控件,修改DataGridView属性,将contextMenuStrip控件绑定dataGridView控件 this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; //选中整行可在属性
在窗体中添加DataGridView控件和ConTextMenuStrip1控件,修改DataGridView属性,将contextMenuStrip控件绑定dataGridView控件
this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; //选中整行可在属性中修改 datagridview.AutoGenerateColumns = false;//不让datagridview自动生成列,可在属性中修改 datagridview.AllowUserToAddRows = true;//禁止自动生成行可在属性中修改
代码实现选中一行,右键出现菜单
private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e) { try { 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); } } } catch (Exception) { MessageBox.Show("请选择内容"); }
总结
到此这篇关于C# 实现dataGridView选中一行右键出现菜单的文章就介绍到这了,更多相关C# dataGridView选中一行右键出现菜单内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!