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.
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!
partial void EditableTasksGrid_InitializeDataWorkspace(List<IDataService> saveChangesTo)
// Write your code here.
this.FindControl(ITEMS_CONTROL).ControlAvailable += DemoItems_ControlAvailable;
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
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?
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.
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.
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
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.
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!
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.