Isn't this going to break the select of a highlighted row by pressing enter? I think the check should be as below and should come after the selectedRowIndex has been assigned on the lines below

if (hasSearchButton && selectedRowIndex == null) {
.....
}