简体   繁体   English

在不按Control键的情况下选择多行

[英]Select multiple Rows without pressing Control Key

I have a gridview where I can select multiple rows by pressing a control key. 我有一个gridview,我可以通过按控制键选择多行。 Is it possible to achieve the same without pressing a control key. 是否可以在不按下控制键的情况下实现相同目的。

As the .net default action will also update the slectedrows of your datagridview you need to have an array to reserve the old selections: 由于.net默认操作也会更新datagridviewslectedrows ,因此您需要有一个数组来保留旧的选择:

DataGridViewRow[] old; 

which will be updated at CellMouseDown (before the default .net action modify your selections): 将在CellMouseDown更新(在默认.net操作修改您的选择之前):

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: 之后,您可以在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;
        }    
    }  
}

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. 您需要实现从原始datagridview导出的自己的datagridview ,并重写OnCellMouseDownOnCellMouseClick以取消默认的取消选择操作并使其平滑。 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...... 并在您的Form.Designer.cs中将DataGridView对象datagridview1 (如果是名称)更改为myDataGridView对象......

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 System.Windows.Forms.DataGridView() to

this.dataGridView1=new myDataGridView ()

最简单的方法是在每一行中放置一个复选框,并在标题上可能选中“全选”复选框。

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. 然后在单击它们时从列表中添加或删除它们......然后查看datagrid并选择列表中的行。

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

You need to subscribe to mouse click event and select the row programmatically by setting Selected property to true. 您需要订阅鼠标单击事件并通过将Selected属性设置为true以编程方式选择行。

Keep in mind that ff the row is already selected you need to set it to false so that the user is able to deselect the row. 请记住,如果已选择该行,则需要将其设置为false,以便用户可以取消选择该行。

我从来没有这样做过,但是当你按住其中一个按钮的同时移动鼠标时,应该可以使用MouseDown和MouseUp事件来选择光标下的任何行。

I know this reply is a bit late but for everyone.. 我知道这个回复有点晚了但对每个人来说......

you can use InputSimulator and do this: 你可以使用InputSimulator并执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM