条件付きスタイル設定

条件付きスタイル設定

機能

Settings


エクスポート
エクスポート名:

Description

このサンプルでは、書式設定セルを含む MVC FlexGrid を Excel ファイルにエクスポートする方法を示します。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApiExplorer.Models;

namespace WebApiExplorer.Controllers
{
    public partial class MVCFlexGridController : Controller
    {
        private readonly GridExportImportOptions _flexGridConditionalStylingModel = new GridExportImportOptions
        {
            NeedExport = true,
            NeedImport = false,
            IncludeColumnHeaders = true
        };

        public ActionResult ConditionalStyling()
        {
            ViewBag.Options = _flexGridConditionalStylingModel;
            return View(Sale.GetData(100));
        }
    }
}
@using WebApiExplorer.Models
@using C1.Web.Mvc.Grid
@model IEnumerable<Sale>
@{
    ClientSettingsModel demoSettingsModel = ViewBag.DemoSettingsModel;
    GridExportImportOptions optionsModel = ViewBag.Options;
    ViewBag.DemoSettings = true;
}

<script>
    function itemFormatter(panel, r, c, cell) {

        // validate CellType and if correct column
        if (wijmo.grid.CellType.Cell === panel.cellType &&
            'Amount' === panel.columns[c].binding) {

            // get the cell's data
            var cellData = panel.getCellData(r, c);

            // set cell's foreground color
            cell.style.color = getAmountColor(cellData);
        }
    }

    // get the color used to display an amount
    function getAmountColor(amount) {
        return amount < 1500 ? 'red' : amount < 3000 ? 'black' : 'green';
    }
</script>

@(Html.C1().FlexGrid<Sale>().Id(optionsModel.ControlId)
    .AutoGenerateColumns(false)
    .IsReadOnly(true)
    .Bind(Model)
    .CssClass("grid")
    .AllowResizing(AllowResizing.None)
    .ItemFormatter("itemFormatter")
    .Columns(columns =>
    {
        columns.Add(column => column.Binding("ID"));
        columns.Add(column => column.Binding("Country"));
        columns.Add(column => column.Binding("Product"));
        columns.Add(column => column.Binding("Amount").Format("c").Width("*").Align("center"));
    })
)

@section Settings{
    @Html.Partial("_FlexGridOptions", optionsModel)
}

@section Description{
    @Html.Raw(Resources.MVCFlexGrid.ConditionalStyling_Text0)
}