SPREAD for ASP.NETにて結合列ヘッダーを持っている状態でソートインジケータを表示しつつ処理する方法. (OnSortColumnCommand, ImageCellType, uparrow.gif, downarrow.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using FarPoint.Web.Spread;
namespace SpdCustomSortSample
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
spd.UseClipboard = false;
var sheet = spd.ActiveSheetView;
sheet.AllowSort = true;
sheet.ColumnCount = 5;
sheet.RowCount = 3;
sheet.Cells[0, 0].Value = "1";
sheet.Cells[0, 1].Value = "2";
sheet.Cells[0, 2].Value = "3";
sheet.Cells[0, 3].Value = "9";
sheet.Cells[0, 4].Value = "13";
sheet.Cells[1, 0].Value = "2";
sheet.Cells[1, 1].Value = "3";
sheet.Cells[1, 2].Value = "1";
sheet.Cells[1, 3].Value = "7";
sheet.Cells[1, 4].Value = "16";
sheet.Cells[2, 0].Value = "3";
sheet.Cells[2, 1].Value = "1";
sheet.Cells[2, 2].Value = "2";
sheet.Cells[2, 3].Value = "5";
sheet.Cells[2, 4].Value = "11";
// 2列目から3列分のカラムヘッダーを結合
sheet.ColumnHeaderSpanModel.Add(0, 1, 1, 3);
// カスタムソートを行う為、予めカラムヘッダー全てをImageCellTypeにしておく
for (int i = 0; i < sheet.ColumnCount; i++)
{
sheet.ColumnHeader.Columns[i].CellType = new FarPoint.Web.Spread.ImageCellType();
}
}
}
//
// SPREAD カスタムソート処理 イベントハンドラ (OnSortColumnCommand)
// SPREAD for ASP.NETにて、結合列をソートするにはカスタムソート処理を記述する必要がある。
// (結合列をソートすると、そのままではソートインジケータが表示されなくなるため)
//
// さらに、カスタムソート処理を行うと他の列のソートインジケータの調整も行わなくては
// ならなくなるので、実質的に全ての列に対しての処理をこのイベントハンドラに記述する
// 必要がある。前準備として、ソートインジケータを設定するために、カラムヘッダーの
// セルタイプをImageCellTypeに設定しておく必要がある。
//
// 参考URL: http://dotnetdemo.grapecity.com/Demo/Spread/spreadwebbasic/sort/customautosort.aspx?menuidx=10
//
protected void spd_SortColumnCommand(object sender, SpreadCommandEventArgs e)
{
const string ascImageUrl = "/fp_client/fpspread/5_0_3523_2008/img/uparrow.gif";
const string descImageUrl = "/fp_client/fpspread/5_0_3523_2008/img/downarrow.gif";
// 列インデックス取得
int col = Convert.ToInt32(e.CommandArgument);
SheetView sheet = e.SheetView;
ImageCellType imgCellType = sheet.ColumnHeader.Columns[col].CellType as ImageCellType;
bool asc = true;
string imgUrl = ascImageUrl;
if (imgCellType.ImageUrl == ascImageUrl)
{
asc = false;
imgUrl = descImageUrl;
}
else if (imgCellType.ImageUrl == descImageUrl)
{
asc = true;
imgUrl = ascImageUrl;
}
SortInfo info = new SortInfo(col, asc);
if (col == 1)
{
// 3列目でソート
info = new SortInfo(col + 1, asc);
}
// 対象列以外のインジケータをクリア
for (int i = 0; i < sheet.ColumnCount; i++)
{
if (i != col)
{
ImageCellType cellType = e.SheetView.ColumnHeader.Columns[i].CellType as ImageCellType;
cellType.ImageUrl = null;
}
}
// ソート処理実行
sheet.SortRows(0, sheet.RowCount, new SortInfo[] { info });
// ソートインジケータ設定.
imgCellType.ImageUrl = imgUrl;
imgCellType.TextOnRight = false;
e.Handled = true;
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SpdCustomSortSample._Default" %>
<%@ Register assembly="FarPoint.Web.SpreadJ" namespace="FarPoint.Web.Spread" tagprefix="FarPoint" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<FarPoint:FpSpread ID="spd" runat="server" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" Height="200px" Width="570px" OnSortColumnCommand="spd_SortColumnCommand">
<TitleInfo BackColor="#E7EFF7" ForeColor="" HorizontalAlign="Center" VerticalAlign="NotSet" Font-Size="X-Large"></TitleInfo>
<commandbar backcolor="Control" buttonfacecolor="Control" buttonhighlightcolor="ControlLightLight" buttonshadowcolor="ControlDark">
<Background BackgroundImageUrl="SPREADCLIENTPATH:/img/cbbg.gif"></Background>
</commandbar>
<sheets>
<FarPoint:SheetView SheetName="Sheet1">
</FarPoint:SheetView>
</sheets>
</FarPoint:FpSpread>
</div>
</form>
</body>
</html>