行、列のリサイズ

行や列のResizable プロパティにて、ユーザーが行または列のサイズを変更することができるかを設定します。 Resizable プロパティをFalseに設定することで、ユーザーによる行または列のサイズ変更を禁止することができます。
また、ユーザーにヘッダの行・列のサイズ変更を許可するには、SPREADのAllowHeaderResizeプロパティを設定します。

このサンプルではチェックボックスにより、行、列およびヘッダのリサイズを許可、禁止することができます。
 製品ID製品分類製品名第1Q第2Q
4月5月6月7月8月9月
110001乳製品酪農ミルク5,5005,0004,5006,0005,5005,000
220001清涼飲料水いよかんドリンク1,0003,0002,7002,7001,0003,000
320002清涼飲料水ぶどうジュース3,0003,5004,8004,8003,0003,500
420003清涼飲料水マンゴードリンク2,0001,0005001,0502,0001,000
530001ビール激辛ビール5,5008,0008,50010,0005,5008,000
630002ビールモルトビール3,0003,5002,7804,0003,0003,500
720004清涼飲料水ぶどうの街500300200700500300
830003ビールオリエントの村8,0009,5009,5809,0008,0009,500
940002焼酎吟醸 ほめごろし6,0007,0009,0009,5006,0007,000
1040003焼酎大吟醸 オリエント1,0005,0006,0005,0001,0005,000

ソースコード

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

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

        // セル型の自動設定を無効化
        FpSpread1.ActiveSheetView.DataAutoCellTypes = false;

        // データ連結
        DataSet ds = new DataSet();
        ds.ReadXml(MapPath("../App_Data/datanum3.xml"));
        FpSpread1.DataSource = ds;

        // SPREAD初期化
        InitSpread(FpSpread1.Sheets[0]);

        CheckBox1.Attributes.Add("onclick", "SpCallBack('chgHeaderResize')");
        CheckBox2.Attributes.Add("onclick", "SpCallBack('chgRowResize')");
        CheckBox3.Attributes.Add("onclick", "SpCallBack('chgColResize')");
    }

    private void InitSpread(FarPoint.Web.Spread.SheetView sheet)
    {
        // SPREAD設定
        FpSpread1.CssClass = "spreadStyle";
        FpSpread1.UseClipboard = false;
        FpSpread1.CommandBar.Visible = false;

        // フォントサイズの設定
        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.Columns[0].Width = 45;
        sheet.Columns[1].Width = 85;
        sheet.Columns[2].Width = 140;
        sheet.Columns[3].Width = 40;
        sheet.Columns[4].Width = 65;
        sheet.Columns[5].Width = 65;
        sheet.Columns[6].Width = 65;
        sheet.Columns[7].Width = 65;
        sheet.Columns[8].Width = 65;
        sheet.Columns[9].Width = 65;

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

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

        sheet.Columns[3].Visible = false;

        // 列ヘッダを2行に設定
        sheet.ColumnHeader.RowCount = 2;
        sheet.ColumnHeader.AutoTextIndex = 0;
        // 行ヘッダを2行に設定
        sheet.RowHeader.ColumnCount = 2;
        sheet.RowHeader.AutoTextIndex = 0;

        // ヘッダ再設定
        sheet.ColumnHeaderSpanModel.Add(0, 0, 2, 1);
        sheet.ColumnHeaderSpanModel.Add(0, 1, 2, 1);
        sheet.ColumnHeaderSpanModel.Add(0, 2, 2, 1);
        sheet.ColumnHeaderSpanModel.Add(0, 3, 2, 1);
        
        sheet.ColumnHeader.Cells[1, 4].Text = "4月";
        sheet.ColumnHeader.Cells[1, 5].Text = "5月";
        sheet.ColumnHeader.Cells[1, 6].Text = "6月";
        sheet.ColumnHeader.Cells[1, 7].Text = "7月";
        sheet.ColumnHeader.Cells[1, 8].Text = "8月";
        sheet.ColumnHeader.Cells[1, 9].Text = "9月";

        sheet.ColumnHeader.Cells[0, 4].Text = "第1Q";
        sheet.ColumnHeaderSpanModel.Add(0, 4, 1, 3);

        sheet.ColumnHeader.Cells[0, 7].Text = "第2Q";
        sheet.ColumnHeaderSpanModel.Add(0, 7, 1, 3);

        sheet.RowHeader.Cells[0, 1].Text = "";
        sheet.RowHeader.Cells[1, 1].Text = "";
        sheet.RowHeader.Cells[2, 1].Text = "";
        sheet.RowHeader.Cells[3, 1].Text = "";
        sheet.RowHeader.Cells[4, 1].Text = "";
        sheet.RowHeader.Cells[5, 1].Text = "";
        sheet.RowHeader.Cells[6, 1].Text = "";
        sheet.RowHeader.Cells[7, 1].Text = "";
        sheet.RowHeader.Cells[8, 1].Text = "";
        sheet.RowHeader.Cells[9, 1].Text = "";
        sheet.RowHeader.Cells[10,1].Text = "";
        sheet.RowHeader.Cells[11,1].Text = "";
        sheet.RowHeader.Cells[12,1].Text = "";
        sheet.RowHeader.Cells[13,1].Text = "";
        sheet.RowHeader.Cells[14,1].Text = "";
        sheet.RowHeader.Cells[15,1].Text = "";
        sheet.RowHeader.Cells[16,1].Text = "";

        // ヘッダのリサイズを許可しない
        FpSpread1.AllowHeaderResize = false;
        
        // 行・列のリサイズを許可しない
        for (int i = 0; i < FpSpread1.Sheets[0].RowCount; i++)
        {
            FpSpread1.Sheets[0].Rows[i].Resizable = false;
        }
        for (int i = 0; i < FpSpread1.Sheets[0].ColumnCount; i++)
        {
            FpSpread1.Sheets[0].Columns[i].Resizable = false;
        }
    }

    protected void FpSpread1_ButtonCommand(object sender, FarPoint.Web.Spread.SpreadCommandEventArgs e)
    {
        if (e.CommandName == "chgHeaderResize")
        {
            FpSpread1.AllowHeaderResize = !FpSpread1.AllowHeaderResize;
        }
        else if (e.CommandName == "chgRowResize")
        {
            for (int i = 0; i < FpSpread1.Sheets[0].RowCount; i++)
            {
                FpSpread1.Sheets[0].Rows[i].Resizable = !FpSpread1.Sheets[0].Rows[i].Resizable;
            }
        }
        else if (e.CommandName == "chgColResize")
        {
            for (int i = 0; i < FpSpread1.Sheets[0].ColumnCount; i++)
            {
                FpSpread1.Sheets[0].Columns[i].Resizable = !FpSpread1.Sheets[0].Columns[i].Resizable;
            }
        }
    }
}

