设置和理解ContextMenuStrip

时间:2018-01-24 16:34:44

标签: c# datagridview

首先: 我是一名仍在学习编程的学生。 问题是,当我右键单击一行时,输入dataGridView,RightClickDataView.Items.Add(“Abgegeben”);出现次数与我点击次数相同。我怎么能改变这个?

    private void dataGridViewBestellungen_MouseDown(object sender, MouseEventArgs e)
    {
        if(e.Button == MouseButtons.Right)
        {
            var hti = dataGridViewBestellungen.HitTest(e.X, e.Y);
            dataGridViewBestellungen.Rows[hti.RowIndex].Selected = true;
            RightClickDataView.Items.Add("Abgegeben");
            RightClickDataView.Show(Cursor.Position);
            var xy = dataGridViewBestellungen.SelectedRows;
            foreach (DataGridViewRow row in xy)
            {
                //take the id in the datagridview
            }
            RightClickDataView.ItemClicked += new ToolStripItemClickedEventHandler(rightclickmenu_ItemClicked);
            // close if mouse goes away from window
        }
    }

    private void rightclickmenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {
        ToolStripItem item = e.ClickedItem;
        dataGridViewBestellungen.ClearSelection();
        if (e.ClickedItem.Text == "Zurück")
        {
            //change the state to erledigt
        }
    }

1 个答案:

答案 0 :(得分:1)

只需从RightClickDataView.Items.Add("Abgegeben");中删除dataGridViewBestellungen_MouseDown,然后将此行放在构造函数上。