Raúl Eduarte
Raúl Eduarte

Reputation: 21

Setting up and understanding ContextMenuStrip

first of all: I´m a student that is still learning about programming. The problem is that when I right click in a row insde the dataGridView, the RightClickDataView.Items.Add("Abgegeben"); appears as many times as I click. How can I change this?

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

Upvotes: 1

Views: 17

Answers (1)

Eric Lira
Eric Lira

Reputation: 11

Just remove RightClickDataView.Items.Add("Abgegeben"); from your dataGridViewBestellungen_MouseDown and place this line on your constructor for example.

Upvotes: 1

Related Questions