Search results

Rows and Columns in ASP.NET MVC Spreadsheet control

Spreadsheet is a tabular format consisting of rows and columns. The intersection point of rows and columns are called as cells. The list of operations that you can perform in rows and columns are,

  • Insert
  • Delete
  • Show and Hide

Insert

You can insert rows or columns anywhere in a spreadsheet. Use the allowInsert property to enable or disable the insert option in Spreadsheet.

Row

The rows can be inserted in the following ways,

  • Using insertRow method, you can insert the rows once the component is loaded.
  • Using context menu, insert the empty rows in the desired position.

The following code example shows the options for inserting rows in the spreadsheet.

razor
insertRowController.cs
@Html.EJS().Spreadsheet("spreadsheet").ShowSheetTabs(false).Created("created").ShowRibbon(false).ShowFormulaBar(false).Sheets(sheet =>
{
    sheet.Range(ranges =>
    {
        ranges.DataSource((IEnumerable<object>)ViewBag.DefaultData).StartCell("B1").Add();
  
    }).Columns(column =>
    {
        column.Width(20).Add();
        column.Width(90).Add();
        column.Width(220).Add();
        column.Width(90).Add();
        column.Width(140).Add();
        column.Width(90).Add();
        column.Width(100).Add();
        column.Width(100).Add();
    }).Add();
}).Render()

<script>

    function created() {
         // Rows model that is going to insert dynamically
            var rowsModel = [{
                index: 9, // Need to specify the index for the first row collection, the specified rows will be inserted in this index.
                cells: [{ value: '' }, { value: '8' }, { value: 'Northwoods Cranberry Sauce' }, { value: '3' }, { value: '12 - 12 oz jars' },
                { value: '40.00' }, { value: '6' }, { value: 'false' }]
            },
            {
                cells: [{ value: '' }, { value: '9' }, { value: 'Mishi Kobe Niku' }, { value: '4' }, { value: '18 - 500 g pkgs.' }, { value: '97.00' }, { value: '29' }, { value: 'true' }]
            }];
        // Applies style formatting before inserting the rows
        this.cellFormat({ fontWeight: 'bold', textAlign: 'center' }, 'B1:H1');
        // inserting a empty row at 0th index
        this.insertRow();
        // inserting 2 rows at the 9th index with data
        this.insertRow(rowsModel);
        // Applies style formatting after the rows are inserted
        this.cellFormat({ textAlign: 'center' }, 'B3:B12');
        this.cellFormat({ textAlign: 'center' }, 'D3:D12');
        this.cellFormat({ textAlign: 'center' }, 'F3:H12');
    }

</script>
public IActionResult Index()
        {
          List<object> data = new List<object>()
            {
                new { ProductId= "1",  ProductName= "Chai",  SupplierId= "1",  QuantityPerUnit= "10 boxes x 20 bags",  UnitPrice= "18.00",  UnitsInStock= "39",  Discontinued= "false" },
                new { ProductId= "2",  ProductName= "Chang",  SupplierId= "1",  QuantityPerUnit= "24 - 12 oz bottles",  UnitPrice= "19.00",  UnitsInStock= "17",  Discontinued= "true" },
                new { ProductId= "3",  ProductName= "Aniseed Syrup",  SupplierId= "1",  QuantityPerUnit= "12 - 550 ml bottles",  UnitPrice= "10.00",  UnitsInStock= "13",  Discontinued= "false" },
                new { ProductId= "4",  ProductName= "Chef Anton\'s Cajun Seasoning",  SupplierId= "2",  QuantityPerUnit= "48 - 6 oz jars",  UnitPrice= "22.00",  UnitsInStock= "53",  Discontinued= "true" },
                new { ProductId= "5",  ProductName= "Chef Anton\'s Gumbo Mix",  SupplierId= "2",  QuantityPerUnit= "36 boxes', 'Unit Price",  UnitPrice= "21.35",  UnitsInStock= "0",  Discontinued= "true" },
                new { ProductId= "6",  ProductName= "Grandma\'s Boysenberry Spread",  SupplierId= "3",  QuantityPerUnit= "12 - 8 oz jars",  UnitPrice= "25.00",  UnitsInStock= "120",  Discontinued= "false" },
                new { ProductId= "7",  ProductName= "Uncle Bob\'s Organic Dried Pears",  SupplierId= "3",  QuantityPerUnit= "12 - 1 lb pkgs.",  UnitPrice= "30.00",  UnitsInStock= "15",  Discontinued= "true" },
                new { ProductId= "10",  ProductName= "Queso Cabrales",  SupplierId= "5",  QuantityPerUnit= "1 kg pkg.",  UnitPrice= "21.00",  UnitsInStock= "22",  Discontinued= "false" },
            };
            ViewBag.DefaultData = data;
            return View();
        }

