いろいろ備忘録日記

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

DevExpress奮闘記-088 (XtraGrid, エンター押下で1行下の列に移動, KeyUp, FocusedRowHandle, ShowEditor)


本家のブログにて、XtraGridにてExcelのように、エンター押下で下の行の列に移動させるためのTIPSが公開されています。
これ、グリッド利用している人は一回はやったことがあるんじゃないでしょうか?


やり方は簡単で、KeyUpイベントをハンドルして対象となるキーが
来たタイミングで処理をいれるだけです。


利用するプロパティ及びメソッドは以下のもの。

  • FocusedRowHandle
    • 現在の行ハンドルを返します。
  • FocusedColumn
    • 現在の列を返します。
  • ShowEditorメソッド
    • アクティブセルのエディタを起動します。つまり編集状態にします。


本家のブログでもサンプルコードが載っているんですが
こちらの方でも、ついでに作成してみましたので以下に記述しておきます。


以下、サンプルです。
まずは、Program.csから。


アプリ起動直前に、XPOの接続設定とフォームスキンの設定をしています。
今回、DBに接続するのは面倒なので、InMemoryDataStoreを利用。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.Xpo;
using DevExpress.Xpo.DB;

namespace DevExpress_XtraGrid_Enterキーで下の列に移動
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            SkinManager.EnableFormSkins();
            XpoDefault.DataLayer = new SimpleDataLayer(new InMemoryDataStore());

            Application.Run(new MainForm());
        }
    }
}


続いて、XPOクラス.
グリッドのデータソースとして利用します。

using System;
using System.Collections.Generic;
using DevExpress.Xpo;

namespace DevExpress_XtraGrid_Enterキーで下の列に移動
{
    public class GridModel : XPLiteObject
    {
        public GridModel(Session session) : base(session) { }

        [Key]
        public string Id
        {
            get
            {
                return GetPropertyValue<string>("Id");
            }
            set
            {
                SetPropertyValue<string>("Id", value);
            }
        }

        public string Name
        {
            get
            {
                return GetPropertyValue<string>("Name");
            }
            set
            {
                SetPropertyValue<string>("Name", value);
            }
        }

        public int? Age
        {
            get
            {
                return GetPropertyValue<int?>("Age");
            }
            set
            {
                SetPropertyValue<int?>("Age", value);
            }
        }
    }
}


最後に、フォームクラス。
KeyUpイベントで列移動の処理を行っています。
また、DataSourceChangedイベントで初期データを投入しています。

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.Xpo;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;

namespace DevExpress_XtraGrid_Enterキーで下の列に移動
{
    public partial class MainForm : XtraForm
    {
        public MainForm()
        {
            InitializeComponent();
            Load += MainForm_Load;
        }

        void MainForm_Load(object sender, EventArgs e)
        {
            grdMain.DataSourceChanged += grdMain_DataSourceChanged;
            grvMain.KeyUp             += grvMain_KeyUp;

            grdMain.DataSource = new XPCollection<GridModel>(){ DisplayableProperties = "Id;Name;Age" };
        }

        void grvMain_KeyUp(object sender, KeyEventArgs e)
        {
            GridView view = sender as GridView;
            
            if (e.KeyCode == Keys.Enter)
            {
                int rowHandle = view.FocusedRowHandle;
                if (rowHandle != GridControl.InvalidRowHandle)
                {
                    GridColumn col = view.FocusedColumn;

                    //
                    // 次の行位置と列位置を設定する. 
                    // %を指定しているのは最終行の場合に次のRowHandleを先頭行にするため.
                    //
                    view.FocusedRowHandle = (++rowHandle % view.RowCount);
                    view.FocusedColumn    = col;

                    //
                    // アクティブセルのエディタを起動.
                    //
                    view.ShowEditor();
                }
            }
        }

        void grdMain_DataSourceChanged(object sender, EventArgs e)
        {
            GridControl             grid = sender          as GridControl;
            XPCollection<GridModel> ds   = grid.DataSource as XPCollection<GridModel>;
         
            if (ds.Count > 0)
            {
                return;
            }

            using (UnitOfWork uow = new UnitOfWork(ds.Session.DataLayer))
            {
                for (int i = 0; i < 10; i++)
                {
                    ds.Add(new GridModel(ds.Session) { Id = i.ToString(), Name = string.Format("Name-{0}", i), Age = (i + 18) });
                }
            	
                uow.CommitChanges();
            }
        }
    }
}


KeyUpの部分をちょいと変更したら、最終行でエンター押下時に先頭行の次の列に移動とかも簡単にできますね。


できれば、このような移動方法は、いちいち開発者が
実装するのではなく、オプションで用意しておいてほしいなと思ったり・・・。


上記のサンプルは以下からダウンロードできます。

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