Select multiple rows without pressing a control key

I have a gridview where I can select multiple rows by pressing the control key. Is it possible to achieve this without pressing the control key.

+3
source share
6 answers

Since the default action .net will also update slectedrowsyours datagridview, you need to have an array to reserve the old options:

DataGridViewRow[] old; 

which will be updated in CellMouseDown(before the default .net action changes your choice):

private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
    old = new DataGridViewRow[dataGridView1.SelectedRows.Count];
    dataGridView1.SelectedRows.CopyTo(old,0);  
}

RowHeaderMouseClick ( RowHeaderSelect - datagridview selectionmode ) CellMouseClick FullRowSelect :

private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{    
    foreach (DataGridViewRow gr in old)
    {
        if (gr == dataGridView1.CurrentRow)
        {
            gr.Selected = false;
        }
        else
        {
            gr.Selected = true;
        }    
    }  
}

: :
datagridview, , OnCellMouseDown & OnCellMouseClick, . :

Using System;
Using System.Windows.Forms;

public class myDataGridView:DataGridView
{
    protected override void OnCellMouseDown(DataGridViewCellMouseEventArgs e)
    {
        //base.OnCellMouseDown(e);
        this.Rows[e.RowIndex].Selected = !this.Rows[e.RowIndex].Selected;
    }

    protected override void OnCellMouseClick(DataGridViewCellMouseEventArgs e)
    {
        //base.OnCellMouseClick(e);
    }
}

Form.Designer.cs datagridview datagridview1 ( ) myDataGridView......

:

private System.Windows.Forms.DataGridView dataGridView1; to

private myDataGridView dataGridView1;

this.dataGridView1=new System.Windows.Forms.DataGridView() to

this.dataGridView1=new myDataGridView ()
+5

- , , " " .

+1

, Selected true.

, , false, .

0

, MouseDown MouseUp, , , , .

0

I needed to do this for a touch screen, where I would like multi-selection with one click, without ugly flags. Why complicate it, just keep not Index'es

    private List<int> SelectedIndexs { get; set; }

Then add or remove them from the list as you click them ... Then browse the datagrid file and select the rows in the list.

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if (SelectedIndexs.Contains(e.RowIndex))
        {
            SelectedIndexs.Remove(e.RowIndex);
        }
        else
        {
            SelectedIndexs.Add(e.RowIndex);
        }

        foreach (DataGridViewRow row in this.dataGridView1.Rows)
        {
            row.Selected = SelectedIndexs.Contains(row.Index);
        }
    }
0
source

I know this answer was a bit late, but for everyone ..

You can use InputSimulator and do this:

InputSimulator sim = new InputSimulator();


    private void CommandeDataGridView_MouseHover(object sender, EventArgs e)
    {
        sim.Keyboard.KeyDown(WindowsInput.Native.VirtualKeyCode.LCONTROL);
    }

    private void CommandeDataGridView_MouseLeave(object sender, EventArgs e)
    {
        sim.Keyboard.KeyUp(WindowsInput.Native.VirtualKeyCode.LCONTROL);
    }
0
source

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


All Articles