Partial Public Class rowcolcell_rowcolresize
    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

        ' セル型の自動設定を無効化
        FpSpread1.ActiveSheetView.DataAutoCellTypes = False

        ' データ連結
        Dim ds As New System.Data.DataSet()
        ds.ReadXml(MapPath("../App_Data/datanum3.xml"))
        FpSpread1.DataSource = ds

        ' SPREAD初期化
        InitSpread(FpSpread1.Sheets(0))

        CheckBox1.Attributes.Add("onclick", "SpCallBack('chgHeaderResize')")
        CheckBox2.Attributes.Add("onclick", "SpCallBack('chgRowResize')")
        CheckBox3.Attributes.Add("onclick", "SpCallBack('chgColResize')")
    End Sub

    Private Sub InitSpread(ByVal sheet As FarPoint.Web.Spread.SheetView)
        ' SPREAD設定
        FpSpread1.CssClass = "spreadStyle"
        FpSpread1.UseClipboard = False
        FpSpread1.CommandBar.Visible = False

        ' フォントサイズの設定
        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.Columns(0).Width = 45
        sheet.Columns(1).Width = 85
        sheet.Columns(2).Width = 140
        sheet.Columns(3).Width = 40
        sheet.Columns(4).Width = 65
        sheet.Columns(5).Width = 65
        sheet.Columns(6).Width = 65
        sheet.Columns(7).Width = 65
        sheet.Columns(8).Width = 65
        sheet.Columns(9).Width = 65

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

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

        sheet.Columns(3).Visible = False

        ' 列ヘッダを2行に設定
        sheet.ColumnHeader.RowCount = 2
        sheet.ColumnHeader.AutoTextIndex = 0
        ' 行ヘッダを2行に設定
        sheet.RowHeader.ColumnCount = 2
        sheet.RowHeader.AutoTextIndex = 0

        ' ヘッダ再設定
        sheet.ColumnHeaderSpanModel.Add(0, 0, 2, 1)
        sheet.ColumnHeaderSpanModel.Add(0, 1, 2, 1)
        sheet.ColumnHeaderSpanModel.Add(0, 2, 2, 1)
        sheet.ColumnHeaderSpanModel.Add(0, 3, 2, 1)

        sheet.ColumnHeader.Cells(1, 4).Text = "4月"
        sheet.ColumnHeader.Cells(1, 5).Text = "5月"
        sheet.ColumnHeader.Cells(1, 6).Text = "6月"
        sheet.ColumnHeader.Cells(1, 7).Text = "7月"
        sheet.ColumnHeader.Cells(1, 8).Text = "8月"
        sheet.ColumnHeader.Cells(1, 9).Text = "9月"

        sheet.ColumnHeader.Cells(0, 4).Text = "第1Q"
        sheet.ColumnHeaderSpanModel.Add(0, 4, 1, 3)

        sheet.ColumnHeader.Cells(0, 7).Text = "第2Q"
        sheet.ColumnHeaderSpanModel.Add(0, 7, 1, 3)

        sheet.RowHeader.Cells(0, 1).Text = ""
        sheet.RowHeader.Cells(1, 1).Text = ""
        sheet.RowHeader.Cells(2, 1).Text = ""
        sheet.RowHeader.Cells(3, 1).Text = ""
        sheet.RowHeader.Cells(4, 1).Text = ""
        sheet.RowHeader.Cells(5, 1).Text = ""
        sheet.RowHeader.Cells(6, 1).Text = ""
        sheet.RowHeader.Cells(7, 1).Text = ""
        sheet.RowHeader.Cells(8, 1).Text = ""
        sheet.RowHeader.Cells(9, 1).Text = ""
        sheet.RowHeader.Cells(10, 1).Text = ""
        sheet.RowHeader.Cells(11, 1).Text = ""
        sheet.RowHeader.Cells(12, 1).Text = ""
        sheet.RowHeader.Cells(13, 1).Text = ""
        sheet.RowHeader.Cells(14, 1).Text = ""
        sheet.RowHeader.Cells(15, 1).Text = ""
        sheet.RowHeader.Cells(16, 1).Text = ""

        ' ヘッダのリサイズを許可しない
        FpSpread1.AllowHeaderResize = False

        ' 行・列のリサイズを許可しない
        For i As Integer = 0 To FpSpread1.Sheets(0).RowCount - 1
            FpSpread1.Sheets(0).Rows(i).Resizable = False
        Next
        For i As Integer = 0 To FpSpread1.Sheets(0).ColumnCount - 1
            FpSpread1.Sheets(0).Columns(i).Resizable = False
        Next
    End Sub

    Protected Sub FpSpread1_ButtonCommand(ByVal sender As Object, ByVal e As FarPoint.Web.Spread.SpreadCommandEventArgs) Handles FpSpread1.ButtonCommand
        If e.CommandName = "chgHeaderResize" Then
            FpSpread1.AllowHeaderResize = Not FpSpread1.AllowHeaderResize
        ElseIf e.CommandName = "chgRowResize" Then
            For i As Integer = 0 To FpSpread1.Sheets(0).RowCount - 1
                FpSpread1.Sheets(0).Rows(i).Resizable = Not FpSpread1.Sheets(0).Rows(i).Resizable
            Next
        ElseIf e.CommandName = "chgColResize" Then
            For i As Integer = 0 To FpSpread1.Sheets(0).ColumnCount - 1
                FpSpread1.Sheets(0).Columns(i).Resizable = Not FpSpread1.Sheets(0).Columns(i).Resizable
            Next
        End If
    End Sub
End Class
<%@ Page MasterPageFile="~/MasterPage.master" Language="c#" AutoEventWireup="true" 
         Inherits="rowcolcell_rowcolresize" CodeFile="rowcolresize.aspx.cs" %>

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

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <script type="text/javascript">        
        function SpCallBack(cmdnm) {
            var spread = document.getElementById("<%= FpSpread1.ClientID %>");
            spread.CallBack(cmdnm);
        }   
    </script>
    <asp:CheckBox ID="CheckBox1" runat="server" Text="ヘッダのリサイズを許可" 
        Checked="False" style="position: relative;" />
    <asp:CheckBox ID="CheckBox2" runat="server" Text="行のリサイズを許可" 
        Checked="False" style="position: relative;" />
    <asp:CheckBox ID="CheckBox3" runat="server" Text="列のリサイズを許可" 
        Checked="False" style="position: relative;" />
    <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>