MESCIUS SPREAD for ASP.NET 10.0J > 開発者の手引き > データ操作 > フィルタリング > 通常フィルタリング > カスタムフィルタ |
カスタムフィルタを作成して、列に対して定義されたフィルタのコレクションに追加できます。
カスタムフィルタを作成するには、次の手順に従います。
次のサンプルコードは、「1000以上5000以下」の数値データのみ表示するMyCustomFilterを作成し、適用する例です。
protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) return; // フィルターを設定 FarPoint.Web.Spread.SheetView = FpSpread1.Sheets[0]: // 非表示フィルタの作成 FarPoint.Web.Spread.HideRowFilter hf = new FarPoint.Web.Spread.HideRowFilter(sheet); // カスタムフィルタを作成 MyCustomFilter mc = new MyCustomFilter(); mc.SheetView = sheet; // 1列目~3列目にカスタムフィルタを設定 for (int i = 0; i < 2; i++) { FarPoint.Web.Spread.FilterColumnDefinition fcd = new FarPoint.Web.Spread.FilterColumnDefinition(i, FarPoint.Web.Spread.FilterListBehavior.Custom); // カスタムフィルタを定義に追加 fcd.Filters.Add(mc); // フィルタ定義を非表示フィルタに追加 hf.AddColumn(fcd); } //フィルタをシートに設定; sheet.RowFilter = hf; } [Serializable()] public class MyCustomFilter : FarPoint.Web.Spread.DefaultFilterItem { FarPoint.Web.Spread.SheetView sv = null; public MyCustomFilter() { } public override string DisplayName { get { return "1000~5000"; }//フィルタに表示する文字列 } public override FarPoint.Web.Spread.SheetView SheetView { set { sv = value; } } private bool IsNumeric(object ovalue) { System.Text.RegularExpressions.Regex _isNumber = new System.Text.RegularExpressions.Regex(@"^\-?\d+\.?\d*$"); System.Text.RegularExpressions.Match m = _isNumber.Match(Convert.ToString(ovalue)); return m.Success; } public bool IsFilteredIn(object ovalue) { bool ret = false; if (IsNumeric(ovalue)) { if (Double.Parse(Convert.ToString(ovalue)) >= 1000 && Double.Parse(Convert.ToString(ovalue)) <= 5000) ret = true; } return ret; } public override int[] Filter(int columnIndex) { System.Collections.ArrayList ar = new System.Collections.ArrayList(); object val; for (int i = 0; i < sv.RowCount; i++) { val = sv.GetValue(i, columnIndex); if (IsFilteredIn(val)) ar.Add(i); //条件に合致する行番号をリストに追加 } return (Int32[])(ar.ToArray(typeof(Int32))); } public override bool ShowInDropDown(int columnIndex, int[] filteredInRowList) { // filteredInRowList引数は現在、フィルター・イン(フィルターの条件に合致した)行リスト // 条件に合うデータが存在する時のみフィルター要素を表示します。 if (filteredInRowList == null) { for (int i = 0; i < sv.RowCount; i++) { object value = sv.GetValue(i, columnIndex); if (value != null) { if (IsFilteredIn(value)) return true; } } } else { // 現在の行リストが条件に合致するか確認 for (int i = 0; i < filteredInRowList.Length; i++) { int row = filteredInRowList[i]; object value = sv.GetValue(row, columnIndex); if (value != null) { if (IsFilteredIn(value)) return true; } } } return false; } public override bool Serialize(System.Xml.XmlTextWriter w) { w.WriteStartElement("MyCustomFilter"); base.Serialize(w); w.WriteEndElement(); return true; } public override bool Deserialize(System.Xml.XmlNodeReader r) { if (r.NodeType == System.Xml.XmlNodeType.Element) { if (r.Name.Equals("MyCustomFilter")) base.Deserialize(r); } return true; } }
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If IsPostBack Then Return End If Dim sheet As FarPoint.Web.Spread.SheetView = FpSpread1.Sheets(0) FarPoint.Web.Spread.FilterColumnDefinition '非表示フィルタの作成 Dim hf As New FarPoint.Web.Spread.HideRowFilter(sheet) 'カスタムフィルタを作成 Dim mc As New MyCustomFilter() 'シートの設定 mc.SheetView = sheet ' 1列目~3列目にカスタムフィルタを設定 For i As Integer = 0 To 2 Dim fcd As New FarPoint.Web.Spread.FilterColumnDefinition(i, FarPoint.Web.Spread.FilterListBehavior.Custom) 'カスタムフィルタを定義に追加 fcd.Filters.Add(mc) 'フィルタ定義を非表示フィルタに追加 hf.AddColumn(fcd) Next 'フィルタをシートに設定 sheet.RowFilter = hf End Sub <Serializable()> _ Public Class MyCustomFilter Inherits FarPoint.Web.Spread.DefaultFilterItem Private sv As FarPoint.Web.Spread.SheetView = Nothing Public Sub New() End Sub Public Overloads Overrides ReadOnly Property DisplayName() As String Get Return "1000~5000" End Get End Property 'フィルタに表示する文字列 Public Overloads Overrides WriteOnly Property SheetView() As FarPoint.Web.Spread.SheetView Set(ByVal value As FarPoint.Web.Spread.SheetView) sv = value End Set End Property Private Function IsNumeric(ByVal ovalue As Object) As Boolean Dim _isNumber As New System.Text.RegularExpressions.Regex("^\-?\d+\.?\d*$") Dim m As System.Text.RegularExpressions.Match = _isNumber.Match(Convert.ToString(ovalue)) Return m.Success End Function Public Function IsFilteredIn(ByVal ovalue As Object) As Boolean Dim ret As Boolean = False If IsNumeric(ovalue) Then If Double.Parse(Convert.ToString(ovalue)) >= 1000 AndAlso Double.Parse(Convert.ToString(ovalue)) <= 5000 Then ret = True End If End If Return ret End Function Public Overloads Overrides Function Filter(ByVal columnIndex As Integer) As Integer() Dim ar As New System.Collections.ArrayList() Dim val As Object For i As Integer = 0 To sv.RowCount - 1 val = sv.GetValue(i, columnIndex) If IsFilteredIn(val) Then ar.Add(i) '条件に合致する行番号をリストに追加 End If Next Return DirectCast((ar.ToArray(GetType(Int32))), Int32()) End Function Public Overloads Overrides Function ShowInDropDown(ByVal columnIndex As Integer, _ ByVal filteredInRowList As Integer()) As Boolean ' filteredInRowList引数は現在、フィルター・イン(フィルターの条件に合致した)行リスト ' 条件に合うデータが存在する時のみフィルター要素を表示します。 If filteredInRowList Is Nothing Then For i As Integer = 0 To sv.RowCount - 1 Dim value As Object = sv.GetValue(i, columnIndex) If value IsNot Nothing Then If IsFilteredIn(value) Then Return True End If End If Next Else ' 現在の行リストが条件に合致するか確認 For i As Integer = 0 To filteredInRowList.Length - 1 Dim row As Integer = filteredInRowList(i) Dim value As Object = sv.GetValue(row, columnIndex) If value IsNot Nothing Then If IsFilteredIn(value) Then Return True End If End If Next End If Return False End Function Public Overloads Overrides Function Serialize(ByVal w As System.Xml.XmlTextWriter) As Boolean w.WriteStartElement("MyCustomFilter") MyBase.Serialize(w) w.WriteEndElement() Return True End Function Public Overloads Overrides Function Deserialize(ByVal r As System.Xml.XmlNodeReader) As Boolean If r.NodeType = System.Xml.XmlNodeType.Element Then If r.Name.Equals("MyCustomFilter") Then MyBase.Deserialize(r) End If End If Return True End Function End Class