How to get selected item (row) value in Vaadin grid?

I have a grid with one column named “Object”. I have one row/item under the column with the value of “Location”. When I try to get the string value of the row, I get an int value of 1. I need the string value of the row which is “Location”, not the number of the row:

queryLabel1.setValue(grid.getSelectedRow().toString());

How can I get the string value of this row when it is selected and put the value in a Label?

Hi Michael,

With “grid.getSelectedRow()” you will get the
item id
of the currently selected row (assuming single selection mode) or
null
if nothing is selected. So, to get the value, you need to retrieve the item from the grid data source, such as:

Object selected = ((SingleSelectionModel) grid.getSelectionModel()).getSelectedRow();
grid.getContainerDataSource().getItem(selected).getItemProperty("Object")).getValue();

Please checkout the grid component
documentation
for further details.

Regards,
Goran

Thanks so much. This is exactly what I was missing.

Glad I could help out!

Sorry to bring back an old thread but while this worked fine in vaadin 7 it doesn’t seen to work any more with the new grid in vaadin 8. Is there some way to do this now ?

Hi Nick,

I’d recommend using the SelectionModel API for finding the selected item like this:

grid.getSelectionModel().getFirstSelectedItem() The returned value is an Optional of Grid data type. In the case of Grid in single selection mode, the optional either contains the currently selected row or value is not present. The behaviour is a bit less defined for the multiple selection and
grid.getSelectedItems()
should be used instead.

The Columns in Grid are defined with a ValueProvider that you can re-use for getting the column value for a specific row at any point:

ValueProvider<MyBean, String> locationProvider = myBean -> myBean.getLocation().toString();
grid.addColumn(locationProvider).setCaption("Location");

/* And later when you need the Location of a selected item: */
grid.getSelectionModel().getFirstSelectedItem().ifPresent(item -> {
    String location = locationProvider.apply(item);
    /* Do what you want with the location */
});

Hope this helps you forward. If you have any further questions or if I did not answer your question, don’t hesitate to ask more!

//Teemu

Hello all, new writing in this forums and i didn’t wanted to open a new thread for something similar asked.
In my case we have selection model.NONE and when the user right clicks an item we should do X, but it seems imposible to get itemId as for example in table where things like these were possible.

we work with 7.6.6 could be there a solution for this version and for when we change to vaadin 8?

Thank you all a lot and have a nice day.

Did you try adding an ItemClickListener?
https://vaadin.com/api/7.7.6/com/vaadin/ui/Grid.html#addItemClickListener-com.vaadin.event.ItemClickEvent.ItemClickListener-

You get the clicked item and the mouse button that was clicked from the event.

-Olli

Note that the API has changed slightly in Vaadin 8, but the same method exists there as well.

-Olli

Hello Olli,
I think I asked in the wrong thread ( had multiple open sorry), but continuing on the topic,
I already tried it, i’m migrating table to grid and grid doesn’t seem to recognise te right click as an clicking event, there is no contextlistener, contextmenu but i tried with it too but the event doesn’t returns the same information as in itemclick