How To Enable Multiple Row Selection In LightSwitch's Grids

I'm extremely pleased to be able to share with the LightSwitch community, a method of enabling multiple grid row selection in LightSwitch, that finally doesn't involve any nasty workarounds, just a few lines of code.

VB.NET (2.8 MB)
5 Star
4,556 times
Add to favorites
E-mail Twitter Digg Facebook
Sign in to ask a question

  • Updated for VS2013?
    1 Posts | Last post July 29, 2015
    • Hi Yann,
      I'm doing a small project for a team I work for and found this bit of code which is exactly what I'm looking for (i.e. multi-select on a grid).  However, I have a problem.  When I get to the following bit of code there is no FindControl definition available and, thus, it is throwing an error.  What reference would this be pulling from?  Or, is there a workaround?  I'm a novice at this stuff, so any help would be appreciated.  Thanks!
      Begin code:
      partial void EditableTasksGrid_InitializeDataWorkspace(List<IDataService> saveChangesTo)
                      // Write your code here.
                    this.FindControl(ITEMS_CONTROL).ControlAvailable += DemoItems_ControlAvailable; 
  • Why "new" SelectionChangedEventHandler but "not new" for ControlAvailable?
    1 Posts | Last post July 07, 2013
    • Great workaround but the event handling stuff has me confused. When registering the ControlAvailable event handler we don't need the keyword "new" before assigning the delegate to call, but when registering SelectionChanged event handler we do.  Why?  Does LightSwitch create a ControlAvailable event handler for us by default but not for SelectionChanged or something? Also there is no "-+" operator to de-register these event handlers.  I was always paranoid in the past that you have to do that or can risk weird bugs creeping in.  Perhaps this is just because this event handling area has always confused me.  Can you help explain in context of this example, please?  Cool workaround and thanks for sharing, jbj
  • VS2012 Update 2
    3 Posts | Last post May 27, 2013
    • Hi Yann,
      I am really looking forward to using this although I am having some trouble.
      After I implemented your example, it seems the CanExecute logic of my buttons don't re-evaluate when the selection changes. I downloaded your example to try it and experience the same problem after VS auto-upgraded the project to VS2012 Update 2.
      I noticed your CanExecute logic was marked as a comment.
      Is this a known bug?
    • Hi Pieter,
      What code are you using in your CanExecute method? The only reason there's just a comment in my example is because I was leaving it up to the reader to put whatever code they wanted to
    • Hi Yann,
      Apologies for the delayed reply.
      My CanExecute method contained a check of how many items are selected. Some buttons should only be allowed to execute if 1 item is selected, others only if more than 1.
      In your example, when I un-commented your CanExecute logic, I experienced the same problem of the code not re-evaluating when the selection changed.
  • How about HTML Client
    2 Posts | Last post April 24, 2013
    • Hi Yann,
      have you got any thoughts on how to approach multi-select in the HTMLClient?
      Ed Richard
    • Sorry Ed, I don't use the HTML client, so I wouldn't have a clue. I suppose that one of these days I'm going to have to start playing with it.
  • Browser based application?
    2 Posts | Last post September 07, 2012
    • Hi Yann.
      First of all this is a very good work. Secondly can this work in browser based application? I have a user demand in which users want to be able to select multiple rows in grid and copy-paste them in excel, txt etc.
      Thanks in advance.
    • Thanks! It should also work in a browser-based application. All my code is doing is activating a capability that the data grid already has, but wasn't exposed in LightSwitch explicitly.
  • Can this code work for multiple grids ?
    3 Posts | Last post September 07, 2012
    • Hi Yann,
      I just tried out your code. It works great. Thanks.
       I have a question though on repeating the multiple selections over a series of grids. In other words, I select mutiple items from grid1 on tab1 on a page.There are stored as array/string and used to show items in a second grid.I multiple select items in second grid on tab2 and store the items selected as string/array for presentation of items in grid on third tab.
      I created a second set of Subs to keep grids logic separate- ie DemoItems_ControlAvailable and DemoItems2_ControlAvailable etc to keep choices and multiple selections separate from grid on tab1 from grid on tab2 which is a different entity.
      The logic worked on the first selection from first grid bringing up selected items on second grid, but when multiple selections are chosen on second grid it exits at  If (_itemsControl2 Is Nothing) Then Return just before second mutiple choices are intended to be built for second string.
      I did notice in DemoItems_ControlAvailable I did not have the equivalent of
      _itemsControl.SelectionChanged += New SelectionChangedEventHandler(ItemsControl_SelectionChanged) in it. Dont know what proper syntax is here in VB. It complained about needing to raise event.
      Any ideas why a second set of selected grid items would exit out at If (_itemsControl2 Is Nothing) Then Return?
      Sorry to bother you 
    • Actually I saw the error of my ways- I had the name of the second grid incorrect.
      It all works even with multiple grids- slick!
    • No bother at all. I'm glad you got it worked out!
  • Multiple selection solution
    2 Posts | Last post November 10, 2011
    • A huge missing piece on Lightswitch but resolved nicely! Thanks Yann, have been seeing your name and posts quite a lot since I started my LS development.
      Only thing on the code I had to change was to fit SelectionChangedEventHandler Delegation on ItemsControl_SelectionChanged method and it's good to go.
    • Thanks! I hope my posts have been helpful!
  • tip for deleting rows
    2 Posts | Last post October 12, 2011
    • hello, great example Yann looking foward for the book, I have a little problem because I want to delete multiple rows at a time, eith your example I went and did it with a little problem. The foreach loop throws a InvalidOperationException with multiple deletes.
      this is the code
       foreach (Competidor item in _itemsControl.SelectedItems)
                              Competidores.SelectedItem = item;
      well as you can see I'm interacting with the collection directly. I just bypass this with a magical try/catch and a return, and works fine with continuous rows (shift) but it doesn't work with not continous rows ( ctrl).
      Any tips would be great
      thanks a lot
    • Hi Freddy. That's very odd, whatever you can do to one selected item, you should be able to do with multiple items. Maybe "deleting" multiple items requires something a little different. WWhen I get a chance I'll try it out for myself. I've just got to get this book finished first, then I'll have time. Thanks for letting me know about this!
  • Use it on Many-To-Many?9
    2 Posts | Last post September 13, 2011
    • My life saver!! :) :)  : Just downloaded the code, but haven't tried it yet. 
    • I'm so glad you like it!
  • Missing Extension + Many-To-Many Selection
    3 Posts | Last post August 28, 2011
    • Excuse me Yann, but the previous question got posted b4 I finish my question.  I tried to open the project but getting an error said about missing extension "Luminous.Extension", but however I do see those DLLs attached in the project. Am I required to register them myself or can I download the installer somewhere? I have searched but found none.
      To come to my previous question, I understand from the screenshot that I can select multiple rows and save them to db (thx to Fang123Lu123), but is there a way to preserve the selection on a Many-to-Many relation? 
    • I'm sorry about the Luminous Extension accidentally being included. It's something that I'm working on, that I forgot to remove from the samples. Thanks for pointing that out! I'll remove it & upload a fixed solution. With your question about many-many, I'm not sure I understand what you're wanting to do. All this technique does is allow you to SELECT multiple rows, what you do with them is up to you. If you can do what you want with a single row, you can do the exactly the same thing with multiple rows.
    • An updated solution has been uploaded.
1 - 10 of 12 Items