devexpress中最强大的控件之一就是Grid,不管你做什么软件,它都能实现想要的功能。今天这篇就是关于Grid控件的详细教程,力图打造全网最全的此控件教程。废话不多说,直接开始正题。友情提醒:内容较多,建议收藏
1、样式美化
1.1、设置列不可见
gridView.OptionsView.ShowColumnHeaders = false;
1.2、选中时整行全选中
另外,它还包含4个选项:Default、MouseDown、MouseUp、MouseDownFocused。有兴趣的可以分别试下效果
gridView.OptionsBehavior.EditorShowMode = EditorShowMode.Click;
1.3、让行只能选择而不能编辑
或者编辑某一单元格
a、View->OptionsBehavior->EditorShowMode 设置为:Click
b、View->OptionsBehavior->Editable 设置为:false
1.4、禁用单击列弹出右键菜单
设置Run Design->OptionsMenu->EnableColumnMenu 设置为:false
1.5、隐藏GroupPanel表头
设置Run Design->OptionsView->ShowGroupPanel 设置为:false
1.6、禁用列头的过滤器
设置Run Design->OptionsCustomization->AllowFilter 设置为:false
1.7、显示水平滚动条
gridView1.OptionsView.ColumnAutoWidth = false;
1.8、禁止各列移动
gridView1.OptionsCustomization.AllowColumnMoving = false;
1.9、禁止各列排序
gridView1.OptionsCustomization.AllowSort = false;
1.10、禁止各列头改变列宽
gridView1.OptionsCustomization.AllowColumnResizing = false;
1.11、获取选中单元格的值
object foValue = gridview.GetFocusedValue();
1.12、数据只读
gridView1.OptionsBehavior.Editable=false;
1.13、不显示MasterDetailView
gridView1.OptionsDetail.EnableMasterViewMode=false;
1.14、修改最上面的GroupPanel内容
gridView1.GroupPanelText="柳生飘絮";
1.15、读写拷贝权限设置
a、只读不可拷贝:
ColumnViewOptionsBehavior.Editable = False
b、只读可拷贝:
ColumnViewOptionsBehavior.Editable = True OptionsColumn.AllowEdit = True OptionsColumn.ReadOnly = True
c、可编辑:
ColumnViewOptionsBehavior.Editable = True OptionsColumn.AllowEdit = True OptionsColumn.ReadOnly = False
1.16、设列文字和标题居中
gridView1.Columns[0].AppearanceHeader.TextOptions.HAlignment =DevExpress.Utils.HorzAlignment.Center; gridView1.Columns[0].AppearanceCell.TextOptions.HAlignment =DevExpress.Utils.HorzAlignment.Center;
1.17、去掉列自动Filter
gridView1.Columns[0].OptionsFilter.AllowAutoFilter = false; gridView1.Columns[0].OptionsFilter.AllowFilter =false; gridView1.Columns[0].OptionsFilter.ImmediateUpdateAutoFilter =false;
1.18、设置冻结列(左冻结)
gridView1.Columns[0].Fixed= DevExpress.XtraGrid.Columns.FixedStyle.Left;
1.19、单元格百分数显示
gridViewChiSquare.Columns[2].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric; gridViewChiSquare.Columns[2].DisplayFormat.FormatString = "p2";
2、功能实现
2.1、定位到第一条
gridView1.MoveFirst()
2.2、定位到下一条
gridView1.MoveNext()
2.3、定位到最后一条
gridView.MoveLast()
2.4、获取单元格值(0行0列)
string ss = gridView1.GetRowCellValue(0, gridView1.Columns[0]); string ss=gridView1.GetRowCellDisplayText(0,gridView1.Columns[0]);
2.5、设置单元格值(0行0列置为123)
gridView1.SetRowCellValue(0, gridView1.Columns[0],"123");
2.6、手动添加dev的列
DevExpress.XtraGrid.Columns.GridColumn Col1=newDevExpress.XtraGrid.Columns.GridColumn (); Col1.FieldName="FID"; Col1.Visible=true; Col1.VisibleIndex=gridView1.Columns.Count; gridView1.Columns.Add(Col1);
2.7、设置行号
这里有个彩蛋,无论页面中有几个Grid,都可以绑定到同一个此事件
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(); }
2.8、新增数据
数据源最好设置成DataTable
private void btn_add_Click(object sender, EventArgs e) { gridView1.AddNewRow(); }
具体如果对于新加行还有什么特别的设置,可以在它gridView1_InitNewRow事件中填写:
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])); }
2.9、删除数据
也可以循环删除,注意自增量不是从0开始
intiSelectRowCount = gridView1.SelectedRowsCount; if(iSelectRowCount > 0) { gridView1.DeleteSelectedRows(); }
2.10、检查数据的有效性
a、先在ValidateRow事件中加入检查代码:
private void gridView1_ValidateRow(object sender, ValidateRowEventArgse) { GridView view = sender as GridView; view.ClearColumnErrors(); if (view.GetRowCellValue(e.RowHandle, "ReceiveDate") == DBNull.Value) { e.Valid = false; view.SetColumnError(view.Columns["ReceiveDate"], "必须指定日期"); } }
b、再调用UpdateCurrentRow()方法执行检查
2.11、设置行样式
private void gridView6_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e) { GridView View = sender as GridView; if (e.RowHandle >= 0) { object needAlert = View.GetRowCellValue(e.RowHandle, View.Columns["needAlert"]); if (needAlert != null & needAlert != DBNull.Value && needAlert.ToString().Trim() != "0" & View.GetRowCellValue(e.RowHandle, View.Columns["Value"]) != DBNull.Value) { decimal AverValue = Convert.ToDecimal(View.GetRowCellValue(e.RowHandle, View.Columns["Value"])); objectMinValue = View.GetRowCellValue(e.RowHandle, View.Columns["MinValue"]); objectMaxVlaue = View.GetRowCellValue(e.RowHandle, View.Columns["MaxValue"]); if (MinValue != DBNull.Value & MinValue != null & MaxVlaue.ToString() != "" & MaxVlaue != DBNull.Value && MaxVlaue != null & MaxVlaue.ToString() != "") { decimal gridColumn2 = Convert.ToDecimal(MinValue); decimal gridColumn1 = Convert.ToDecimal(MaxVlaue); if (gridColumn2 > AverValue || AverValue > gridColumn1) { e.Appearance.ForeColor = Color.Red; e.Appearance.BackColor = Color.LightGray; } } } } }
2.12、设置单元格样式
private void GridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e) { int rowIndex = e.RowHandle; if (rowIndex < 0) return; DevExpress.XtraGrid.Views.Grid.GridView gridViewTemp = (DevExpress.XtraGrid.Views.Grid.GridView)sender; string goodsStatus = FormUIHelper.GetGridCellValue(gridViewTemp, rowIndex, "GoodsStatus") as string; if (goodsStatus != MetaCodeEnum.GoodsStatus.Normal.ToString()) { e.Appearance.ForeColor = Color.Red; } }
2.13、某列上悬浮提示
private void bandedGridView2_MouseMove(object sender, MouseEventArgs e) { //记录鼠标所在单元格,用于ToolTip显示 BandedGridView gridView = (BandedGridView)sender; DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo hitInfo = gridView.CalcHitInfo(e.Location); if (hitInfo.InRowCell && hitInfo.Column == this.BandedGridView2.Columns["LastSuccessTime"]) { DataRow selectedDataRow = this.BandedGridView2.GetDataRow(hitInfo.RowHandle); if (selectedDataRow == null) return; string shopCode = selectedDataRow["ShopCode"] as string; string shopCodeText = selectedDataRow["ShopCodeText"] as string; List<string> listP = new List<string>() { "[" + shopCodeText + "]店铺同步时间" }; var model1 = this.listApiSyncService.FirstOrDefault(c => c.ShopCode == shopCode); if (model1 == null) return; string all = model1.Description; ToolTipControllerShowEventArgs aa = new ToolTipControllerShowEventArgs(); aa.ToolTip = all; this.toolTipController1.ShowHint(aa); } else { this.toolTipController1.HideHint(); } }
2.14、添加合计带
this.BandedGridView2.OptionsView.ShowFooter = true; this.BandedGridView2.Columns["ShopCodeText"].Summary.Add(DevExpress.Data.SummaryItemType.Custom, "ShopCodeText", "合计"); this.BandedGridView2.Columns["CurrentDaySaleCount"].Summary.Add(DevExpress.Data.SummaryItemType.Sum, "CurrentDaySaleCount");
2.15、设置分组效果
DevExpress.XtraGrid.Columns.GridColumn gc = this.BandedGridView2.Columns["SourceText"]; gc.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending; gc.GroupIndex = 0;
2.16、打印
简单的打印直接调用Print()方法即可;复杂点的,就要继承DevExpress.XtraReports.UI.XtraReport这个类,自己画界面
2.17、导出
内置的方法很多,可以导出至Excel、Html、Csv、Pdf、Txt等
文章评论