行、列の非表示

行や列のVisibleプロパティをfalseに設定することで任意の行や列を非表示にすることができます。

このサンプルでは、「「乳製品」行を非表示」ボタンを押下することで選択した製品分類が「乳製品」の行が非表示になります。 「全行表示」ボタンを押下すると、全行を再表示します。
また、「計画」列を非表示ボタンを押下することでデータ上の計画列が非表示になります。 全列表示ボタンを押下すると、全列を再表示します。

 製品ID製品分類製品名4月計画4月実績5月計画5月実績6月計画6月実績7月計画7月実績8月計画8月実績9月計画9月実績10月計画10月実績11月計画11月実績12月計画12月実績1月計画1月実績2月計画2月実績3月計画3月実績
110001乳製品酪農ミルク5,5005,6005,0005,1004,5004,6006,0006,1005,5005,6005,0005,1004,5004,6006,0006,1005,5005,6005,0005,1004,5004,6006,0006,100
220001清涼飲料水いよかんドリンク1,0001,1003,0003,1002,7002,8002,7002,8001,0001,1003,0003,1002,7002,8002,7002,8001,0001,1003,0003,1002,7002,8002,7002,800
320002清涼飲料水ぶどうジュース3,0003,1003,5003,6004,8004,9004,8004,9003,0003,1003,5003,6004,8004,9004,8004,9003,0003,1003,5003,6004,8004,9004,8004,900
420003清涼飲料水マンゴードリンク2,0002,1001,0001,1005006001,0501,1502,0002,1001,0001,1005006001,0501,1502,0002,1001,0001,1005006001,0501,150
530001ビール激辛ビール5,5005,6008,0008,1008,5008,60010,00010,1005,5005,6008,0008,1008,5008,60010,00010,1005,5005,6008,0008,1008,5008,60010,00010,100
630002ビールモルトビール3,0003,1003,5003,6002,7802,8804,0004,1003,0003,1003,5003,6002,7802,8804,0004,1003,0003,1003,5003,6002,7802,8804,0004,100
720004清涼飲料水ぶどうの街500600300400200300700800500600300400200300700800500600300400200300700800
830003ビールオリエントの村8,0008,1009,5009,6009,5809,6809,0009,1008,0008,1009,5009,6009,5809,6809,0009,1008,0008,1009,5009,6009,5809,6809,0009,100
940002焼酎吟醸 ほめごろし6,0006,1007,0007,1009,0009,1009,5009,6006,0006,1007,0007,1009,0009,1009,5009,6006,0006,1007,0007,1009,0009,1009,5009,600
1040003焼酎大吟醸 オリエント1,0001,1005,0005,1006,0006,1005,0005,1001,0001,1005,0005,1006,0006,1005,0005,1001,0001,1005,0005,1006,0006,1005,0005,100
1140005焼酎麦焼酎 ちこちこ1,0001,1001,5001,6001,2001,3001,2581,3581,0001,1001,5001,6001,2001,3001,2581,3581,0001,1001,5001,6001,2001,3001,2581,358
1210002乳製品酪農ミルク(低脂肪)501601202302380480456556501601202302380480456556501601202302380480456556

ソースコード

別ウィンドウで表示
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;

