右键单击以选择Datagridview中的行并显示一个菜单以将其删除

时间:2010-06-14 05:37:31

标签: c# select datagridview contextmenu right-click

我的DataGridView中有几列,并且我的行中有数据。我在这里看到了一些解决方案,但我无法将它们结合起来!

只需右键单击一行,它就会选择整行并显示一个菜单,其中包含一个删除行的选项,当选择该选项时,它将删除该行。

我做了几次尝试,但没有一个工作,看起来很乱。我该怎么办?

12 个答案:

答案 0 :(得分:95)

我终于解决了它:

  • 在Visual Studio中,使用名为“DeleteRow”的项目创建ContextMenuStrip

  • 然后在DataGridView链接上使用ContextMenuStrip

使用下面的代码帮我搞定了。

this.MyDataGridView.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MyDataGridView_MouseDown);
this.DeleteRow.Click += new System.EventHandler(this.DeleteRow_Click);

这是很酷的部分

private void MyDataGridView_MouseDown(object sender, MouseEventArgs e)
{
    if(e.Button == MouseButtons.Right)
    {
        var hti = MyDataGridView.HitTest(e.X, e.Y);
        MyDataGridView.ClearSelection();
        MyDataGridView.Rows[hti.RowIndex].Selected = true;
    }
}

private void DeleteRow_Click(object sender, EventArgs e)
{
    Int32 rowToDelete = MyDataGridView.Rows.GetFirstRow(DataGridViewElementStates.Selected);
    MyDataGridView.Rows.RemoveAt(rowToDelete);
    MyDataGridView.ClearSelection();
}

答案 1 :(得分:31)

对于这个问题的完整性,最好使用Grid事件而不是鼠标。

首先设置您的数据网格属性:

SelectionMode为FullRowSelect 和 RowTemplate / ContextMenuStrip到上下文菜单。

创建CellMouseDown事件: -

private void myDatagridView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        int rowSelected = e.RowIndex;
        if (e.RowIndex != -1)
        {
            this.myDatagridView.ClearSelection();
            this.myDatagridView.Rows[rowSelected].Selected = true;
        }
        // you now have the selected row with the context menu showing for the user to delete etc.
    }
}

答案 2 :(得分:8)

private void dgvOferty_CellContextMenuStripNeeded(object sender, DataGridViewCellContextMenuStripNeededEventArgs e)
    {
        dgvOferty.ClearSelection();
        int rowSelected = e.RowIndex;
        if (e.RowIndex != -1)
        {
            this.dgvOferty.Rows[rowSelected].Selected = true;
        }
        e.ContextMenuStrip = cmstrip;
    }
TADA:D。最简单的方式。对于自定义单元格,只需修改一下。

答案 3 :(得分:3)

仅为mousedown添加事件要容易得多:

private void MyDataGridView_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        var hti = MyDataGridView.HitTest(e.X, e.Y);
        MyDataGridView.Rows[hti.RowIndex].Selected = true;
        MyDataGridView.Rows.RemoveAt(rowToDelete);
        MyDataGridView.ClearSelection();
    }
}

这更容易。对于cource,你必须如下所述初始化你的mousedown事件:

this.MyDataGridView.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MyDataGridView_MouseDown);

在你的构造函数中。

答案 4 :(得分:2)

此问题中提出的所有答案均基于鼠标点击事件。您还可以为ContenxtMenuStrip分配DataGridview,并检查DataGridView上的用户 RightMouseButtons 时是否选择了行,并决定是否要查看ContenxtMenuStrip与否CancelEventArgs.Cancel。您可以通过在ContextMenuStrip

