this.gridView1.OptionsView.ShowColumnHeaders = true; //显示列标题
this.gridView1.OptionsView.ShowIndicator = true; // 显示行标题
this.gridView1.IndicatorWidth = 40; //行标题宽度
//显示行标题
this.gridView1.CustomDrawRowIndicator += (s, e) =>
{
e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; //行号对齐方式
if (e.Info.IsRowIndicator && e.RowHandle >= 0)
{
e.Info.DisplayText = (e.RowHandle + 1).ToString(); //行号
}
};
this.gridView1.OptionsView.ShowGroupPanel = false; // 不显示分组panel
this.gridView1.OptionsView.ShowGroupedColumns = true; //显示分组的列
this.gridView1.OptionsView.ShowFilterPanelMode = ShowFilterPanelMode.Never; //是否显示过滤面板
this.gridView1.OptionsView.EnableAppearanceEvenRow = true; //是否启用偶数行外观
this.gridView1.OptionsView.EnableAppearanceOddRow = true; //是否启用奇数行外观
this.gridView1.OptionsCustomization.AllowFilter = true; //是否允许过滤
this.gridView1.OptionsCustomization.AllowGroup = true; //是否允许分组
this.gridView1.OptionsCustomization.AllowColumnMoving = true; //是否允许移动列
this.gridView1.OptionsCustomization.AllowColumnResizing = false; //是否允许调整列宽
this.gridView1.OptionsCustomization.AllowSort = true; //是否允许排序
this.gridView1.OptionsSelection.EnableAppearanceFocusedCell = false; //是否允许选中单元格
this.gridView1.OptionsBehavior.EditorShowMode = EditorShowMode.Click;//允许选中行,不允许编辑
this.gridView1.OptionsBehavior.Editable = false; //是否允许用户编辑单元格
this.gridView1.OptionsMenu.EnableColumnMenu = false; //禁用列标题右键菜单
this.gridView1.ClearSorting();//清除排序
this.gridView1.PaintStyleName = "Flat"; //设置外观样式
//选中行设置
this.gridView1.Appearance.FocusedRow.BackColor = Color.Green; //选中的行
this.gridView1.Appearance.FocusedCell.BackColor = Color.GreenYellow; //选中的单元格
this.gridView1.Appearance.FocusedRow.ForeColor = Color.Gray; //字体颜色
//空白区域颜色
this.gridView1.Appearance.Empty.BackColor = Color.Gray;
//列标题颜色
this.gridView1.Appearance.HeaderPanel.BackColor = Color.Blue;
//表格列在表格工具栏自动统计
this.col.SummaryItem.DisplayFormat = "{0:#.##}"; //"共{0}笔"
this.col.SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;
或者this.col.SummaryItem.SetSummary(DevExpress.Data.SummaryItemType.Count, "共{0}笔");
#region 绘制复选框
//不允许排序
this.colIsCheck.OptionsColumn.AllowSort = DefaultBoolean.False;
//不显示列头
this.colIsCheck.OptionsColumn.ShowCaption = false;
this.gvMain.CustomDrawColumnHeader += gvMain_CustomDrawColumnHeader;
this.gvMain.MouseUp += gvMain_MouseUp;
private void gvMain_CustomDrawColumnHeader(object sender, DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgs e)
{
if (e.Column == this.colIsCheck)
{
nMainCheckBoxColumnHeaderRect = e.Bounds;
nMainCheckBoxColumn = e.Column;
//须把列头标题设置为空
e.Painter.DrawObject(e.Info);
//在列头中心显示复选框
int x = e.Bounds.X + (int)((e.Bounds.Width - CheckBoxRenderer.GetGlyphSize(e.Graphics, CheckBoxState.UncheckedNormal).Width) * 0.5);
int y = e.Bounds.Y + (int)((e.Bounds.Height - CheckBoxRenderer.GetGlyphSize(e.Graphics, CheckBoxState.UncheckedNormal).Height) * 0.5);
Point location = new Point(x, y);
CheckBoxState checkBoxState;
if (e.Column.Tag != null && e.Column.Tag.ToString() == "1")
checkBoxState = CheckBoxState.CheckedPressed;
else
checkBoxState = CheckBoxState.UncheckedNormal;
CheckBoxRenderer.DrawCheckBox(e.Graphics, location, checkBoxState);
e.Handled = true;
}
}
private void gvMain_MouseUp(object sender, MouseEventArgs e)
{
if (nMainCheckBoxColumnHeaderRect != Rectangle.Empty)
{
if (e.X > nMainCheckBoxColumnHeaderRect.X &&
e.X < (nMainCheckBoxColumnHeaderRect.X + nMainCheckBoxColumnHeaderRect.Width) &&
e.Y > nMainCheckBoxColumnHeaderRect.Y &&
e.Y < (nMainCheckBoxColumnHeaderRect.Y + nMainCheckBoxColumnHeaderRect.Height))
{
if (nMainCheckBoxColumn.Tag != null && nMainCheckBoxColumn.Tag.ToString() == "1")
{
nMainCheckBoxColumn.Tag = "0";
//在这写未全选逻辑
for (int i = 0; i < this.nMainGridDataTable.Rows.Count; i++)
{
this.nMainGridDataTable.Rows[i][this.colIsCheck.FieldName] = "N";
}
}
else
{
nMainCheckBoxColumn.Tag = "1";
//在这写全选逻辑
for (int i = 0; i < this.nMainGridDataTable.Rows.Count; i++)
{
this.nMainGridDataTable.Rows[i][this.colIsCheck.FieldName] = "Y";
}
}
gvMain.InvalidateColumnHeader(nMainCheckBoxColumn);
}
}
}
#endregion