DatagridView plus ContextMenuStrip in Winform realizes the right-click display menu, and can obtain data (the right-click menu will not be displayed where there is no data, and the right-click focus will be changed)

Show results

The check box in the first line is fully realized. Read the previous article, right-clicking on the gray area will not display the right-click menu.
insert image description here
After clicking the test button, a pop-up box will pop up to read the data information selected by the right button
insert image description here

Implementation process

Drag a ContextMenuStrip from the visual component and throw it into the DatagridView
insert image description here
Binding test button click event
insert image description here
insert image description here
insert image description here
test button click event

		private void 测试按钮_Click(object sender, EventArgs e)
        {
    
    
            List<int> list = new List<int>();
            //首先进行第一次循环 获取选中的行数
            for (int i = 0; i < dgv.Rows.Count; i++)
            {
    
    
                //如果被选中
                if ((bool)dgv.Rows[i].Cells[0].EditedFormattedValue == true)
                {
    
    
                    list.Add(i);
                }
            }

            //新建 选中长度的数组存放每一行
            string[] str = new string[list.Count];
            int j = 0;
            foreach (int i in list)
            {
    
    
                str[j++] = dgv.Rows[i].Cells[1].Value.ToString() + "@" + dgv.Rows[i].Cells[2].Value.ToString();
            }

            //输出选中所有行的内容
            for (int i = 0; i < str.Length; i++)
            {
    
    
                MessageBox.Show(string.Format("获取的第{0}条为:", i + 1) + str[i]);
            }
        }

Bind dgv (my DatagridView is called dgv) right click event
insert image description here
insert image description here

		/// <summary>
        /// dgv鼠标右键,焦点改变到当前点击处,并选择改行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Dgv_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
    
    
            if (e.RowIndex >= 0)
            {
    
    
                Console.WriteLine(dgv.Rows[e.RowIndex].Cells[1].Value);
                //判断是否右键和是否有数据
                if (e.Button == MouseButtons.Right && dgv.Rows[e.RowIndex].Cells[1].Value != null)
                {
    
    
                    dgv.ClearSelection();
                    dgv.Rows[e.RowIndex].Cells[0].Value = 1;
                    //焦点改变
                    dgv.CurrentCell = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex];

                    this.contextMenuStrip1.Show(Control.MousePosition);
                }
            }
        }
如果不考虑是否有数据才显示右键菜单,可以直接在DatagridView 属性中设置如下,但注意如果有此设置,则上述绑定的dgv右键点击事件实现效果不会生效

insert image description here
Directly select your ContextMenuStrip component and it can be displayed anywhere in dgv

reference documents

Datagridview right-click to select the cell and get the focus
c# winform to get the gridview data
right-click. Click to judge. After the row is selected, the right-click menu will pop up

Guess you like

Origin blog.csdn.net/munangs/article/details/132188123