Support multiple clicks and additional functions

Hi, I am making an application in C #.

What I'm trying to achieve

  • I select multiple rows with CTRL among the results in a DataGridView
  • then I right click on it and shows the options
  • i Click on the selected option
  • Selected rows remain in the DataGridView, while other rows are deleted.

Problem when i'm stuck

  • I select multiple lines using CTRL
  • But when I find the right mouse button on the lines, the choice of several options

Note. I use the MouseClick event to right-click on rows and create a menu. The code for this event is shown below:

private void SearchPanelDataGridView_MouseClick(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        ContextMenuStrip smenu = new System.Windows.Forms.ContextMenuStrip();
        var htest = SearchPanelDataGridView.HitTest(e.X, e.Y);
        SearchPanelDataGridView.ClearSelection();
        SearchPanelDataGridView.Rows[htest.RowIndex].Selected = true;

        smenu.Items.Add("Clear Record").Name = "Clear Record";
        smenu.Items.Add("Pay Amount").Name = "Pay Amount";
        smenu.Items.Add("Break Apart").Name = "Break Apart";
        smenu.Items.Add("View Details").Name = "View Details";
        smenu.Items.Add("Choose Selected").Name = "Choose Selected"; // Choose Rows Option
        smenu.Items.Add("Reset").Name = "Reset";

        int CurrentMouseOverRow = SearchPanelDataGridView.HitTest(e.X, e.Y).RowIndex;
        smenu.Show(SearchPanelDataGridView, new Point(e.X, e.Y));
        smenu.ItemClicked += new ToolStripItemClickedEventHandler(smenu_ItemClicked);
    }
}
+4
source share
2 answers

MouseClick. ContextmenuStrip . ContextmenuStrip DataGridView, Click ToolStripMenuItem, :

dataGridView1.Rows.Cast<DataGridViewRow>()
    .Where(x => !dataGridView1.SelectedRows.Contains(x) && !x.IsNewRow)
    .ToList().ForEach(x => dataGridView1.Rows.Remove(x));

, , .

+1

, :

    List<int> selectedRowsIndexes = new List<int>();
    private void dataGridView1_Click(object sender, EventArgs e)
    {
        selectedRowsIndexes.Clear();
        foreach (DataGridViewRow row in dataGridView1.SelectedRows)
           selectedRowsIndexes.Add(row.Index);
    }

    private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            ContextMenuStrip smenu = new System.Windows.Forms.ContextMenuStrip();
            var htest = dataGridView1.HitTest(e.X, e.Y);
            dataGridView1.ClearSelection();
            dataGridView1.Rows[htest.RowIndex].Selected = true;

            smenu.Items.Add("Clear Record").Name = "Clear Record";
            smenu.Items.Add("Pay Amount").Name = "Pay Amount";
            smenu.Items.Add("Break Apart").Name = "Break Apart";
            smenu.Items.Add("View Details").Name = "View Details";
            smenu.Items.Add("Choose Selected").Name = "Choose Selected"; // Choose Rows Option
            smenu.Items.Add("Reset").Name = "Reset";

            int CurrentMouseOverRow = dataGridView1.HitTest(e.X, e.Y).RowIndex;
            smenu.Show(dataGridView1, new Point(e.X, e.Y));

            foreach (int rowIndex in selectedRowsIndexes)
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                   dataGridView1.Rows[i].Selected = (i == rowIndex);
                }
            }
        }
    }

DataGridView SelectionMode FullRowSelect.

0

Source: https://habr.com/ru/post/1686992/


All Articles