Column

The columns can be inserted in the following ways,

  • Using insertColumn method, you can insert the columns once the component is loaded.
  • Using context menu, insert the empty columns in the desired position.

The following code example shows the options for inserting columns in the spreadsheet.

razor
insertColumnController.cs
@Html.EJS().Spreadsheet("spreadsheet").ShowSheetTabs(false).Created("created").ShowRibbon(false).ShowFormulaBar(false).Sheets(sheet =>
{
    sheet.Range(ranges =>
    {
        ranges.DataSource((IEnumerable<object>)ViewBag.DefaultData).StartCell("A2").Add();
  
    }).Columns(column =>
    {
        column.Width(90).Add();
        column.Width(220).Add();
        column.Width(90).Add();
        column.Width(140).Add();
        column.Width(90).Add();
        column.Width(100).Add();
        column.Width(100).Add();
    }).Add();
}).Render()

<script>

    function created() {
        // Cells model that you are going to update in the inserted 5th column dynamically
        var cellsModel = [{ value: 'UnitPrice', style: { fontWeight: 'bold', textAlign: 'center' } }, { value: '18.00' },
        { value: '19.00' }, { value: '10.00' }, { value: '22.00' }, { value: '21.35' }, { value: '25.00' }, { value: '30.00' },
        { value: '21.00' }, { value: '40.00' }, { value: '97.00' }];
        // Applies style formatting before inserting the column
        this.cellFormat({ fontWeight: 'bold', textAlign: 'center' }, 'A2:G2');
        // inserting a empty column at 0th index
        this.insertColumn();
        // inserting 1 column at the 5th index with column model
        this.insertColumn([{ index: 5, width: 90 }]);
        var rowIndex = 1;
        // Updating the 5th column data
        cellsModel.forEach((cell) => {
            this.updateCell(cell, ej.spreadsheet.getCellAddress(rowIndex, 5)); rowIndex++;
        });
        // Applies style formatting after the columns are inserted
        this.cellFormat({ textAlign: 'center' }, 'B3:B12');
        this.cellFormat({ textAlign: 'center' }, 'D3:D12');
        this.cellFormat({ textAlign: 'center' }, 'F3:H12');
    }

</script>
public IActionResult Index()
        {
              List<object> data = new List<object>()
            {
                new { ProductId= "1",  ProductName= "Chai",  SupplierId= "1",  QuantityPerUnit= "10 boxes x 20 bags",  UnitsInStock= "39",  Discontinued= "false" },
                new { ProductId= "2",  ProductName= "Chang",  SupplierId= "1",  QuantityPerUnit= "24 - 12 oz bottles",  UnitsInStock= "17",  Discontinued= "true" },
                new { ProductId= "3",  ProductName= "Aniseed Syrup",  SupplierId= "1",  QuantityPerUnit= "12 - 550 ml bottles",  UnitsInStock= "13",  Discontinued= "false" },
                new { ProductId= "4",  ProductName= "Chef Anton\'s Cajun Seasoning",  SupplierId= "2",  QuantityPerUnit= "48 - 6 oz jars",  UnitsInStock= "53",  Discontinued= "true" },
                new { ProductId= "5",  ProductName= "Chef Anton\'s Gumbo Mix",  SupplierId= "2",  QuantityPerUnit= "36 boxes', 'Unit Price", UnitsInStock= "0",  Discontinued= "true" },
                new { ProductId= "6",  ProductName= "Grandma\'s Boysenberry Spread",  SupplierId= "3",  QuantityPerUnit= "12 - 8 oz jars",  UnitsInStock= "120",  Discontinued= "false" },
                new { ProductId= "7",  ProductName= "Uncle Bob\'s Organic Dried Pears",  SupplierId= "3",  QuantityPerUnit= "12 - 1 lb pkgs.", UnitsInStock= "15",  Discontinued= "true" },
                new { ProductId= "8",  ProductName= "Queso Cabrales",  SupplierId= "5",  QuantityPerUnit= "1 kg pkg.",  UnitsInStock= "22",  Discontinued= "false" },
                new { ProductId= "9",  ProductName= "Northwoods Cranberry Sauce",  SupplierId= "3",  QuantityPerUnit= "12 - 12 oz jars",  UnitsInStock= "6",  Discontinued= "false" },
                new { ProductId= "10",  ProductName= "Mishi Kobe Niku",  SupplierId= "4",  QuantityPerUnit= "18 - 500 g pkgs.",   UnitsInStock= "29", Discontinued= "false" },
            };
            ViewBag.DefaultData = data;
            return View();
        }

