CellContentClickは、その名前の通りセルの内容がクリックされた際に発生します。
CellClickイベントとの違いは、CellClickイベントはセルの一部、つまりセルの内容じゃ
ない部分をクリックしても発生します。
CellContentClickイベントは、セルの内容を直接クリックした場合にしか発生しません。
なので、CellContentClickはボタンセルなどの場合に使用します。
以下サンプルです。
CellClickイベントは、セルの一部のクリックが発動条件なので
セルの枠をクリックしても発生します。
CellContentClickイベントは、セルの内容のクリックが発動条件なので
セルの値の部分をクリックしないと発生しません。
// vim:set ts=4 sw=4 et ws is nowrap ft=cs: using System; using System.Windows.Forms; using Gsf.Samples.Utility; namespace Gsf.Samples.DGV{ /// <summary> /// DataGridView.CellContentClickイベントについてのサンプルです。 /// </summary> /// <remarks> /// CellClickイベントは、セルの一部をクリックした際に発生します。 /// セルの内容がクリックされたイベントを補足する場合は、CellContentClickイベント /// の方を使用します。Buttonカラムやリンクカラムの場合はCellContentClickイベントを /// を補足します。 /// /// MSDNのドキュメントにも記載がありますが、CellClick,CellContentClickイベントの場合 /// で、セルがチェックボックスセルの場合は、値が変更される前のものが取得されます。 /// セルがチェックボックスの場合は、CellValueChangedイベントを補足します。 /// </remarks> public class DataGridViewSample15 : BaseForm{ public DataGridViewSample15() : base(typeof(DataGridViewSample15).FullName){ // nop; } protected override void InitializeComponents(){ // // DataGridView & BindingSource // DataGridView grid = new DataGridView(); grid.Dock = DockStyle.Fill; grid.AutoGenerateColumns = true; grid.DataSource = GetDataSource(); // // CellClick, CellContentClickイベントにコールバックを設定 // grid.CellClick += OnCellClick; grid.CellContentClick += OnCellContentClick; Controls.Add(grid); } object GetDataSource(){ BindingSource source = new BindingSource(); source.DataSource = typeof(Sample); source.Add(new Sample(0, "gsf_zero1")); source.Add(new Sample(1, "gsf_zero2")); return source; } /// <summary> /// セルの一部がクリックされた際に発生するCellClickイベントの /// コールバックメソッドです。 /// </summary> /// <param name="sender">イベント送信元オブジェクト</param> /// <param name="args">イベント引数オブジェクト</param> /// <remarks> /// クリックされたセルの位置と値をメッセージボックスにて /// 表示します。 /// </remarks> protected void OnCellClick(object sender, DataGridViewCellEventArgs args){ ShowCellInformation(sender, args, "CellClick"); } /// <summary> /// セルの値自体がクリックされた際に発生するCellContentClickイベントの /// コールバックメソッドです。 /// </summary> /// <param name="sender">イベント送信元オブジェクト</param> /// <param name="args">イベント引数オブジェクト</param> /// <remarks> /// クリックされたセルの位置と値をメッセージボックスにて /// 表示します。 /// </remarks> protected void OnCellContentClick(object sender, DataGridViewCellEventArgs args){ ShowCellInformation(sender, args, "CellContentClick"); } void ShowCellInformation(object sender, DataGridViewCellEventArgs args, string boxTitle){ DataGridView g = sender as DataGridView; if(g != null){ int col = args.ColumnIndex; int row = args.RowIndex; // // クリックがヘッダー部分などの場合は、どちらかの // インデックスが-1となります。 // if(col >= 0 && row >= 0){ MessageBox.Show(string.Format("行:{0}, 列:{1}, 値:{2}", row, col, g[col, row].Value), boxTitle); } } } [STAThread] static void Main(){ ApplicationUtility.Launch(typeof(DataGridViewSample15).FullName); } } class Sample{ int? _id; string _name; public Sample() : this(null, null){ // nop; } public Sample(int? id, string name){ _id = id; _name = name; } public int? Id{ get{ return _id; } set{ _id = value; } } public string Name{ get{ return _name; } set{ _name = value; } } } }
================================
過去の記事については、以下のページからご参照下さい。
- いろいろ備忘録日記まとめ
サンプルコードは、以下の場所で公開しています。
- いろいろ備忘録日記サンプルソース置き場