我在DataGridView中有几列,并且在我的行中有数据。我在这里看到了几个解决方案,但我无法将它们合并!用鼠标右键在Datagridview中选择一行并显示一个删除它的菜单
只需右键单击一行,它将选择整行并显示一个菜单,其中包含一个用于删除行的选项,选择该选项时将删除该行。
我做了很少的尝试,但没有工作,它看起来很混乱。我该怎么办?
我在DataGridView中有几列,并且在我的行中有数据。我在这里看到了几个解决方案,但我无法将它们合并!用鼠标右键在Datagridview中选择一行并显示一个删除它的菜单
只需右键单击一行,它将选择整行并显示一个菜单,其中包含一个用于删除行的选项,选择该选项时将删除该行。
我做了很少的尝试,但没有工作,它看起来很混乱。我该怎么办?
我终于解决了这个问题:
在Visual Studio中创建一个的ContextMenuStrip一个名为 “DeleteRow”
在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();
}
我希望代码可以帮助别人:-)
我欢迎任何修正,如果有一个错误。
你在DeleteRow_Click中使用了什么控件? – Crimsonland 2011-02-22 23:40:38
是的,它是点击 – 2011-04-28 07:57:31
当网格上出现'ContextMenuStripNeeded'事件时,这个回答似乎不起作用。使用'CellMouseDown'工作。 – 2013-04-25 14:59:41
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();
}
它更容易为鼠标按下只添加事件:
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事件:
this.MyDataGridView.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MyDataGridView_MouseDown);
在你的构造函数中。
为了完善这个问题,最好使用Grid事件而不是鼠标。
首先设置你的DataGrid的属性:
的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.Rows[rowSelected].Selected = true;
}
// you now have the selected row with the context menu showing for the user to delete etc.
}
}
当“ContextMenuStripNeeded”存在时,此答案有效。基于'MouseDown'的解决方案没有。 – 2013-04-25 14:58:15
@peter如果选中多行,如何获取行的索引? – Vbp 2013-12-22 19:53:00
嗨vbp,我认为你需要提出一个新的问题,如果还没有回答,这个问题是指只选择1行。 – peterincumbria 2013-12-29 10:48:17
您也可以使用该稍微简单的事件里面的代码如下:
private void MyDataGridView_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
rowToDelete = e.RowIndex;
MyDataGridView.Rows.RemoveAt(rowToDelete);
MyDataGridView.ClearSelection();
}
}
这只会删除没有警告或确认的行。我确定OP不希望那样。 – ProfK 2012-09-28 08:18:46
看到这里可以使用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;
然后你去,就这么简单!
对此问题提出的所有答案均基于鼠标单击事件。您还可以将ContenxtMenuStrip
分配给您的DataGridview
,并检查当用户RightMouseButtons在DataGridView
上时是否选择了一行,并决定是否要查看ContenxtMenuStrip
。您可以通过设置的ContextMenuStrip
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;
}
的的Opening event的CancelEventArgs.Cancel
值这样做,但如果你有几个上下文菜单条,用含有不同的选择,根据不同的选择,我会去一个鼠标器点击进入自己以及。
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。最简单的方式。对于自定义单元格只需修改一下。
最好的方法,也适用于仅使用键盘。但是需要警告:只有在连接了DataSource的情况下才有效。有关DataGridView.CellContextMenuStripNeeded事件的MSDN:“只有在设置了DataGridView控件的DataSource属性或其VirtualMode属性为true时,才会发生CellContextMenuStripNeeded事件。” – 2016-02-12 08:27:30
什么是'cmstrip'变量引用? – reformed 2016-04-06 20:50:38
上@资料库答案基地将无法正常工作,直到进行选择模式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();
}
你的问题太模糊。在遇到问题时添加更多详细信息。你想做什么,并不是很困难。 – leppie 2010-06-14 06:11:47