public partial class rowcolcell_rowcolvisible : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack) return;

        // SPREADの設定
        InitSpread(FpSpread1);

        // シート設定
        InitSheet(FpSpread1.Sheets[0]);
    }

    private void InitSpread(FarPoint.Web.Spread.FpSpread spread)
    {
        // データ連結
        DataSet ds = new DataSet();
        ds.ReadXml(MapPath("../App_Data/databind.xml"));
        spread.ActiveSheetView.DataSource = ds;

        spread.CommandBar.Visible = false;
        spread.CssClass = "spreadStyle";
        spread.UseClipboard = false;
    }

    private void InitSheet(FarPoint.Web.Spread.SheetView sheet)
    {
        // フォントサイズの設定
        sheet.DefaultStyle.Font.Size = FontUnit.Parse("80%");
        sheet.ColumnHeader.DefaultStyle.Font.Size = FontUnit.Parse("80%");
        sheet.RowHeader.DefaultStyle.Font.Size = FontUnit.Parse("80%");
        sheet.SheetCorner.DefaultStyle.Font.Size = FontUnit.Parse("80%");

        // シート設定
        sheet.PageSize = sheet.RowCount;

        // 列幅の設定
        sheet.Columns[0].Width = 45;
        sheet.Columns[1].Width = 85;
        sheet.Columns[2].Width = 140;

        for (int i = 3; i < sheet.ColumnCount; i++)
        {
            sheet.Columns[i].Width = 65;
        }

        //「計画」列の背景色を設定
        for (int i = 0; i < 12; i++)
        {
            sheet.Columns[i * 2 + 3].BackColor = System.Drawing.Color.LavenderBlush;
        }

        //「乳製品」行の背景色を設定
        for (int i = 0; i < sheet.RowCount; i++)
        {
            if (sheet.Cells[i, 1].Text == "乳製品")
            {
                sheet.Rows[i].BackColor = System.Drawing.Color.LavenderBlush;
            }
        }

        // 縦方向の揃え位置を中央に設定
        sheet.DefaultStyle.VerticalAlign = VerticalAlign.Middle;

        // 各列のセル型を設定
        FarPoint.Web.Spread.GeneralCellType gnr = new FarPoint.Web.Spread.GeneralCellType();
        gnr.FormatString = "#,##0";
        sheet.Columns[3, sheet.ColumnCount - 1].CellType = gnr;
        sheet.Columns[3, sheet.ColumnCount - 1].HorizontalAlign = HorizontalAlign.Right;
    }

    protected void FpSpread1_ButtonCommand(object sender, FarPoint.Web.Spread.SpreadCommandEventArgs e)
    {
        if (e.CommandName == "showRows")
        {
            // 全行表示
            for (int i = 0; i < FpSpread1.Sheets[0].RowCount; i++)
            {
                FpSpread1.Sheets[0].Rows[i].Visible = true;
            }
        }
        else if (e.CommandName == "hideRows")
        {
            //「乳製品」行を非表示
            for (int i = 0; i < FpSpread1.Sheets[0].RowCount; i++)
            {
                if (FpSpread1.ActiveSheetView.Cells[i, 1].Text == "乳製品")
                {
                    FpSpread1.ActiveSheetView.Rows[i].Visible = false;
                }
            }
        }
        else if (e.CommandName == "showCols")
        {
            // 全列表示
            for (int i = 0; i < FpSpread1.Sheets[0].ColumnCount; i++)
            {
                FpSpread1.Sheets[0].Columns[i].Visible = true;
            }
        }
        else if (e.CommandName == "hideCols")
        {
            //「計画」列を非表示
            for (int i = 0; i < 12; i++)
            {
                FpSpread1.Sheets[0].Columns[i * 2 + 3].Visible = false;
            }
        }        
    }
}
Imports System
Imports System.Data
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.Configuration

