Selection in Spreadsheet Control

7 Jan 202316 minutes to read

Selection provides interactive support to highlight the cell, row, or column that you select. Selection can be done through Mouse, Touch, or Keyboard interaction. To enable selection, set mode as Single or Multiple in selectionSettings. If you set mode to None, it disables the UI selection.

NOTE

The default value for mode in selectionSettings is Multiple.

You have the following options in Selection,

  • Cell selection
  • Row selection
  • Column selection

Cell selection

Cell selection is used to select a single or multiple cells. It can be performed using the selectRange method.

User Interface:

  • Click on a cell to select it (or) use the arrow keys to navigate to it and select it.
  • To select a range, select a cell, then use the left mouse button to select and drag over to other cells (or) use the Shift + arrow keys to select the range.
  • To select non-adjacent cells and cell ranges, hold Ctrl and select the cells.

You can quickly locate and select specific cells or ranges by entering their names or cell references in the Name box, which is located to the left of the formula bar, and also select named or unnamed cells or ranges by using the Go To (Ctrl+G) command.

Row selection

Row selection is used to select a single or multiple rows.

User Interface:

You can perform row selection in any of the following ways,

  • By clicking the row header.
  • To select multiple rows, select a row header with the left mouse button and drag over to other row headers (or) use the Shift + arrow keys to select multiple rows.
  • To select non-adjacent rows, hold Ctrl and select the row header.
  • You can also use the selectRange method for row selection.

The following sample shows the row selection in the spreadsheet, here selecting the 5th row using the selectRange method.

<ejs-spreadsheet id="spreadsheet" created="createHandler">
    <e-spreadsheet-sheets>
        <e-spreadsheet-sheet>
            <e-spreadsheet-ranges>
                <e-spreadsheet-range dataSource="ViewBag.DefaultData"></e-spreadsheet-range>
            </e-spreadsheet-ranges>
            <e-spreadsheet-columns>
                <e-spreadsheet-column width="130"></e-spreadsheet-column>
                <e-spreadsheet-column width="92"></e-spreadsheet-column>
                <e-spreadsheet-column width="96"></e-spreadsheet-column>
            </e-spreadsheet-columns>
        </e-spreadsheet-sheet>
    </e-spreadsheet-sheets>
</ejs-spreadsheet>

<script>
    function createHandler() {
        //Applies format to specified range
        this.cellFormat({ fontWeight: 'bold' }, 'A1:D1');
        var colCount = this.getActiveSheet().colCount;
        this.selectRange(ej.spreadsheet.getRangeAddress([4, 0, 4, colCount]));
    }
</script>
public IActionResult Index()
        {
           List<object> data = new List<object>()
            {
                new { Expense Type= "Housing", Projected Cost= "7000", Actual Cost= "7500", Difference= "-500" },
                new { Expense Type= "Transportation", Projected Cost= "500", Actual Cost= "500", Difference= "0" },
                new { Expense Type= "Insurance", Projected Cost= "1000", Actual Cost= "1000", Difference= "0" },
                new { Expense Type= "Food", Projected Cost= "2000", Actual Cost= "1800", Difference= "200" },
                new { Expense Type= "Pets", Projected Cost= "300", Actual Cost= "200", Difference= "100" },
                new { Expense Type= "Personel Care", Projected Cost= "500", Actual Cost= "500", Difference= "0" },
                new { Expense Type= "Loan", Projected Cost= "1000", Actual Cost= "1000", Difference= "0" },
                new { Expense Type= "Tax", Projected Cost= "200", Actual Cost= "200", Difference= "0" },
                new { Expense Type= "Savings", Projected Cost= "1000", Actual Cost= "900", Difference= "100" },
                new { Expense Type= "Total", Projected Cost= "13500", Actual Cost= "13600", Difference= "-100" },
            };
            ViewBag.DefaultData = data;
            return View();
        }

Column selection

Column selection is used to select a single or multiple columns.

User Interface:

You can perform column selection in any of the following ways,

  • By clicking the column header.
  • To select multiple columns, select a column header with the left mouse button and drag over to other column headers (or) use the Shift + arrow keys to select the multiple columns.
  • To select non-adjacent columns, hold Ctrl and select the column header.
  • You can also use the selectRange method for row selection.

The following sample shows the column selection in the spreadsheet, here selecting the 3rd column using the selectRange method.

