一、常用控件样式
public void setDgv(DevExpress.XtraGrid.Views.Grid.GridView gridView1) { gridView1.OptionsView.ShowColumnHeaders = true; //显示列标题 gridView1.OptionsView.ShowIndicator = false; // 不显示行标题 gridView1.OptionsView.ShowGroupPanel = true; // 显示分组panel gridView1.OptionsCustomization.AllowGroup = true; //是否允许分组 gridView1.OptionsView.ShowGroupedColumns = true; //显示分组的列 gridView1.OptionsView.ShowFilterPanelMode = DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.Never; //是否显示过滤面板 gridView1.OptionsCustomization.AllowFilter = true; //是否允许过滤 gridView1.OptionsCustomization.AllowColumnMoving = true; //是否允许移动列 gridView1.OptionsCustomization.AllowColumnResizing = false; //是否允许调整列宽 gridView1.OptionsCustomization.AllowSort = true; //是否允许排序 gridView1.OptionsSelection.EnableAppearanceFocusedCell = false; //是否允许选中单元格 gridView1.OptionsBehavior.EditorShowMode = DevExpress.Utils.EditorShowMode.Click; gridView1.OptionsBehavior.Editable = false; //是否允许用户编辑单元格 gridView1.OptionsView.EnableAppearanceEvenRow = true; //是否启用偶数行外观 gridView1.OptionsView.EnableAppearanceOddRow = true; //是否启用奇数行外观 gridView1.OptionsMenu.EnableColumnMenu = false; //禁用列标题右键菜单 // gridView1.ClearSorting();//禁止排序 gridView1.PaintStyleName = "Flat"; //设置外观样式 DevExpress.XtraGrid.Views.Grid.GridViewAppearances Appearance1 = new DevExpress.XtraGrid.Views.Grid.GridViewAppearances(gridView1); //列标题颜色:System.Drawing.Color.FromArgb(((int)(((byte)(198)))), ((int)(((byte)(232)))), ((int)(((byte)(243))))); //奇数行 Appearance1.EvenRow.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(236)))), ((int)(((byte)(249)))), ((int)(((byte)(254))))); Appearance1.EvenRow.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); //偶数行 Appearance1.OddRow.BackColor = System.Drawing.Color.White; Appearance1.OddRow.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); //选中行 Appearance1.FocusedRow.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(254)))), ((int)(((byte)(211)))), ((int)(((byte)(128))))); Appearance1.FocusedRow.ForeColor = System.Drawing.Color.Black; //分组panel颜色 Appearance1.GroupPanel.BackColor = System.Drawing.Color.White; Appearance1.GroupPanel.BackColor2 = System.Drawing.Color.CornflowerBlue; //分组row颜色 Appearance1.GroupRow.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(198)))), ((int)(((byte)(232)))), ((int)(((byte)(243))))); Appearance1.GroupRow.BackColor2 = System.Drawing.Color.FromArgb(((int)(((byte)(198)))), ((int)(((byte)(232)))), ((int)(((byte)(243))))); //空白区域颜色 Appearance1.Empty.BackColor = System.Drawing.Color.LightYellow; //列标题颜色 Appearance1.HeaderPanel.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(210)))), ((int)(((byte)(229)))), ((int)(((byte)(249))))); //奇数行 gridView1.Appearance.EvenRow.BackColor = Appearance1.EvenRow.BackColor; gridView1.Appearance.EvenRow.Font = Appearance1.EvenRow.Font; //偶数行 gridView1.Appearance.OddRow.BackColor = Appearance1.OddRow.BackColor; gridView1.Appearance.OddRow.Font = Appearance1.OddRow.Font; //选中行 gridView1.Appearance.FocusedRow.BackColor = Appearance1.FocusedRow.BackColor; //选中的行 gridView1.Appearance.FocusedCell.BackColor = Appearance1.FocusedRow.BackColor; //选中的单元格 gridView1.Appearance.FocusedRow.ForeColor = Appearance1.FocusedRow.ForeColor; //字体颜色 gridView1.Appearance.GroupPanel.BackColor = Appearance1.GroupPanel.BackColor; gridView1.Appearance.GroupPanel.BackColor2 = Appearance1.GroupPanel.BackColor2; //分组row颜色 gridView1.Appearance.GroupRow.BackColor = Appearance1.GroupRow.BackColor; // gridView1.Appearance.GroupRow.BackColor2 = Appearance1.GroupRow.BackColor2; //空白区域颜色 gridView1.Appearance.Empty.BackColor = Appearance1.Empty.BackColor; //列标题颜色 gridView1.Appearance.HeaderPanel.BackColor = Appearance1.HeaderPanel.BackColor; //设置分组 gridView1.GroupSummary.Add(DevExpress.Data.SummaryItemType.Count, "分组1"); //添加分组1,如果不是count,则名称必须与字段名对应 gridView1.GroupFormat = "{1} {2}"; //默认"{0}: [#image]{1} {2}"; 字段名称:数据 计数=? gridView1.Columns["部门名称"].GroupIndex = 0; //设置默认分组列 }
二、设置分组显示格式
gridView1.GroupFormat = "{0}: [#image]{1} {2}"; //"字段名:图标 字段值"; gridView1.GroupFormat = "{1} {2}"; //"字段值";
三、统计汇总字段
gridView1.GroupSummary.Add(DevExpress.Data.SummaryItemType.Count, "分组1"); //添加分组1,如果不是count,则名称必须与字段名对应 gridView1.GroupFormat = "{1} {2}"; //默认"{0}: [#image]{1} {2}"; 字段名称:数据 计数=? gridView1.Columns["FieldName1"].GroupIndex = 0; //设置默认分组列 //分组列格式 gridView1.GroupSummary.Add(DevExpress.Data.SummaryItemType.Average, "id", gridView1.Columns["id"]); gridView1.GroupSummary[1].DisplayFormat = "AVG={0:c}"; gridView1.GroupSummary.Add(DevExpress.Data.SummaryItemType.Count, "姓名", gridView1.Columns["姓名"]); ((DevExpress.XtraGrid.GridSummaryItem)gridView1.GroupSummary[gridView1.GroupSummary.Count - 1]).DisplayFormat = "小计:{0:N0}"; //展开 gridView1.ExpandAllGroups();
四、对列的操作
//设某一列只读或者不可编辑 gridView1.Columns[0].OptionsColumn.AllowEdit = false; gridView1.Columns[0].OptionsColumn.ReadOnly = false; //设某一列文字和标题局中显示 gridView1.Columns[0].AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; gridView1.Columns[0].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; //去掉某一列上面的自动筛选功能(Filter) gridView1.Columns[0].OptionsFilter.AllowAutoFilter = false; gridView1.Columns[0].OptionsFilter.AllowFilter = false; gridView1.Columns[0].OptionsFilter.ImmediateUpdateAutoFilter = false; //设置冻结列(左冻结) gridView1.Columns[0].Fixed= DevExpress.XtraGrid.Columns.FixedStyle.Left; //得到单元格数据(0行0列) string ss = gridView1.GetRowCellDisplayText(0, gridView1.Columns[0]); string ss = gridView1.GetRowCellValue(0, gridView1.Columns[0]); //设置单元格数据(将0行0列的单元格赋值123) gridView1.SetRowCellValue(0, gridView1.Columns[0], "123"); //手动添加dev的列 DevExpress.XtraGrid.Columns.GridColumn Col1 = new DevExpress.XtraGrid.Columns.GridColumn(); Col1.FieldName="FID"; Col1.Visible=true; Col1.VisibleIndex=gridView1.Columns.Count; gridView1.Columns.Add(Col1);
五、一些常用事件
//设置自动增加的行号,需要先添加给gridview添加事件CustomDrawRowIndicator private void gridview_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e) { if (e.Info.IsRowIndicator && e.RowHandle >= 0) e.Info.DisplayText = (e.RowHandle + 1).ToString(); } //删除: (修改了dgvdel里的datagridviewdel方法) public static void datagridviewdel_Dev(DevExpress.XtraGrid.Views.Grid.GridView Mydgv) { if (MessageBox.Show("你确定要删除选中的记录吗?", "删除提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2, 0, false) == DialogResult.Yes) { int iSelectRowCount = Mydgv.SelectedRowsCount; if (iSelectRowCount > 0) { Mydgv.DeleteSelectedRows(); } } } //新增: private void btn_add_Click(object sender, EventArgs e) { gridView1.AddNewRow(); } //新增行初始值设置: private void gridView1_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e) { ColumnView View = sender as ColumnView; View.SetRowCellValue(e.RowHandle, View.Columns[0], gridView1.GetRowCellValue(gridView1.GetRowHandle(gridView1.RowCount - 2), gridView1.Columns[0])); //复制最后一行的数据到新行 View.SetRowCellValue(e.RowHandle, View.Columns[1], gridView1.GetRowCellValue(gridView1.GetRowHandle(gridView1.RowCount - 2), gridView1.Columns[1])); //复制最后一行的数据到新行 } //保存 datagridviewsave
时间: 2024-10-28 10:57:37