不按控制键就可以选择多行 [英] Select multiple Rows without pressing Control Key

查看:98
本文介绍了不按控制键就可以选择多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gridview,我可以通过按下一个控制键来选择多行。是否有可能在没有按下控制键的情况下实现同样的功能。

解决方案

由于.net默认动作也会更新<$ code> datagridview 您需要有一个数组来保留旧选择:

  DataGridViewRow [] old; 

将在 CellMouseDown 默认的.net动作修改你的选择):
$ b $ pre $ code 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)
{
if(gr == dataGridView1.CurrentRow)
{
gr.Selected = false;
}
else
{
gr.Selected = true;
}
}
}

编辑:更好解决方案:

您需要实现自己的 datagridview 从原始派生而来,并覆盖 OnCellMouseDown & OnCellMouseClick 取消默认的取消选择操作并使其平滑。使一个新的类如下所示:

 使用System; 
使用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;
}

保护覆盖无效OnCellMouseClick(DataGridViewCellMouseEventArgs e)
{
//base.OnCellMouseClick();


$ / code $ / pre

并在Form.Designer.cs中更改 DataGridView object datagridview1 (如果这是名称)为 myDataGridView 对象......



例如:Change



private System.Windows .Forms.DataGridView dataGridView1; to

  private myDataGridView dataGridView1; 

并更改

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

  this。 dataGridView1 = new myDataGridView()


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.

解决方案

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 ()

这篇关于不按控制键就可以选择多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