C# WinForm反射窗体控件gridView并对其增加列操作

        /// <summary>
        /// 在窗体中根据名称查找gridview需要增加的列
        /// </summary>
        public void GetGridViewInWin()
        {
            try
            {
                BLL.SYS.ctlmWinAddColumn bllColumn = new BLL.SYS.ctlmWinAddColumn();
                //取得窗体属性
                Type FormType = this.GetType();
                //取得所有控件
                //FieldInfo[] fi = FormType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
                string FieldName="";
                DataTable dtList = bllColumn.GetColumnByWinAndGridid(this.FindForm().GetType().FullName);
//查找窗口控件数据列表
                foreach (DataRow dr in dtList.Rows)
                {
                    //取得指定名字的控件
                    FieldInfo fi = FormType.GetField(dr["gridViewId"].ToString().Trim(), BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
                    //如果gridview进行下列判断
                    if (fi.FieldType == typeof(DevExpress.XtraGrid.Views.Grid.GridView))
                    {
                        DevExpress.XtraGrid.Views.Grid.GridView gv = (fi.GetValue(this)) as DevExpress.XtraGrid.Views.Grid.GridView;
                        FieldName = dr["FieldName"].ToString();
                        DevExpress.XtraGrid.Columns.GridColumn gc = new DevExpress.XtraGrid.Columns.GridColumn();
                        gc.Caption = dr["Caption"].ToString();
                        gc.FieldName = FieldName;
                        gc.Name = dr["GridColumnName"].ToString();
                        gc.Width = Convert.ToInt32(dr["Width"]);
                        gc.VisibleIndex = Convert.ToInt32(dr["locationIndex"]);
                        gc.OptionsColumn.ReadOnly = Convert.ToBoolean(dr["readOn"]);
                        gc.OptionsColumn.AllowEdit = Convert.ToBoolean(dr["allowEdit"]);
                        gc.ColumnEdit = bllColumn.SetLookUpEdit(FieldName);
                        gv.Columns.Add(gc);

                    }
                }

            }
            catch 
            { 
            }
        }

主要实现自动添加gridView列,日常新增列时可以不用修改程序,维护基础数据即可

有下拉子窗口用的方法应该不是很好,如果有更好的方法,下面留言,感谢

存在下来的判断FieldName,有返回ColumnEdit的数据源即可

        /// <summary>
        /// 设置有LOOKUPEdit的列
        /// </summary>
        /// <param name="FieldName"></param>
        /// <returns></returns>
        public DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit SetLookUpEdit(string FieldName)
        {
            DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit rep = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();
            if (FieldName == "check_type")
            {
                rep.ValueMember = "value";
                rep.DisplayMember = "name";
                rep.DataSource = (new BLL.List()).GetType();
                return rep;
            }
            else
                return null;

        }
//(new BLL.List()).GetType(); 为获取LookUpEdit的数据源

 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页