Programatically create a block selection

In the dhtmlx grid version 1.6 build 80319.
I would like to programatically create a block selection (in javascript code).
I tried the code given in this other question, but it doesn’t work.

dhtmlx.com/docs/products/kb/inde … 439&a=6354

I’m using the following code and my grid doesn’t show anymore.

// Grid initialisation
var mygrid = new dhtmlXGridObject();
mygrid.enableBlockSelection(true);
mygrid.enableDragAndDrop(false);
mygrid.attachEvent(“onBlockSelected”,function(){
  // Disable selection of first column
  if ( mygrid._selectionArea.LeftTopCol <= 0 || mygrid._selectionArea.RightBottomCol <= 0) {
    return false;
  }
  // This sends the selection to the server.
  gridSelection(mygrid._selectionArea.LeftTopRow,
    mygrid._selectionArea.LeftTopCol - 1,
    mygrid._selectionArea.RightBottomRow,
    mygrid._selectionArea.RightBottomCol - 1 );
  return true;
});
refreshGrid();

//the “refreshGrid” function is called when the grid changes. Sometimes new columns are added and the selection doesn’t get redrawn.
function refreshGrid()
{
            mygrid.clearAll();
            mygrid.loadXML(url);
            mygrid.init();

            mygrid._CreateSelection();
            mygrid_ShowSelection();
            mygrid._selectionArea = mygrid._RedrawSelectionPos(
                mygrid.cellByIndex(#{selectedRegion.leftTopRow},#{selectedRegion.leftTopCol}).cell,
                mygrid.cellByIndex(#{selectedRegion.rightBottomRow}, #{selectedRegion.rightBottomCol})).cell;
}

Thank you,
-Alim

Code which you are using is not exactly the same as was suggested.
Please check attached sample

1218187862.zip (92.9 KB)

It works!
Thank you,
I didn’t put the code inside a function as a parameter to loadXML.

-Alim

The data loading is async, so data related commands need to be loaded only after XML loading, which can be catched by using second parameter of loadXML command

Would it be possible to do this selection from the xml file as an parameter?

-Alim

You can call any method of grid from afterInit section , so it technically possible to call your custom code from afterInit, but it will not work for selection creation , because afterInit section processed when hea section parsed, but data not loaded in grid yet. There is no row in grid - so selection can’t be created.