いろいろ備忘録日記

主に .NET とか Go とか Flutter とか Python絡みのメモを公開しています。

DataGridView入門記-13 (セルが属する行と列)(OwningRow, OwningColumn)


現在選択中のセルオブジェクトは

DataGridView.CurrentCell

プロパティで取得できます。


取得できるオブジェクトは

DataGridViewCell

オブジェクトとなります。


このオブジェクトからは、

  • このセルの現在の値(Valueプロパティ)
  • このセルが存在している行オブジェクト(OwningRowプロパティ)
  • このセルが存在しているカラムオブジェクト(OwningColumnプロパティ)

が取得できます。


以下サンプルです。

// vim:set ts=4 sw=4 et ws is nowrap ft=cs:

using System;
using System.Drawing;
using System.Windows.Forms;

using Gsf.Samples.Utility;

namespace Gsf.Samples.DGV{

    /// <summary>
    /// DataGridView.CurrentCellのサンプルです。
    /// </summary>
    /// <remarks>
    /// CurrentCellプロパティからは、現在選択中のセルオブジェクトが
    /// 取得できます。
    /// 取得できるオブジェクトはDataGridViewCellクラスのインスタンスです。
    /// セルオブジェクトからは
    /// -OwningRow
    /// -OwningColumn
    /// プロパティより属する行オブジェクトとカラムオブジェクトが取得できます。
    /// </remarks>
    public class DataGridViewSample13 : BaseForm{

        public DataGridViewSample13() : base(typeof(DataGridViewSample13).FullName){
            // nop;
        }

        protected override void InitializeComponents(){
            //
            // Controls.
            //
            DataGridView         grid           = new DataGridView();
            StatusStrip          statusBar      = new StatusStrip();
            ToolStripStatusLabel statusBarLabel = new ToolStripStatusLabel();

            //
            // StatusBar
            //
            statusBar.Name           = "statusBar";
            statusBarLabel.Name      = "statusBarLabel";
            statusBarLabel.Spring    = true;
            statusBarLabel.Text      = "ready.....";
            statusBarLabel.TextAlign = ContentAlignment.MiddleLeft;

            statusBar.Dock = DockStyle.Bottom;

            statusBar.Items.Add(statusBarLabel);

            //
            // DataGridView
            //
            grid.Dock                = DockStyle.Fill;
            grid.AutoGenerateColumns = true;
            grid.DataSource          = GetDataSource();

            grid.CellClick += delegate(object sender, DataGridViewCellEventArgs e){
                //
                // カレントセルを取得し、そのセルが属する行オブジェクトをさらに取得し
                // ステータスバーに表示.
                //
                DataGridView g = sender as DataGridView;

                if(g != null){
                    //
                    // ToolStripStatusLabel(ToolStripItem)は, Controlクラスのサブクラスでは
                    // ないので、一旦フォームからStatusStripを取得し、そこからさらにFindメソッドを
                    // 使用して、ラベルを取得する
                    //
                    Control[] controls = g.FindForm().Controls.Find("statusBar", false);
                    if(controls.Length != 0){

                        ToolStripItem[] toolStripItems = (controls[0] as StatusStrip).Items.Find("statusBarLabel", false);
                        if(toolStripItems.Length != 0){

                            DataGridViewCell currentCell = g.CurrentCell;

                            toolStripItems[0].Text = string.Format("{0} -- Value: {1}", currentCell.OwningRow.DataBoundItem.ToString(), currentCell.Value.ToString());
                        }
                    }

                }
            };

            Controls.Add(grid);
            Controls.Add(statusBar);

            Size = new Size(450, 200);
        }

        object GetDataSource(){
            BindingSource source = new BindingSource();

            source.DataSource = typeof(Sample);

            source.Add(new Sample(1, "gsf_zero1"));
            source.Add(new Sample(2, "gsf_zero2"));

            return source;
        }

        [STAThread]
        static void Main(){
            ApplicationUtility.Launch(typeof(DataGridViewSample13).FullName);
        }
    }

    class Sample{
        int    _id;
        string _name;

        public Sample() : this(-1, 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;
            }
        }

        public override string ToString(){
            return string.Format("id:{0}, name:{1}", _id, _name);
        }
    }
}

次からは、イベント関連をやってみようかなと思います。



================================
過去の記事については、以下のページからご参照下さい。

サンプルコードは、以下の場所で公開しています。