views:

78

answers:

4

Hi,

In Windows form's Grid View i can select multiple rows by pressing control key.Is it possible to select multiple rows without pressing control key.

Thanks in advance

A: 

You need to subscribe to mouse click event and select the row programmatically by setting Selected property to 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.

Giorgi
A: 

I've never done it, but it should be possible to use the MouseDown and MouseUp events to select any rows that are under the cursor when you move the mouse while holding one of the buttons down.

Tom Bushell
+1  A: 

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

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 ()
Bolu
@Bolu but i am using FullRowSelect selection mode.I can't find FullRowSelectMouseClick event.
Gulshan
just use cellMouseClick then..
Bolu
I have tested u'r code but its look like that gird refreshed every time i select any row.
Gulshan
@Gulshan, it's refreshing because it was doing deselect and reselect. I think my answer already gives you the right solution for your question above, and from my answer you should have enough knowledge of what to do next even without my comments above, as I'm just trying to help and not supposed to do the work for you.....
Bolu
@Gulshan, have you tested my solution above? since no more comments from you after my update.
Bolu
+1  A: 

The easiest way out can be putting a checkbox in each row and possibly a "select all" checkbox on the header.

Kangkan