【发布时间】:2019-06-21 11:41:59
【问题描述】:
我有一个 C# 项目 (VS 2017),它使用数据网格视图在对象列表中显示数据。使用 contextMenuStrip 我希望能够右键单击一行并能够将其从 datagridview 和基础数据源中删除。
我在 Datagridview 的属性中设置了 contextMenuStrip,其中一项具有以下处理事件的方法。
private void dgv_Test_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
var hti = dgv_Test.HitTest(e.X, e.Y);
dgv_Test.ClearSelection();
dgv_Test.Rows[hti.RowIndex].Selected = true;
}
}
private void cms_DGV_Remove_Click(object sender, EventArgs e)
{
MessageBox.Show("Content Menu Clicked on Remove Option");
PersonModel temp = (PersonModel)dgv_Test.CurrentRow.DataBoundItem;
string msg = $"The index for the selected Person is {temp.Id}.";
MessageBox.Show(msg);
}
我希望这会将当前行发送到右键单击的行。这不会发生,因为 CurrentRow 停留在第一行。如果我先左键单击该行然后右键单击同一行,它确实有效。
【问题讨论】:
标签: c# datagridview contextmenustrip