Opening event中设置 private void MyContextMenuStrip_Opening(object sender, CancelEventArgs e) { //Only show ContextMenuStrip when there is 1 row selected. if (MyDataGridView.SelectedRows.Count != 1) e.Cancel = true; } 值来实现此目的
{{1}}

但是如果你有几个上下文菜单条,每个菜单条包含不同的选项,根据选择,我也会自己选择鼠标点击方式。

答案 5 :(得分:2)

基于@ Data-Base答案,在make选择模式FullRow

之前它将无效
  MyDataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

但是如果你需要让它在CellSelect模式下工作

 MyDataGridView.SelectionMode = DataGridViewSelectionMode.CellSelect;

 // for cell selection
 private void MyDataGridView_MouseDown(object sender, MouseEventArgs e)
 {
  if(e.Button == MouseButtons.Right)
    {
       var hit = MyDataGridView.HitTest(e.X, e.Y);
       MyDataGridView.ClearSelection();

       // cell selection
       MyDataGridView[hit.ColumnIndex,hit.RowIndex].Selected = true;
   }
}

private void DeleteRow_Click(object sender, EventArgs e)
{
   int rowToDelete = MyDataGridView.Rows.GetFirstRow(DataGridViewElementStates.Selected);
   MyDataGridView.Rows.RemoveAt(rowToDelete);
   MyDataGridView.ClearSelection();
}

答案 6 :(得分:1)

private void MyDataGridView_MouseDown(object sender, MouseEventArgs e)
{
    if(e.Button == MouseButtons.Right)
    {
        MyDataGridView.ClearSelection();
        MyDataGridView.Rows[e.RowIndex].Selected = true;
    }
}

private void DeleteRow_Click(object sender, EventArgs e)
{
    Int32 rowToDelete = MyrDataGridView.Rows.GetFirstRow(DataGridViewElementStates.Selected);
    MyDataGridView.Rows.RemoveAt(rowToDelete);
    MyDataGridView.ClearSelection();
}

答案 7 :(得分:1)

对我来说没有任何错误:

this.dataGridView2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MyDataGridView_MouseDown); 
this.dataGridView2.Click += new System.EventHandler(this.DeleteRow_Click);

还有这个

private void MyDataGridView_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        var hti = dataGridView2.HitTest(e.X, e.Y);
        dataGridView2.ClearSelection();
        dataGridView2.Rows[hti.RowIndex].Selected = true;
    }

}


private void DeleteRow_Click(object sender, EventArgs e)
{
    Int32 rowToDelete = dataGridView2.Rows.GetFirstRow(DataGridViewElementStates.Selected);
    if (rowToDelete == -1) { }
    else 
    {
        dataGridView2.Rows.RemoveAt(rowToDelete);
        dataGridView2.ClearSelection();
    }
}

答案 8 :(得分:0)

您还可以通过在事件代码中使用以下内容使其更简单:

private void MyDataGridView_MouseDown(object sender, MouseEventArgs e) 
{     
    if (e.Button == MouseButtons.Right)     
    {         
        rowToDelete = e.RowIndex;
        MyDataGridView.Rows.RemoveAt(rowToDelete);         
        MyDataGridView.ClearSelection();     
    } 
}

答案 9 :(得分:0)

点击此处可以使用DataGridView RowTemplate属性完成。

注意:此代码未经过测试,但我之前使用过此方法。

// Create DataGridView
DataGridView gridView = new DataGridView();
gridView.AutoGenerateColumns = false;
gridView.Columns.Add("Col", "Col");

// Create ContextMenu and set event
ContextMenuStrip cMenu = new ContextMenuStrip();
ToolStripItem mItem = cMenu.Items.Add("Delete");
mItem.Click += (o, e) => { /* Do Something */ };           

// This makes all rows added to the datagridview use the same context menu
DataGridViewRow defaultRow = new DataGridViewRow();
defaultRow.ContextMenuStrip = cMenu;

你去,就这么简单!

答案 10 :(得分:0)

我有一个新的解决方法来获得相同的结果,但代码更少。 对于Winforms ......这个例子是葡萄牙语 一步一步跟进

  1. 在表单中创建contextMenuStrip并创建一个项目
  2. 为此contextMenuStrip签署一个事件单击(OnCancelarItem_Click) enter image description here
  3. 创建活动' UserDeletingRow'在gridview上 enter image description here 现在......你在用户的关键新闻del上进行模拟
      
        

    你不要忘记在gridview上启用删除,对吧?!

      
  4. enter image description here enter image description here 最后...... enter image description here

答案 11 :(得分:0)

private void dataGridView1_CellContextMenuStripNeeded(object sender, 
DataGridViewCellContextMenuStripNeededEventArgs e)
{            
    if (e.RowIndex != -1)
    {
        dataGridView1.ClearSelection();
        this.dataGridView1.Rows[e.RowIndex].Selected = true;
        e.ContextMenuStrip = contextMenuStrip1;
    }
}