Partial Public Class rowcolcell_rowcolvisible
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack Then
            Return
        End If

        ' SPREADの設定
        InitSpread(FpSpread1)

        ' シート設定
        InitSheet(FpSpread1.Sheets(0))
    End Sub

    Private Sub InitSpread(spread As FarPoint.Web.Spread.FpSpread)
        ' データ連結
        Dim ds As New DataSet()
        ds.ReadXml(MapPath("../App_Data/databind.xml"))
        spread.ActiveSheetView.DataSource = ds

        spread.CommandBar.Visible = False
        spread.CssClass = "spreadStyle"
        spread.UseClipboard = False
    End Sub

    Private Sub InitSheet(sheet As FarPoint.Web.Spread.SheetView)
        ' フォントサイズの設定
        sheet.DefaultStyle.Font.Size = FontUnit.Parse("80%")
        sheet.ColumnHeader.DefaultStyle.Font.Size = FontUnit.Parse("80%")
        sheet.RowHeader.DefaultStyle.Font.Size = FontUnit.Parse("80%")
        sheet.SheetCorner.DefaultStyle.Font.Size = FontUnit.Parse("80%")

        ' シート設定
        sheet.PageSize = sheet.RowCount

        ' 列幅の設定
        sheet.Columns(0).Width = 45
        sheet.Columns(1).Width = 85
        sheet.Columns(2).Width = 140

        For i As Integer = 3 To sheet.ColumnCount - 1
            sheet.Columns(i).Width = 65
        Next

        '「計画」列の背景色を設定
        For i As Integer = 0 To 11
            sheet.Columns(i * 2 + 3).BackColor = System.Drawing.Color.LavenderBlush
        Next

        '「乳製品」行の背景色を設定
        For i As Integer = 0 To sheet.RowCount - 1
            If sheet.Cells(i, 1).Text = "乳製品" Then
                sheet.Rows(i).BackColor = System.Drawing.Color.LavenderBlush
            End If
        Next

        ' 縦方向の揃え位置を中央に設定
        sheet.DefaultStyle.VerticalAlign = VerticalAlign.Middle

        ' 各列のセル型を設定
        Dim gnr As New FarPoint.Web.Spread.GeneralCellType()
        gnr.FormatString = "#,##0"
        sheet.Columns(3, sheet.ColumnCount - 1).CellType = gnr
        sheet.Columns(3, sheet.ColumnCount - 1).HorizontalAlign = HorizontalAlign.Right
    End Sub

    Protected Sub FpSpread1_ButtonCommand(ByVal sender As Object, ByVal e As FarPoint.Web.Spread.SpreadCommandEventArgs) Handles FpSpread1.ButtonCommand
        If e.CommandName = "showRows" Then
            ' 全行表示
            For i As Integer = 0 To FpSpread1.Sheets(0).RowCount - 1
                FpSpread1.Sheets(0).Rows(i).Visible = True
            Next
        ElseIf e.CommandName = "hideRows" Then
            '「乳製品」行を非表示
            For i As Integer = 0 To FpSpread1.Sheets(0).RowCount - 1
                If FpSpread1.ActiveSheetView.Cells(i, 1).Text = "乳製品" Then
                    FpSpread1.ActiveSheetView.Rows(i).Visible = False
                End If
            Next
        ElseIf e.CommandName = "showCols" Then
            ' 全列表示
            For i As Integer = 0 To FpSpread1.Sheets(0).ColumnCount - 1
                FpSpread1.Sheets(0).Columns(i).Visible = True
            Next
        ElseIf e.CommandName = "hideCols" Then
            '「計画」列を非表示
            For i As Integer = 0 To 11
                FpSpread1.Sheets(0).Columns(i * 2 + 3).Visible = False
            Next
        End If
    End Sub
End Class
<%@ Page MasterPageFile="~/MasterPage.master" Language="c#" AutoEventWireup="true" 
         Inherits="rowcolcell_rowcolvisible" CodeFile="rowcolvisible.aspx.cs" %>

<%@ Register Assembly="FarPoint.Web.SpreadJ" Namespace="FarPoint.Web.Spread" TagPrefix="FarPoint" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeaderPlaceHolder1" Runat="Server">
<script type="text/javascript">
    function showRows() {
        var spread = document.getElementById("<%= FpSpread1.ClientID %>");
        spread.CallBack("showRows");
    }
    function hideRows() {
        var spread = document.getElementById("<%= FpSpread1.ClientID %>");
        spread.CallBack("hideRows");
    }
    function showCols() {
        var spread = document.getElementById("<%= FpSpread1.ClientID %>");
        spread.CallBack("showCols");
    }
    function hideCols() {
        var spread = document.getElementById("<%= FpSpread1.ClientID %>");
        spread.CallBack("hideCols");
    }    
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <input id="Button1" type="button" value="全行表示" onclick="showRows();" style="width: 160px;" />
    <input id="Button2" type="button" value="「乳製品」行を非表示" onclick="hideRows();" style="width: 160px;" />
    <br />
    <input id="Button3" type="button" value="全列表示" onclick="showCols();" style="width: 160px;" />
    <input id="Button4" type="button" value="「計画」列を非表示" onclick="hideCols();" style="width: 160px;" />
    <farpoint:FpSpread ID="FpSpread1" runat="server" BorderColor="#A0A0A0" BorderStyle="Solid"
        BorderWidth="1px" onbuttoncommand="FpSpread1_ButtonCommand">
        <CommandBar BackColor="#F6F6F6" ButtonFaceColor="Control" ButtonHighlightColor="ControlLightLight"
            ButtonShadowColor="ControlDark" />
        <Sheets>
            <farpoint:SheetView SheetName="Sheet1">
            </farpoint:SheetView>
        </Sheets>
    </farpoint:FpSpread>
</asp:Content>