Delete

Delete support provides an option for deleting the rows and columns in the spreadsheet. Use allowDelete property to enable or disable the delete option in Spreadsheet.

The rows and columns can be deleted dynamically in the following ways,

  • Using delete method, you can delete the loaded rows and columns.
  • Using context menu, you can delete the selected rows and columns.

The following code example shows the delete operation of rows and columns in the spreadsheet.

razor
deleteRowController.cs
@Html.EJS().Spreadsheet("spreadsheet").ShowSheetTabs(false).Created("created").ShowRibbon(false).ShowFormulaBar(false).Sheets(sheet =>
{
    sheet.Range(ranges =>
    {
        ranges.DataSource((IEnumerable<object>)ViewBag.DefaultData).Add();
  
    }).Columns(column =>
    {
        column.Width(90).Add();
        column.Width(220).Add();
        column.Width(90).Add();
        column.Width(140).Add();
        column.Width(90).Add();
        column.Width(100).Add();
        column.Width(100).Add();
    }).Add();
}).Render()

<script>

    function created() {
       this.cellFormat({ fontWeight: 'bold', textAlign: 'center' }, 'A1:H1');
        // deleting the rows from 8th to 10th index. To delete row, the last argument of enum type is passed as 'Row'
        this.delete(8, 10, 'Row');
        // deleting the 2nd and 5th indexed columns
        this.delete(2, 2, 'Column');
        this.delete(5, 5, 'Column');
        // Applies style formatting after deleted the rows and columns
        this.cellFormat({ textAlign: 'center' }, 'A2:A8');
        this.cellFormat({ textAlign: 'center' }, 'D2:G8');
    }

</script>
public IActionResult Index()
        {
            List<object> data = new List<object>()
            {
                new { ProductId= "1",  ProductName= "Chai",  SupplierId= "1",  QuantityPerUnit= "10 boxes x 20 bags",  UnitPrice= "18.00",  UnitsInStock= "39",  Discontinued= "false" },
                new { ProductId= "2",  ProductName= "Chang",  SupplierId= "1",  QuantityPerUnit= "24 - 12 oz bottles",  UnitPrice= "19.00",  UnitsInStock= "17",  Discontinued= "true" },
                new { ProductId= "3",  ProductName= "Aniseed Syrup",  SupplierId= "1",  QuantityPerUnit= "12 - 550 ml bottles",  UnitPrice= "10.00",  UnitsInStock= "13",  Discontinued= "false" },
                new { ProductId= "4",  ProductName= "Chef Anton\'s Cajun Seasoning",  SupplierId= "2",  QuantityPerUnit= "48 - 6 oz jars",  UnitPrice= "22.00",  UnitsInStock= "53",  Discontinued= "true" },
                new { ProductId= "5",  ProductName= "Chef Anton\'s Gumbo Mix",  SupplierId= "2",  QuantityPerUnit= "36 boxes', 'Unit Price",  UnitPrice= "21.35",  UnitsInStock= "0",  Discontinued= "true" },
                new { ProductId= "6",  ProductName= "Grandma\'s Boysenberry Spread",  SupplierId= "3",  QuantityPerUnit= "12 - 8 oz jars",  UnitPrice= "25.00",  UnitsInStock= "120",  Discontinued= "false" },
                new { ProductId= "7",  ProductName= "Uncle Bob\'s Organic Dried Pears",  SupplierId= "3",  QuantityPerUnit= "12 - 1 lb pkgs.",  UnitPrice= "30.00",  UnitsInStock= "15",  Discontinued= "true" },
                new { ProductId= "8",  ProductName= "Queso Cabrales",  SupplierId= "5",  QuantityPerUnit= "1 kg pkg.",  UnitPrice= "21.00",  UnitsInStock= "22",  Discontinued= "false" },
                new { ProductId= "9",  ProductName= "Northwoods Cranberry Sauce",  SupplierId= "3",  QuantityPerUnit= "12 - 12 oz jars",  UnitPrice= "21.00",  UnitsInStock= "6",  Discontinued= "false" },
                new { ProductId= "10",  ProductName= "Mishi Kobe Niku",  SupplierId= "4",  QuantityPerUnit= "18 - 500 g pkgs.",  UnitPrice= "21.00",  UnitsInStock= "29",  Discontinued= "true" },
            };
            ViewBag.DefaultData = data;
            return View();
        }

Hide and show

You can show or hide the rows and columns in the spreadsheet through property binding, method, and context menu.

Row