<ejs-spreadsheet id="spreadsheet" created="createHandler">
    <e-spreadsheet-sheets>
        <e-spreadsheet-sheet>
            <e-spreadsheet-ranges>
                <e-spreadsheet-range dataSource="ViewBag.DefaultData"></e-spreadsheet-range>
            </e-spreadsheet-ranges>
            <e-spreadsheet-columns>
                <e-spreadsheet-column width="130"></e-spreadsheet-column>
                <e-spreadsheet-column width="92"></e-spreadsheet-column>
                <e-spreadsheet-column width="96"></e-spreadsheet-column>
            </e-spreadsheet-columns>
        </e-spreadsheet-sheet>
    </e-spreadsheet-sheets>
</ejs-spreadsheet>

<script>
    function createHandler() {
        //Applies format to specified range
        this.cellFormat({ fontWeight: 'bold' }, 'A1:D1');
        var rowCount = this.getActiveSheet().rowCount;
        this.selectRange(ej.spreadsheet.getRangeAddress([0, 2, rowCount, 2]));
    }
</script>
public IActionResult Index()
        {
           List<object> data = new List<object>()
            {
                new { Expense Type= "Housing", Projected Cost= "7000", Actual Cost= "7500", Difference= "-500" },
                new { Expense Type= "Transportation", Projected Cost= "500", Actual Cost= "500", Difference= "0" },
                new { Expense Type= "Insurance", Projected Cost= "1000", Actual Cost= "1000", Difference= "0" },
                new { Expense Type= "Food", Projected Cost= "2000", Actual Cost= "1800", Difference= "200" },
                new { Expense Type= "Pets", Projected Cost= "300", Actual Cost= "200", Difference= "100" },
                new { Expense Type= "Personel Care", Projected Cost= "500", Actual Cost= "500", Difference= "0" },
                new { Expense Type= "Loan", Projected Cost= "1000", Actual Cost= "1000", Difference= "0" },
                new { Expense Type= "Tax", Projected Cost= "200", Actual Cost= "200", Difference= "0" },
                new { Expense Type= "Savings", Projected Cost= "1000", Actual Cost= "900", Difference= "100" },
                new { Expense Type= "Total", Projected Cost= "13500", Actual Cost= "13600", Difference= "-100" },
            };
            ViewBag.DefaultData = data;
            return View();
        }

How to remove selection in the spreadsheet

The following sample shows, how to remove the selection in the spreadsheet. Here changing the mode as None in selectionSettings to disable’s the UI selection.

<ejs-spreadsheet id="spreadsheet" created="createHandler" cellEdit="cellEdit">
    <e-spreadsheet-selectionsettings mode="None"></e-spreadsheet-selectionsettings>
    <e-spreadsheet-sheets>
        <e-spreadsheet-sheet>
            <e-spreadsheet-ranges>
                <e-spreadsheet-range dataSource="ViewBag.DefaultData"></e-spreadsheet-range>
            </e-spreadsheet-ranges>
            <e-spreadsheet-columns>
                <e-spreadsheet-column width="130"></e-spreadsheet-column>
                <e-spreadsheet-column width="92"></e-spreadsheet-column>
                <e-spreadsheet-column width="96"></e-spreadsheet-column>
            </e-spreadsheet-columns>
        </e-spreadsheet-sheet>
    </e-spreadsheet-sheets>
</ejs-spreadsheet>

<script>
    function createHandler() {
        //Applies format to specified range
        this.cellFormat({ fontWeight: 'bold' }, 'A1:D1');
    }
    function cellEdit (args){
        args.cancel = true;
    }
</script>
public IActionResult Index()
        {
           List<object> data = new List<object>()
            {
                new { Expense Type= "Housing", Projected Cost= "7000", Actual Cost= "7500", Difference= "-500" },
                new { Expense Type= "Transportation", Projected Cost= "500", Actual Cost= "500", Difference= "0" },
                new { Expense Type= "Insurance", Projected Cost= "1000", Actual Cost= "1000", Difference= "0" },
                new { Expense Type= "Food", Projected Cost= "2000", Actual Cost= "1800", Difference= "200" },
                new { Expense Type= "Pets", Projected Cost= "300", Actual Cost= "200", Difference= "100" },
                new { Expense Type= "Personel Care", Projected Cost= "500", Actual Cost= "500", Difference= "0" },
                new { Expense Type= "Loan", Projected Cost= "1000", Actual Cost= "1000", Difference= "0" },
                new { Expense Type= "Tax", Projected Cost= "200", Actual Cost= "200", Difference= "0" },
                new { Expense Type= "Savings", Projected Cost= "1000", Actual Cost= "900", Difference= "100" },
                new { Expense Type= "Total", Projected Cost= "13500", Actual Cost= "13600", Difference= "-100" },
            };
            ViewBag.DefaultData = data;
            return View();
        }

Limitations

  • We have a limitation while performing the Select All(ctrl + A). You can do this only by clicking the Select All button at the top left corner.