If you use ListDataProvider to manage the DataGrid's data, then the DataGrid will not scroll when removing/adding items.
Here is a minimal example of removing grid rows without any scrolling (all contained within the entry point class):
Class for DataGrid Rows:
private class Item{
public String text;
public int value;
public Item(String text, int value){
this.text = text;
this.value = value;
}
}
Filling the DataGrid:
Here I use a private variable, data, to hold the items for the DataGrid. Note, that we must attach the dataGrid to data via the addDataDisplay method.
ListDataProvider data;
public void onModuleLoad() {
// build grid:
DataGrid dataGrid = new DataGrid();
BuildColumns(dataGrid);
dataGrid.setWidth("300px");
dataGrid.setHeight("300px");
// add items:
data = new ListDataProvider();
for(int i = 1; i < 25; i++){
data.getList().add(new Item("Item " + i, i));
}
data.addDataDisplay(dataGrid);
// display:
RootPanel.get().add(dataGrid);
}
Building the DataGrid:
This private method is used to build the columns for the DataGrid. Inside of the FieldUpdater for delCol, which is used to listen for click events for button columns, we remove the respective item from data, and call data.refresh() to update the DataGrid display.
private void BuildColumns(DataGrid dataGrid){
Column textCol = new Column(new SafeHtmlCell()) {
@Override
public SafeHtml getValue(Item object) {
SafeHtmlBuilder sb = new SafeHtmlBuilder();
sb.appendEscaped(object.text);
return sb.toSafeHtml();
}
};
dataGrid.addColumn(textCol);
dataGrid.setColumnWidth(textCol, 75, Unit.PCT);
Column deleteCol = new Column(new ButtonCell()) {
@Override
public String getValue(Item object) {
return "Delete " + object.value;
}
};
deleteCol.setFieldUpdater(new FieldUpdater() {
@Override
public void update(int index, Item object, String value) {
data.getList().remove(index);
data.refresh();
}
});
dataGrid.addColumn(deleteCol);
}
I put this code in a new GWT project and tested it. The DataGrid does not scroll when removing rows.