The rows can be hidden or shown through the following ways,

  • Using hidden property in row, you can hide/show the rows at initial load.
  • Using hideRow method, you can hide the rows by specifying the start and end row index, set the last argument hide as false to unhide the hidden rows.
  • Right-click on the row header and select the desired option from context menu

Column

The columns can be hidden or shown through following ways,

  • Using hidden property in columns, you can hide/show the columns at initial load.
  • Using hideColumn method, you can hide the columns by specifying the start and end column index, set the last argument hide as false to unhide the hidden columns.
  • Right-click on the column header and select the desired option from context menu

The following code example shows the hide/show rows and columns operation in the spreadsheet.

razor
showHideController.cs
@Html.EJS().Spreadsheet("spreadsheet").ShowSheetTabs(false).Created("created").ShowRibbon(false).ShowFormulaBar(false).Sheets(sheet =>
{
    sheet.Range(ranges =>
    {
        ranges.DataSource((IEnumerable<object>)ViewBag.DefaultData).Add();
  
    }).Rows(row =>
    {
        row.Index(2).Hidden(true).Add();
        row.Hidden(true).Add();
    }).Columns(column =>
    {
        column.Width(90).Add();
        column.Width(220).Add();
        column.Width(90).Add();
        column.Width(140).Add();
        column.Width(90).Add();
        column.Width(100).Add();
        column.Width(100).Add();
    }).Add();
}).Render()

<script>

    function created() {
       this.cellFormat({ fontWeight: 'bold', textAlign: 'center' }, 'A1:H1');
        // Unhide the 2nd index hidden column
        this.hideColumn(1, 1, false);
        // Unhide the 3rd index hidden row
        this.hideRow(3, 3, false);
        // Hiding the 6th index column
        this.hideColumn(6);
        // Hiding the 8th and 9th index row
        this.hideRow(8, 9);
        this.cellFormat({ textAlign: 'center' }, 'D2:H11');
    }

</script>
public IActionResult Index()
        {
           List<object> data = new List<object>()
            {
                new { ProductId= "1",  ProductName= "Chai",  SupplierId= "1",  QuantityPerUnit= "10 boxes x 20 bags",  UnitPrice= "18.00",  UnitsInStock= "39",  Discontinued= "false" },
                new { ProductId= "2",  ProductName= "Chang",  SupplierId= "1",  QuantityPerUnit= "24 - 12 oz bottles",  UnitPrice= "19.00",  UnitsInStock= "17",  Discontinued= "true" },
                new { ProductId= "3",  ProductName= "Aniseed Syrup",  SupplierId= "1",  QuantityPerUnit= "12 - 550 ml bottles",  UnitPrice= "10.00",  UnitsInStock= "13",  Discontinued= "false" },
                new { ProductId= "4",  ProductName= "Chef Anton\'s Cajun Seasoning",  SupplierId= "2",  QuantityPerUnit= "48 - 6 oz jars",  UnitPrice= "22.00",  UnitsInStock= "53",  Discontinued= "true" },
                new { ProductId= "5",  ProductName= "Chef Anton\'s Gumbo Mix",  SupplierId= "2",  QuantityPerUnit= "36 boxes', 'Unit Price",  UnitPrice= "21.35",  UnitsInStock= "0",  Discontinued= "true" },
                new { ProductId= "6",  ProductName= "Grandma\'s Boysenberry Spread",  SupplierId= "3",  QuantityPerUnit= "12 - 8 oz jars",  UnitPrice= "25.00",  UnitsInStock= "120",  Discontinued= "false" },
                new { ProductId= "7",  ProductName= "Uncle Bob\'s Organic Dried Pears",  SupplierId= "3",  QuantityPerUnit= "12 - 1 lb pkgs.",  UnitPrice= "30.00",  UnitsInStock= "15",  Discontinued= "true" },
                new { ProductId= "8",  ProductName= "Queso Cabrales",  SupplierId= "5",  QuantityPerUnit= "1 kg pkg.",  UnitPrice= "21.00",  UnitsInStock= "22",  Discontinued= "false" },
                new { ProductId= "9",  ProductName= "Northwoods Cranberry Sauce",  SupplierId= "3",  QuantityPerUnit= "12 - 12 oz jars",  UnitPrice= "21.00",  UnitsInStock= "6",  Discontinued= "false" },
                new { ProductId= "10",  ProductName= "Mishi Kobe Niku",  SupplierId= "4",  QuantityPerUnit= "18 - 500 g pkgs.",  UnitPrice= "21.00",  UnitsInStock= "29",  Discontinued= "true" },
            };
            ViewBag.DefaultData = data;
            return View();
        }

See Also