DevExpress使用方法GridControl总结 (转)
DevExpress使用方法GridControl总结
1.隐藏最上面的GroupPanel
gridView1.OptionsView.ShowGroupPanel=false;
2.得到当前选定记录某字段的值
sValue=Table.Rows[gridView1.FocusedRowHandle][FieldName].ToString();
3.数据只读
gridView1.OptionsBehavior.Editable=false;
4.不显示MasterDetailView
gridView1.OptionsDetail.EnableMasterViewMode=false;
5.修改最上面的GroupPanel内容
gridView1.GroupPanelText="柳生飘絮";
6.数据绑定:
FieldName --数据库的字段名称
7.读写拷贝权限设置
ColumnView.Editable
This property returns a value of the ColumnViewOptionsBehavior.Editable option
不可写
ColumnViewOptionsBehavior.Editable
Gets or sets whether end users are allowed to invoke cell editors
可读可写
OptionsColumn.AllowEdit
Gets or sets whether end users are allowed to invoke editors for the column's cells.
可读可写
只有ColumnViewOptionsBehavior.Editable = True设置OptionsColumn.AllowEdit才有意义
OptionsColumn.ReadOnly
Gets or sets whether end-users are prevented from editing the column's cell values.
可读可写
If the ReadOnly property is set to true, the cell values of columns cannot be modified by end-users. In this case cell editors can be invoked but end users can only select and copy an editor's content. This option is not in effect if the column's AllowEdit or the view's ColumnView.Editable option is disabled.
Cell values can still be modified in code using the ColumnView.SetRowCellValue method regardless of the ReadOnly property's setting.只读不可拷贝:
ColumnViewOptionsBehavior.Editable = False
只读可拷贝:
ColumnViewOptionsBehavior.Editable = True
OptionsColumn.AllowEdit = True
OptionsColumn.ReadOnly = True
可编辑:
ColumnViewOptionsBehavior.Editable = True
OptionsColumn.AllowEdit = True
OptionsColumn.ReadOnly = False
出处:https://blog.csdn.net/liushengpiaoxu/article/details/3420996
=============================================================
一、如何解决单击记录整行选中的问题
View->OptionsBehavior->EditorShowMode 设置为:Click
二、如何新增一条记录
(1)、gridView.AddNewRow()
(2)、实现gridView_InitNewRow事件
三、如何解决GridControl记录能获取而没有显示出来的问题
gridView.populateColumns();
四、如何让行只能选择而不能编辑(或编辑某一单元格)
(1)、View->OptionsBehavior->EditorShowMode 设置为:Click
(2)、View->OptionsBehavior->Editable 设置为:false
五、如何禁用GridControl中单击列弹出右键菜单
设置Run Design->OptionsMenu->EnableColumnMenu 设置为:false
六、如何隐藏GridControl的GroupPanel表头
设置Run Design->OptionsView->ShowGroupPanel 设置为:false
七、如何禁用GridControl中列头的过滤器
过滤器如下图所示:
设置 Run Design->OptionsCustomization->AllowFilter 设置为:false
八、如何在查询得到0条记录时显示自定义的字符提示/显示
如图所示:
方法如下:
//When no Records Are Being Displayed private void gridView1_CustomDrawEmptyForeground(object sender, CustomDrawEventArgs e) { //方法一(此方法为GridView设置了数据源绑定时,可用) ColumnView columnView = sender as ColumnView; BindingSource bindingSource = this.gridView1.DataSource as BindingSource; if(bindingSource.Count == 0) { string str = "没有查询到你所想要的数据!"; Font f = new Font("宋体", 10, FontStyle.Bold); Rectangle r = new Rectangle(e.Bounds.Top + 5, e.Bounds.Left + 5, e.Bounds.Right - 5, e.Bounds.Height - 5); e.Graphics.DrawString(str, f, Brushes.Black, r); } //方法二(此方法为GridView没有设置数据源绑定时,使用,一般使用此种方法) if (this._flag) { if (this.gridView1.RowCount == 0) { string str = "没有查询到你所想要的数据!"; Font f = new Font("宋体", 10, FontStyle.Bold); Rectangle r = new Rectangle(e.Bounds.Left + 5, e.Bounds.Top + 5, e.Bounds.Width - 5, e.Bounds.Height - 5); e.Graphics.DrawString(str, f, Brushes.Black, r); } } }
九、如何显示水平滚动条?
设置this.gridView.OptionsView.ColumnAutoWidth = false;
十、如何定位到第一条数据/记录?
设置 this.gridView.MoveFirst()
十一、如何定位到下一条数据/记录?
设置 this.gridView.MoveNext()
十二、如何定位到最后一条数据/记录?
设置 this.gridView.MoveLast()
十三、设置成一次选择一行,并且不能被编辑
this.gridView1.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus; this.gridView1.OptionsBehavior.Editable = false; this.gridView1.OptionsSelection.EnableAppearanceFocusedCell = false;
十四、如何显示行号?
this.gridView1.IndicatorWidth = 40; //显示行的序号 private void gridView1_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e) { if (e.Info.IsRowIndicator && e.RowHandle>=0) { e.Info.DisplayText = (e.RowHandle + 1).ToString(); } }
十五、如何让各列头禁止移动?
设置gridView1.OptionsCustomization.AllowColumnMoving = false;
十六、如何让各列头禁止排序?
设置gridView1.OptionsCustomization.AllowSort = false;
十七、如何禁止各列头改变列宽?
设置gridView1.OptionsCustomization.AllowColumnResizing = false;
出处:https://www.cnblogs.com/starksoft/p/4936517.html
=======================================================================================
devexpress表格控件gridcontrol设置隔行变色、焦点行颜色、设置(改变)显示值、固定列不移动(附源码)
介绍一些常用的gridcontrol设置。
1、设置隔行变色。首先设置显示隔行变色,步骤:OptionsView-->EnableAppearanceEvenRow-->true和OptionsView-->EnableAppearanceOddRow-->true;然后设置奇数行和偶数行样式颜色等:Appearance-->EvenRow和Appearance-->OddRow。设计完成后,设计器出现隔行变色效果,如图:
2、设置奇偶行样式时,会看到其他行样式。Appearance-->FoucsedRow就是焦点行颜色,设置后可突出显示焦点行样式,如图所示:
3、设置显示值,有个两种方法。第一种可以在如图所示地方设置,列显示出来就会加上单位元;用的最多的一般是设置日期样式yyyy年MM月dd日:
第二种可使用代码改变,代码如下:
/// <summary> /// 改变显示值 /// </summary> private void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e) { if (e.Column.FieldName == "A3") { if (e.Value.ToString() == "1") e.DisplayText = "男"; else e.DisplayText = "女"; } }
4、有时表格列太多,需要拖动查看,但是又希望某些列能始终固定不移动。这个时候就可以设置该列为固定列了,属性如图所示:
5、运行效果图:
附源码:https://files.cnblogs.com/files/starksoft/demo006.rar
附加内容:根据条件改变行的样式(字体颜色、背景颜色、渐变色)
private void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e) { int hand = e.RowHandle;//行号 if(hand<0) { return; } DataRow dr = gridView1.GetDataRow(hand); if (dr == null) return; //int selectedHandle = gridView1.GetSelectedRows()[hand]; if (gridView1.GetRowCellValue(hand, "CODE").ToString().Contains("5")) { e.Appearance.ForeColor = Color.Red;//字体颜色 e.Appearance.BackColor=Color.Linen;//行背景颜色 //e.Appearance.BackColor2 = Color.Blue;//渐变颜色 } }
出处:https://www.cnblogs.com/starksoft/p/4936207.html
GridControl_gridView 单元格内容换行
通过正常的添加列,或者下面的方式添加列:
DevExpress.XtraGrid.Columns.GridColumn gcContent = gridView1.Columns.AddField("Content");
或者
gridView1.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] {
new DevExpress.XtraGrid.Columns.GridColumn { Visible=true,Caption="通知公告", FieldName="Content"},
new DevExpress.XtraGrid.Columns.GridColumn { Visible=true,Caption="时间", FieldName="CreateTime", MaxWidth=100, MinWidth=90},
new DevExpress.XtraGrid.Columns.GridColumn { Visible=true,Caption="发布人", FieldName="Creator", MaxWidth=120, MinWidth=90}
});
再通过设置列的ColumnEidt属性,使其换行。
RepositoryItemMemoEdit repoMemo = new RepositoryItemMemoEdit();
repoMemo.WordWrap = true;
repoMemo.AutoHeight = true;
this.gridControl1.RepositoryItems.Add(repoMemo);
this.gridControl1.gridView1.Columns[0].ColumnEdit=repoMemo
this.gridControl1.gridView1.OptionsView.RowAutoHeight = true;
我自己写了个变体,其实效果是一样的
gridControl1.RepositoryItems.Add(new RepositoryItemMemoEdit { Name= "Content", WordWrap = true,AutoHeight=true });
gridView1.Columns[0].ColumnEdit = gridControl1.RepositoryItems["Content"];
gridView1.OptionsView.RowAutoHeight = true;
参考出处:https://blog.csdn.net/ljunqiang/article/details/40346171
=======================================================================
表头字体颜色
for (int i = 0; i < this.gridView1.Columns.Count; i++)
{
this.gridView1.Columns[i].AppearanceHeader.ForeColor = Color.Red;
}
表头背景颜色
for (int i = 0; i < this.gridView1.Columns.Count; i++)
{
this.gridView1.Columns[i].AppearanceHeader.BackColor = Color.Red;
this.gridView1.Columns[i].AppearanceHeader.BackColor2 = Color.Red;
}
在form1构造方法中加入 gridView1.CustomDrawColumnHeader+= OnCustomDrawColumnHeader; 添加方法代码如下
private void OnCustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e)
{
//背景颜色没有设置且为空,则默认
if (e.Column == null || (e.Column.AppearanceHeader.BackColor == Color.Empty && !e.Column.AppearanceHeader.Options.UseBackColor))
return;
Rectangle rect = e.Bounds;
rect.Inflate(-1, -1);
// 填充表头背景颜色
e.Graphics.FillRectangle(new SolidBrush(e.Column.AppearanceHeader.BackColor), rect);
// 填充表头字体颜色
e.Appearance.DrawString(e.Cache, e.Info.Caption, e.Info.CaptionRect);
e.Handled = true;
}
字体颜色
form1构造函数中加入:this.gridView1.RowCellStyle += new DevExpress.XtraGrid.Views.Grid.RowCellStyleEventHandler(this.gridView1_RowCellStyle); 添加方法代码如下
private void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
{
e.Appearance.ForeColor = Color.Red;
}
//获取焦点(只有获取焦点后才会改变颜色)
this.gridControl1.Focus();
选中行颜色
this.gridView1.Appearance.FocusedRow.BackColor = Color.Red;
this.gridView1.Appearance.FocusedRow.BackColor2 = Color.Red;
表体颜色
1、首先开启gridControl 奇偶行外观
this.gridView1.OptionsView.EnableAppearanceEvenRow = true;
this.gridView1.OptionsView.EnableAppearanceOddRow = true;
2、 再改变表体颜色
this.gridView1.Appearance.OddRow.BackColor = Color.Red;
this.gridView1.Appearance.OddRow.BackColor2 = Color.Red;
this.gridView1.Appearance.EvenRow.BackColor = Color.Red;
this.gridView1.Appearance.EvenRow.BackColor2 = Color.Red;
表格线颜色
this.gridView1.Appearance.HorzLine.BackColor= Color.Red;
this.gridView1.Appearance.HorzLine.BackColor2 = Color.Red;
this.gridView1.Appearance.VertLine.BackColor = Color.Red;
this.gridView1.Appearance.VertLine.BackColor2 = Color.Red;
选中单元格颜色
this.gridView1.Appearance.FocusedCell.BackColor = Color.Red;
this.gridView1.Appearance.FocusedCell.BackColor2 = Color.Red;
空白区域颜色
gridView1.Appearance.Empty.BackColor = Color.Red;
出处:https://blog.csdn.net/weixin_42609389/article/details/82109148
========================================================
设置gridControl的gridView的每行的字体和大小
表头:gridView1.Appearance.HeaderPanel.Font = new Font("Tahoma", 14);
表体:gridView1.Appearance.Row.Font = new Font("Tahoma", 14);
单元格都靠左或者右边显示
run 进去后选择需要左或右显示的列。找到列属性中的AppearanceCell下的TextOptions下的HAlignment 选择 Far 靠左 center 中间 defalut 靠右
DevExpress GridControl 选择整行被选单元格不变色的设置
设置GridControl 里面的 gridview 属性, 找到OptionSelection 将EnableAppearanceFocusedCell 属性设置False 就可以了
================================================================================================
使用Devpress LayoutControl控件后,运行软件时,右击控件的时候有菜单customize layout 弹出来,我想问下怎么把它屏蔽了?
答:layoutControl1.AllowCustomization = false;
出处:https://bbs.csdn.net/topics/390692246
================================================================================================