Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select multiple Rows without pressing Control Key

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

like image 323
Hukam Avatar asked Feb 27 '23 01:02

Hukam


2 Answers

As the .net default action will also update the slectedrows of your datagridview you need to have an array to reserve the old selections:

DataGridViewRow[] old; 

which will be updated at CellMouseDown (before the default .net action modify your selections):

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

after that, you can do your changes in RowHeaderMouseClick (as RowHeaderSelect is the default datagridview selectionmode) or use CellMouseClick for FullRowSelect and reselect those old selected rows:

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

Edit: Better solution:
You need to implement your own datagridview derived from the original one and override OnCellMouseDown&OnCellMouseClick to cancel the default deselect action and make it smooth. make a new Class as something like this:

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

and in your Form.Designer.cs change DataGridView object datagridview1 (if that is the name) to myDataGridView object......

For example:Change

private System.Windows.Forms.DataGridView dataGridView1; to

private myDataGridView dataGridView1;

and change

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

this.dataGridView1=new myDataGridView ()
like image 197
Bolu Avatar answered Mar 05 '23 17:03

Bolu


I had to do this for a touch screen where I wanted multiselect on single click, without ugly check boxes. Why over complicate it, just keep a not of the Index'es

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

Then add or remove them from the list as you click them... Then look through you datagrid 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);
        }
    }
like image 31
Bob Smedly Avatar answered Mar 05 '23 19:03

Bob Smedly