很多朋友发现GridControl控件中很多属性(属性太多,主要是不愿意看英文文档或说明的朋友)不知道怎么使用,这里博主给大家用一个方法的代码加注释给大家介绍下:
public static void SetGridViewFormat(GridView gridView) { // 设置奇数行颜色 默认也是白色 可以省略 gridView.Appearance.OddRow.BackColor = Color.White; // 使奇数行颜色有效 gridView.OptionsView.EnableAppearanceOddRow = true; // 设置偶数行颜色 gridView.Appearance.EvenRow.BackColor = Color.FromArgb(244, 244, 244); // 使偶数行颜色有效 gridView.OptionsView.EnableAppearanceEvenRow = true; // 设置行选中颜色 gridView.Appearance.FocusedRow.BackColor = Color.FromArgb(231, 243, 251); // 设置行高 gridView.RowHeight = 28; // 光标单击选择一整行 gridView.OptionsSelection.EnableAppearanceFocusedCell = false; // 关闭列头筛选 gridView.OptionsCustomization.AllowFilter = false; // 禁止用户手动调整列位置 gridView.OptionsCustomization.AllowColumnMoving = false; // 关闭列头右键菜单 gridView.OptionsMenu.EnableColumnMenu = false; // 关闭列头点击排序 gridView.OptionsCustomization.AllowSort = false; // 禁止用户手动调整列宽度 gridView.OptionsCustomization.AllowColumnResizing = false; // 禁止用户手动调整列位置 gridView.OptionsCustomization.AllowColumnMoving = false; // 列样式设置 foreach (DevExpress.XtraGrid.Columns.GridColumn item in gridView.Columns) { // 标题列居中 item.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; // 内容居中 item.AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; // 非控件列,设置为不可编辑 if (item.ColumnEdit == null) { item.OptionsColumn.AllowEdit = false; } } }
绑定行号:
/// <summary> /// 显示行序号 /// </summary> public static void ShowRowIndicator(GridView gridView) { // 定义序号行的宽度 gridView.IndicatorWidth = 40; // 绑定行号 gridView.CustomDrawRowIndicator += gridView_CustomDrawRowIndicator; } /// <summary> /// 序号行初始化 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void gridView_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e) { try { if (e.Info.IsRowIndicator && e.RowHandle >= 0) { e.Info.DisplayText = (e.RowHandle + 1).ToString(); } } catch { } }
文章评论