64

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

Simply a way to right-click on a row, it will select the whole row and show a menu with an option to delete the row and when the option selected it will delete the row.

我做了一些尝试,但没有一个有效,而且看起来很乱。我该怎么办?

4

12 回答 12

110

我终于解决了:

  • 在 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();
}
于 2010-06-14T07:10:05.663 回答
40

为了这个问题的完整性,最好使用 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.
    }
}
于 2012-03-22T10:33:08.930 回答
11
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;
    }

多田:D。最简单的方式时期。对于自定义单元格,只需稍作修改。

于 2015-05-05T11:11:31.443 回答
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();
    }
}

这更容易。当然,您必须像已经提到的那样初始化 mousedown-event:

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

在你的构造函数中。

于 2011-09-24T12:51:43.223 回答
2

这个问题的所有答案都是基于鼠标点击事件。您还可以为您分配一个ContenxtMenuStripDataGridview检查当用户RightMouseButtons上是否有选择的行DataGridView并决定您是否要查看该行ContenxtMenuStrip。您可以通过CancelEventArgs.CancelContextMenuStrip

    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;
    }

但是,如果您有多个上下文菜单条,每个都包含不同的选项,具体取决于选择,我自己也会选择鼠标单击方法。

于 2013-10-28T12:52:36.643 回答
2

基于@Data-Base 的回答,在选择模式 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();
}
于 2016-10-29T18:25:45.737 回答
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();
}
于 2010-12-15T11:22:41.070 回答
1
private void dataGridView1_CellContextMenuStripNeeded(object sender, 
DataGridViewCellContextMenuStripNeededEventArgs e)
{            
    if (e.RowIndex != -1)
    {
        dataGridView1.ClearSelection();
        this.dataGridView1.Rows[e.RowIndex].Selected = true;
        e.ContextMenuStrip = contextMenuStrip1;
    }
}
于 2019-11-04T15:05:21.233 回答
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();
    }
}
于 2020-05-15T12:34:03.367 回答
0

您还可以通过在事件代码中使用以下内容来简化此操作:

private void MyDataGridView_MouseDown(object sender, MouseEventArgs e) 
{     
    if (e.Button == MouseButtons.Right)     
    {         
        rowToDelete = e.RowIndex;
        MyDataGridView.Rows.RemoveAt(rowToDelete);         
        MyDataGridView.ClearSelection();     
    } 
}
于 2012-05-14T17:23:44.700 回答
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;

就这么简单!

于 2013-05-04T01:08:32.000 回答
0

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

  1. 在您的表单中创建一个 contextMenuStrip 并创建一个项目
  2. 为此 contextMenuStrip 签署一次事件单击 (OnCancelarItem_Click) 在此处输入图像描述
  3. 在gridview上创建一个事件'UserDeletingRow' 在此处输入图像描述 ,现在......你已经模拟了来自用户的按键del

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

在此处输入图像描述 在此处输入图像描述 最后... 在此处输入图像描述

于 2018-02-02T23:39:34.840 回答