- 积分
- 18
- 在线时间
- 54 小时
- 主题
- 4
- 注册时间
- 2013-7-5
- 帖子
- 27
- 最后登录
- 2023-12-21
- 帖子
- 27
- 软币
- 491
- 在线时间
- 54 小时
- 注册时间
- 2013-7-5
|
本帖最后由 萧從军 于 2013-10-21 00:19 编辑
根据http://www.dxper.net/thread-1667-1-1.html问题帖。
对[GridControl列的自定义],这样做以后我们的列就不用不断的在gridview设计界面中添加列,只需要给对象赋特征值,就灵活了。
代码如下:
- <p> /// <summary>
- /// 初始化grid列
- /// </summary>
- /// <param name="grid"></param>
- /// <param name="typeName">加载对象如Model.BasicUser</param>
- protected virtual void InitGrid(GridView grid, string typeName)
- {
- grid.Columns.Clear();</p><p> if (string.IsNullOrEmpty(typeName))
- {
- return;
- }
- Type type = Type.GetType(typeName);
- if (type == null)
- {
- return;
- }</p><p> foreach (PropertyInfo item in type.GetProperties())
- {
- ColumnAttribute attribute = GetColumnAttribute(item);//定义对象的特征值,根据定义特征获取设置的值。
- if (attribute == null || string.IsNullOrWhiteSpace(attribute.Name) || attribute.Width == 0)
- {
- continue;
- }
- grid.OptionsView.EnableAppearanceEvenRow = true;
- grid.OptionsView.EnableAppearanceOddRow = true;
- grid.Appearance.EvenRow.BackColor = Color.WhiteSmoke;
- grid.Appearance.OddRow.BackColor = Color.White;
- grid.Appearance.FocusedRow.BackColor = Color.RoyalBlue;
- grid.Appearance.FocusedRow.ForeColor = Color.White;
- grid.OptionsView.ColumnAutoWidth = false;
- var gridColumn = new GridColumn();
- if (!string.IsNullOrEmpty(attribute.DisplayFormat))
- {
- gridColumn.DisplayFormat.FormatString = attribute.DisplayFormat;
- gridColumn.DisplayFormat.FormatType = FormatType.Custom;
- }
- if (!string.IsNullOrEmpty(attribute.Align))
- {
- gridColumn.AppearanceCell.Options.UseTextOptions = true;
- gridColumn.AppearanceCell.TextOptions.HAlignment =
- (HorzAlignment)Enum.Parse(typeof(HorzAlignment), attribute.Align, true);
- }
- gridColumn.OptionsColumn.AllowEdit = false;
- gridColumn.OptionsColumn.AllowFocus = !attribute.IsReadOnly;
- gridColumn.OptionsColumn.ReadOnly = attribute.IsReadOnly;
- gridColumn.OptionsFilter.AllowAutoFilter = false;
- gridColumn.OptionsFilter.AllowFilter = false;
- gridColumn.Caption = attribute.Name;
- gridColumn.Width = attribute.Width;
- gridColumn.FieldName = item.Name;
- gridColumn.Visible = true;
- gridColumn.VisibleIndex = grid.Columns.Count;
- gridColumn.AppearanceHeader.Options.UseTextOptions = true;
- gridColumn.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;</p><p> //根据列的属性值都可以在特征值里面定义,再通过初始化GridControl列来负责
- grid.Columns.Add(gridColumn);
- }
- }</p></p>
复制代码 根据gridcontrol自定义列,那么treelist自定义列就类似一样了。如有兴趣可以尝试,有任何问题可以以前讨论。
|
评分
-
查看全部评分
|