いろいろ備忘録日記

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

DevExpress奮闘記-092 (XtraGrid, AllowFixedGroups, スクロール時にグループ帯を常に表示)


v2011 vol.1にて、XtraGridにAllowFixedGroupsという機能が追加されました。
(v2011 vol.2で、WPF側にも追加されています。)
この機能をONにして、グルーピングを行っている状態でスクロールすると
グループのヘッダ部分が常に表示されるようになります。


デフォルトでOFFとなっているので、よく忘れるためメモメモ。


有効にするには、以下のようにします。

gridView1.OptionsBehavior.AllowFixedGroups = DefaultBoolean.True;


OFFにしている場合(つまりノーマルの状態)だと、以下のように
1グループの行数が多い場合、ヘッダ部分がスクロール時に見えなくなります。

ONにすると、以下のように常にヘッダ部分が表示されるようになります。


以下、サンプルです。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

using DevExpress.Utils;
using DevExpress.XtraSplashScreen;

namespace WindowsFormsApplication1
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      checkEdit1.Properties.ValueChecked = DefaultBoolean.True;
      checkEdit2.Properties.ValueChecked = DefaultBoolean.False;

      checkEdit1.Checked = true;
    }

    private void Form1_Shown(object sender, EventArgs e)
    {
      LoadData();
    }

    private void btnReload_Click(object sender, EventArgs e)
    {
      gridView1.ClearGrouping();
      gridControl1.DataSource = null;

      LoadData();
    }

    private void LoadData()
    {
      SplashScreenManager.ShowForm(typeof(WaitForm1));

      Task.Factory.StartNew<DataList>(() =>
      {
        return GetDataSource();
      })
      .ContinueWith((prevTask) =>
      {
        DataList result = prevTask.Result;
        SetDataSource(result);
      },
      CancellationToken.None,
      TaskContinuationOptions.None,
      TaskScheduler.FromCurrentSynchronizationContext());
    }

    private DataList GetDataSource()
    {
      DataList result = new DataList();

      Random rnd = new Random();
      for (int i = 0; i < 1000; i++)
      {
        string dataId = string.Format("ID-{0}", (i + 1));

        Data data = new Data() 
                    { 
                      Id = dataId, 
                      Name = string.Format("Name-{0}", dataId), 
                      Value = rnd.Next(10), 
                      Childs = new List<ChildData>() 
                    };

        for (int j = 0; j < 500; j++)
        {
          string childId = string.Format("CHILDID-{0}", (j + 1));

          ChildData childData = new ChildData() 
                                { 
                                  Id = childId, 
                                  ParentId = dataId, 
                                  Name = string.Format("CHILDNAME-{0}", childId), 
                                  Value = rnd.Next(500) 
                                };

          data.Childs.Add(childData);
        }

        result.Add(data);
      }

      return result;
    }

    private void SetDataSource(DataList result)
    {
      gridControl1.DataSource = result;

      gridView1.OptionsBehavior.Editable = false;
      gridView1.OptionsBehavior.AllowFixedGroups = 
        (DefaultBoolean) (checkEdit1.Checked ? checkEdit1.EditValue : checkEdit2.EditValue);

      gridView1.Columns.ColumnByFieldName("Value").Group();

      SplashScreenManager.CloseForm();
    }
  }

  class DataList : List<Data>
  {
  }

  class Data
  {
    public string Id { get; set; }
    public string Name { get; set; }
    public int Value { get; set; }

    public List<ChildData> Childs { get; set; }
  }

  class ChildData
  {
    public string Id { get; set; }
    public string ParentId { get; set; }
    public string Name { get; set; }
    public int Value { get; set; }
  }
}


サンプルは以下の場所にアップしてあります。
https://sites.google.com/site/gsfzero1/Home/XtraGrid_AllowFixedGroups.zip?attredirects=0&d=1


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