C# Devpress GridView Columns应用

GridView动态显示隐藏列,并保存客户的配置

Devpress GridView 没有RowCreated事件,无法用自带控件自定义选中数据列的显示

这里选择带有CheckedList属性的控件,可选CheckedListBoxControl,CheckedListBox,CheckedComboBoxEdit

这里我用的控件时CheckedComboBoxEdit

主要控件:RibbonForm,GridControl 的GridView(GridVIew1),CheckedComboBoxEdit(displayBox)

方法步骤:

1.窗体加载事件(Load)时遍历GridView

读取选中GridView所有列的列名(Caption)和绝对索引(AbsoluteIndex)

Colums存在四个索引属性,其中VisibleIndex会在这条列被隐藏后变为-1,所以不适合使用

放入集合中 Dictionary<string, int> cols = new Dictionary<string, int>();

Dictionary<string, int> cols = new Dictionary<string, int>();
for (int x = 0; x < gridView1.Columns.Count; x++)
            {
                string cap = gridView1.Columns[x].Caption.ToString();
                int field = gridView1.Columns[x].AbsoluteIndex+1;

                cols.Add(cap, field);
                displayBox.Properties.Items.Add(cap);
            }

同时将列标题存入下拉多选控件中

2.设置下拉多选框的EditValueChanged事件

在选中需要显示的数据列后触发displayBox_EditValueChanged事件

获取多选项的文本值(displayBox.EditValue),"公司名称, 个人税号, 部门, 车间"

这里存在 ', ' 这个符号,逗号后有空字符,需要用replace'方法转换下方便切割

再存入集合中

string str=displayBox.EditValue.ToString().Replace(", ","-");
           
List<string> colList = str.Split(new Char[] { '-' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            

注:再初始化加载时因为多选框会一直加入数据列列名所以会一直触发这个事件,需要设置变量控制这个事件再初始化时触发

获取需要显示的数据列后重新设置显示列,这里的cols[item]就是之前设置的绝对索引,

设置显示后显示坐标VisibleIndex是被打乱的不方便查看,也要重新按绝对索引的值设置

for (int i = 0; i < gridView1.Columns.Count; i++)
                {
                    gridView1.Columns[i].Visible = false;
                }
                colList.ForEach(item =>
                {
                   
                    if (cols.ContainsKey(item))
                    {
                        
                        gridView1.Columns[cols[item]].Visible = true;
                        gridView1.Columns[cols[item]].VisibleIndex = cols[item];
                    }

                });

3.保存和读取设置

  private void saveLayout()
        {
            string path = Path.GetDirectoryName(Application.ExecutablePath) + "\\LayoutStyle\\";
            string file = this.Name + ".xml";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            gridControl1.MainView.SaveLayoutToXml(path + file);
        }

        private void getLayout()
        {
            string path = Path.GetDirectoryName(Application.ExecutablePath) + "\\LayoutStyle\\";
            string file = this.Name + ".xml";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            if (!File.Exists(path + file))
                return;
            gridControl1.MainView.RestoreLayoutFromXml(path + file);
        }

        private void Frm_ryxx_FormClosed(object sender, FormClosedEventArgs e)
        {
            saveLayout();
        }

GridView获取变量名和表头名实现动态导入excel

                        
                        string filepath = dialog.FileName.ToString();
                        //导入设置字典
                        Dictionary<string, string> exconfig = new Dictionary<string, string>();

                        Workbook workbook = new Workbook();
                        workbook.LoadDocument(filepath);
                        var sheets = workbook.Worksheets.ToArray(); ;

                       
                        Worksheet sheet = sheets[0];


                        //根据Excel管理中确定的标题行titlenum   读取所有需要导入的标题名存入dt中
                        var col = sheet.Rows[0].Where(k => k.Value.ToString().Trim() != "").ToList();

                        //获取所有列表名和参数名字典
                        Dictionary<string, string> cols = new Dictionary<string, string>();


                        for (int x = 0; x < gridView1.Columns.Count; x++)
                        {
                            string cap=gridView1.Columns[x].Caption.ToString();
                            string field = gridView1.Columns[x].FieldName.ToString();

                            cols.Add(cap, field);

                        }

                        for (int i = 0; i < col.Count; i++)
                        {
                            if (cols.ContainsKey(col[i].Value.ToString()))
                            {
                                string str = cols[col[i].Value.ToString()];
                                exconfig.Add(i.ToString(), str);
                            }
                           
                        }

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
用官方汉化包汉化DXperience控件详细步骤!! 1.你要先安装某一版本的DXperience控件(如:DXperience-11.2.5.exe)可以在这里http://www.devexpresscn.com/下载。 2.安装DXperience控件到某一目录下。(如:D:\DevExpress 11.2) 3.下载对应的汉包。(如:DXperience-11.2.5.exe的汉化包,名称为:dxKB_A421_DXperience_v11.2_(2012-04-27).zip)可以在http://www.devexpresscn.com/news/DevExpress-news-94.html下载。 4.解压汉包,在解压后的文件夹中找到DevExpress.DLL文件夹,在DevExpress.DLL文件夹中找到zh-CHS文件夹。 5.把zh-CHS文件夹复制到你建立的工程下的Debug文件夹中。(如 D:\Test\汉化Test\汉化Test\bin\Debug) 到这里汉化资源已经到位了。下面是在编程时使用汉化资源。(以C#为例) 6.在编程时,在Main方法中加入System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-CHS");(C#中Main方法在Program.cs文件中) 比较: 加入前: using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace 汉o化?¥Test { static class Program { /// <summary> /// 应?|用??程¨?序¨°的ì?主??入¨?口¨2点ì?。?ê /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } } 加入后: using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace 汉o化?¥Test { static class Program { /// <summary> /// 应?|用??程¨?序¨°的ì?主??入¨?口¨2点ì?。?ê /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-CHS"); Application.Run(new Form1()); } } } 到这汉化已经全部完成。接下来就是托控件了(使用控件)。如果你连托控件都不会,那么……我已被震亡了!! 说明:这个方法汉化不完全。(是官方汉化包的问题)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值