Programmatically select rows in a grid

I need to programmatically select rows in a MultiSelect-capable grid.
I did it like this:

final MultiSelectionModel multiselectionModel = (MultiSelectionModel) myGrid.getSelectionModel(); multiselectionModel.deselectAll();; The getSelectedIds() method returns a Set of the select id’s as defined in the datasource belonging to the grid.

I changed the last line to setSelected(…).

In both variants no rows are selected = no checkboxes are set.

I’m working on Vaadin 7.6-SNAPSHOT.

Thanks for your help.

Hi Claus,

Is it possible that the second line of your code deselects all rows, so then there are no rows returned by getSelectedIds()?


The second line has to delete all the previous selections, because another part could have changed them.
The list of the currently selected IDs is returned by the getSelectedIds() method.

another part = another part of the application

I created a selection with the following code:

Grid grid = new Grid(); Set<Report> reports = getReports(); // Report is just a Java bean BeanItemContainer<Report> reportContainer = new BeanItemContainer<Report>( Report.class, reports); grid.setContainerDataSource(reportContainer); MultiSelectionModel msm = new MultiSelectionModel(); grid.setSelectionModel(msm); Iterator<Report> iter = reports.iterator(); // select two objects from the Set,; and it seems to work as it should, two items in the grid are selected.

EDIT: it also works if I change the last line to the following:

    Set<Report> three = new HashSet<Report>();

Strange, this looks much like my code apart from the Iterator part. I’ll test that …

I see a difference: You refer to complete Report objects, while my code relies on id’s.
I’ll rewrite my code to adapt your approach…

I can’t doublicate your approach, because I need BeanItemContainer and the data’s id to identify it.
But it should work with IDs (?).
Did you do your example using 7.6-SNAPSHOT? It might be a bug in the curent beta.

Yes, it should work with ids. MultiSelectionModel’s select() method will fail with an IllegalArgumentException if the paramer is not a proper id in the container:

if (!getParentGrid().getContainerDataSource().containsId(itemId)) { throw new IllegalArgumentException("Given item id (" + itemId + ") does not exist in the container"); } or if the input is null:

if (itemIds == null) { throw new IllegalArgumentException("itemIds may not be null"); } I tried also with 7.6-SNAPSHOT, with the same results.

Can you try something like the following:

final MultiSelectionModel multiselectionModel = (MultiSelectionModel) myGrid.getSelectionModel();
Set selectedIds = getSelectedIds();
                "Amount of selected ids: " + selectedIds.size());
                "Amount of selected rows in grid: " + myGrid.getSelectedRows().size());

Hi Olli
Thanks for your example. Do you refer to a particular implementation of getSelectedIds() or just a generic one returning the list of selected ids?
The number of select rows can be misleading in the context of the application, because I’ve to synchronize two ways to multi select by hand (one is the grid-based multi selection, the other is a custom implementation.
That’s why I have to deselectAll() before.
The only thing, that I have to do is set the multi selection of a grid programatically. When debugging, I see those items being selected, but the checkboxes in the grid aren’t set by the state change of the MultiSelectionModel.


have you tried sorting the grid after changing the state of the selection model? That should force a redraw of the grid, so you can be guaranteed that everything is as up to date as possible.

Otherwise, you’ll need to look further into your own code. From my limited trials, changing the selection model does reflect into the UI, but obviously you’re doing something different if it’s not working for you.


that’s a good idea. I’ll try that.
Is there any way to force a redraw?


Hi Olli
reordering the grid, doesn’t solve the issue.

Hi Olli
I suspect, that the issue is caused by the use of BeanContainer as opposed to BeanItemContainer (as in your example).
The handling of BeanItems based on their hashcode or their given id seems to be different. When I debug into the code, I see the right item ids, but the GUI isn’t updated.
We have to use the BeanContainer and Vaadin 7.6 in this project, because we need several new Vaadin features.
There is no way back :wink:


Just read your discussion and investigated a bit of Grid’s code. Seems that there is a case where it does not force the selected/deselected rows to update to the client. I added a ticket for it and made a quick fix. How ever, the original example using deselectAll and select should work, but apparently it doesn’t…

Can you create a quick vanilla Vaadin UI where we can see this issue happening? It would speed up the debugging process quite a lot. Thank you for your report!


Hi Teemu
I’m on a quite tight schedule, but I’ll create a quick example to show the behaviour.
Thanks for your support!

The issue with setSelected not updating has been fixed and merged. It should be present tomorrow in a 7.6-SNAPSHOT.

Great! Thank you very much.

While further evaluating the multi-selection behaviour I found the following.
This code works as excepted:

Grid grid = new Grid();

    Set<Report> reports = getReports(); // Report is just a Java bean
    BeanContainer<String, Report> reportContainer = new BeanContainer<>(Report.class);
    MultiSelectionModel msm = new MultiSelectionModel();
    grid.setSelectionModel(msm);"2", "3");

[/code]The following doesn’t select the reports:

Grid grid2 = new Grid();

    Set<Report> reports2 = getReports(); // Report is just a Java bean
    BeanContainer<String, Report> reportContainer2 = new BeanContainer<>(Report.class);
    MultiSelectionModel msm2 = (MultiSelectionModel)grid.getSelectionModel();
    msm2.setSelected("2", "3");

[/code]The only difference between these two is the location of the SelectionMode definition.

I see another one… You are doing a select in the first example vs a setSelected in the second one. Although I think it’s a typo :wink: