C# Devexpress GirdView 保存用户界面

        最近写了一个程序,界面上只有一个GridControl 控件,用来显示不同分类的数据。

      GridControl 显示数据的时候,用户可能设置分组列(如下图)来改变数据的显示样式;下次再次显示该类数据的时候,自动将数据显示成上次显示的样式。(如下图:当前的数据把“asd1”列作为分组列,下次显示数据的是自动把“asd1”列作为分组列)。



如何实现上述的要求呢?


<pre name="code" class="csharp"> //GridView 界面样式的保存路径
        private string _templateDirectPath = string.Format("{0}\\TemplateFile", System.Environment.CurrentDirectory);

        //GridView 界面样式的名称
        private string _templateName = string.Empty;

        private void ShowData(DataTable dt)
        {
            SaveCurrentGridViewLayout();
            gridView1.Columns.Clear();
            gridControl1.DataSource = dt;
            _templateName = dt.Namespace;
            LoadCurrentGridViewLayout();
        }

        //保存当前GridView 的模板
        private void SaveCurrentGridViewLayout()
        {
            //如果GridControl 数据源为空 或者模板文件名称为空 不保存模板。
            if (gridControl1.DataSource ==null ||string.IsNullOrEmpty(_templateName))  
                return;

            if (!Directory.Exists(_templateDirectPath))
                Directory.CreateDirectory(_templateDirectPath);

            string filePath = string.Format("{0}\\{1}}.xml", _templateDirectPath, _templateName);
            
            gridView1.SaveLayoutToXml(filePath);
        }

        //加载当前 GridView 的界面模板
        private void LoadCurrentGridViewLayout()
        {
            if (string.IsNullOrEmpty(_templateName))
                return;

            string currentTemplatePath = string.Format("{0}\\{1}.xml", _templateDirectPath, _templateName);
            if (!Directory.Exists(_templateDirectPath) || !File.Exists(currentTemplatePath))
                return;

            gridView1.RestoreLayoutFromXml(currentTemplatePath);
        }


 

注意事项:必须先绑定数据(gridControl1.DataSource = dt;),然后再加在模板(LoadCurrentGridViewLayout();),否则不能正确显示样式。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值