DataGridView中实现右击选中当前行功能,并通过ContextMenuStrip获取当前行

2012-09-18

添加cell mouse down事件.

   private void gvShow_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)

        {

            if (e.Button == MouseButtons.Right)

            {

                gvShow.ClearSelection();

                gvShow.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected = true;

                gvShow.CurrentCell = gvShow.Rows[e.RowIndex].Cells[e.ColumnIndex];

            }

        }

测试效果,正确的alert行号

   private void TestToolStripMenuItem_Click(object sender, EventArgs e)

        {

            string str = gvShow.CurrentCell.RowIndex.ToString();

            MessageBox.Show(str);

        }

  

你可能感兴趣的:(datagridview)