1

I need shift Key support with Mouse button.My requirement is to select multiple rows by select first row and then click on last row of grid with shift press ,it select all rows between first row and last row.

How to implement this in ASP.NET Gridview

Jasmine
  • 5,186
  • 16
  • 62
  • 114
  • Have you tried GridView.OptionsSelection.MultiSelect = True; ? – Seano666 Oct 24 '14 at 04:26
  • @Seano666: I am afraid I do not have either MultiSelect or OptionSelection. Please help me. Why is this grid soo frustrating – Jasmine Oct 24 '14 at 04:59
  • Sorry I was reading something about a custom framework GridView control. It looks like you can multi-select with checkboxes in an ASP GridView, but I do not see that this client-side multi-select is an option. If you really need this functionality, look into Telerik RadGrid or Dev Express custom ASP controls. – Seano666 Oct 24 '14 at 16:25
  • Does this help? http://www.ezineasp.net/post/GridView-Select-Multiple-Rows-using-C-ASP-Net-AJAX.aspx – Seano666 Oct 24 '14 at 16:51
  • @Seano666: No, I already came across this, didn't help my situation as mine doesn't contain a checkbox. Is there any solution? – Jasmine Oct 27 '14 at 01:14

1 Answers1

2

You should do this in client side. There you must use JavaScript or JQuery along with CSS. And, you should know that a GridView is ultimately converted to a HTML table in client side. Here's a tip for you to try out. Follow these steps.

Assumption - Each row in your GridView has a row number and a hiddenfield to indicate whether it's selected or not (You can easily do this when you load data).

  1. Create two CSS classes named SelectedRow and DefaultRow (You may use any name you like for these classes).

  2. When you load the table for the first time all rows should have the CSS class DefaultRow and hidden field value set to 0 (or any value you prefer to indicate the rows are not selected).

  3. Write JavaScript or JQuery to handle first row (TR) click (You won't press Shift at this stage) and change the CSS class of that row (TR) to SelectedRow and the hidden field value to 1. And, save this first row number in a separate hidden field to easily identify the first row selected.

NOTE: In this same function you should set rest of the rows CSS class to DefaultRow. This is just to clear all selections if the user just clicks without Shift key.

  1. Now. write JavaScript or JQuery to handle Shift + Click on HTML row and change the CSS class of that row (TR) and all other rows (+ or - based on the first row number) from first row selected (Row number you just saved in your hidden field) to SelectedRow. At this stage you should set all hidden field values of selected rows to 1.

Here's an example to handle Shift + Click event in JQuery

$(document).click(function(e) {
    if (e.shiftKey) {
        alert("shift+click")
    } 
});
  1. Now you can read your hidden field values from your code behind and do any processing you like on the selected rows.

Hope you understand the logic here. :)

Sam
  • 2,917
  • 1
  • 15
  • 28
  • Thank you Sam, I am afraid I completed this using some C# code and using Shift button ascii 16. I do not know much Javascript or jquery etc, but somehow I achieved what I wanted. Its solved now, thank you so much for your time. – Jasmine Oct 27 '14 at 06:08
  • 1
    No worries. I didn't have enough time to write the code but will do that when I get a chance for future reference. Cheers! – Sam Oct 27 '14 at 06:17
  • Sam, no problem, thank you. If you could help me here, it would be highly appreciated http://stackoverflow.com/questions/26582045/how-to-navigate-up-or-down-in-asp-net-gridview-using-arrow-keys – Jasmine Oct 27 '14 at 06:22