///////////////////
//ソートダイアログ
///////////////////
class SortDlg : Form
{ //ダイアログのフィールド
public int ColIndex; //解説:ソートの対象となる列番号です。
public bool IsAscending; //解説:昇順、降順のフラグです。
///////////////////////////////
// IDList_CS.cs
// Copyright (c) 2026 by ysama
// https://learn.microsoft.com/ja-jp/dotnet/api/system.windows.forms.datagridview?view=windowsdesktop-10.0
///////////////////////////////
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel; //[DisplayName()]使用の為
using System.IO; //StreamReader使用の為
using System.Text; //Encoding.GetEncoding使用の為
using System.Security.Cryptography; //データの暗号化の為
namespace IDList_CS
{ ///////////////////////////////////
//IDListで使用するIDデータのクラス
/////////////////////////////////// public classidItem
{ //解説:これについては既に「【DataGridView】IDList for CS写真集」の「3.編集操作」で触れていますので省略します。
}
/////////////////////////////////////
//idListクラス(DataGridViewから派生)
///////////////////////////////////// public classidList : DataGridView; //解説:これが今回の主役(心臓部)です。
{ //idItemのデータリスト(解説:idItemクラスインスタンスをListに入れて使います。)
List <idItem> idItems = new List <idItem> (); //DataGridViewのバインディングデータソース(解説:今回初めて知りました。DataGridViewとListをつなぐインターフェースです。)
BindingSource bs = new BindingSource(); //システムデータファイルパス(解説:セキュリティ上はユーザーの好きなところに作らせ、そのパスも暗号化すべきでしょうが...)
const string dfPath = ".\\IDListFiles\\IDList.dat"; //解説:相対パスはChat-GPTに批判されましたが、これでいいです。 //コンテキストメニュー(解説:右クリックでポップアップさせるメニューです。)
ContextMenuStrip cms = new ContextMenuStrip(); //並び替え順(true - 昇順、false - 降順-解説:ソートの為にこれに記憶させておきます。)
bool sortOrder = true;
前回の【DataGridView】IDList for CS写真集でアプリの具体的な全容がお分かりだと思いますので、今回は「このプログラムの中核である機能を持つIDList_CS.dllをコントロールとして持つ器のウィンドウプログラム」の(一応)すべてのコードを掲載するとともに、IDList_CS.dllに係る部分(注)の解説を行います。
///////////////////////////////
// IDList.cs
// Copyright (c) 2026 by ysama
///////////////////////////////
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Reflection; //Assemblyを使う為
using System.Resources; //リソース関係クラス等の使用の為
using System.IO; //StreamReader使用の為
using System.Text; //Encoding.GetEncoding使用の為
using IDList_CS; //IDList.dll(idList使用の為)
注:尚、Google Geminiによれば、DataGridViewが"List<T>"を含むデータ集合をDataSourceメソッドで読み込むためには「"また、これらのオブジェクトにバインドするときは、公開プロパティのみをバインドでき、公開フィールドはバインドできません。"(英語原文: "Also, when binding to these objects, only the public properties (not public fields) can be bound.")」(Geminiが引用した出典-ただし和訳、英語原文共に引用されている文は見当たりませんでした<爆>)
///////////////////////////////////
//IDListで使用するIDデータのクラス
/////////////////////////////////// public class idItem
{
[DisplayName("メンバーシップ")]
public string Membership {get; set;}
[DisplayName("ログインID")]
public string ID {get; set;}
[DisplayName("パスワード")]
public string Password {get; set;}
[DisplayName("備考")]
public string Remark {get; set;}
//比較メソッドの定義 public int CompareMembershipTo(idItem other)
{
if(ReferenceEquals(other, null)) //参照比較 return 1; //thisが大きい //0なら等しい、負なら小さい、正なら大きい return this.Membership.CompareTo(other.Membership);
}
public int CompareIDTo(idItem other)
{
if(ReferenceEquals(other, null)) //参照比較 return 1; //thisが大きい //0なら等しい、負なら小さい、正なら大きい return this.ID.CompareTo(other.ID);
}
public int ComparePasswordTo(idItem other)
{
if(ReferenceEquals(other, null)) //参照比較 return 1; //thisが大きい //0なら等しい、負なら小さい、正なら大きい return this.Password.CompareTo(other.Password);
}
public int CompareRemarkTo(idItem other)
{
if(ReferenceEquals(other, null)) //参照比較 return 1; //thisが大きい //0なら等しい、負なら小さい、正なら大きい return this.Remark.CompareTo(other.Remark);
}
}
ps. これは単なる移植プログラムですし、器となるメインウィンドウはCells for CSやBoids for CSの使いまわしなので、全部見たい方はBCCForm and BCCSkeltonパッケージにアップしますので、そこから見ていただき、「ブログではつまみ食い」程度のコード紹介に止めようかと思っています。
注:A highball is a mixed drink made from a base spirit, like whisky or gin, and a large amount of carbonated mixer, such as soda water or ginger ale, served over ice in a tall glass. (Gemini)