DevExpress中GridView的⽇期编辑器
RepositoryItemDateEd。。。
如果你使⽤的DevExpress套件、Vs的版本较低(像我的是Vs2010+DevExpress13.1),可能会出现:在你为GridView的⽇期字段绑定RepositoryItemDateEdit型编辑器,当这个⽇期字段只需要精确到⽉份,此时,你当然希望RepositoryItemDateEdit编辑器在⽤户使⽤时,弹出PopupMenu时,显⽰的⽉份的选择界⾯,如:
但是,当我们设置好如下代码:
RepositoryItemDateEdit nRide = new RepositoryItemDateEdit();
nRide.VistaCalendarViewStyle = DevExpress.XtraEditors.VistaCalendarViewStyle.YearView;
string sYm= "yyyy年MM⽉";
nRide.DisplayFormat.FormatString = sYm;
nRide.EditMask = sYm;
gridView1.Columns[0].ColumnEdit = nRide;
可能会显⽰出这个样⼦:
也就是它还是精确到⽇,⽽不是⽉份。实际上,DevExpress官⽅已经注意到,这是个bug,那么,如果我们不想升级DevExpress 套件的版本,如何解决这个问题呢?
实际上,我们可以继承RepositoryItemDateEdit,并做⼀部分的改写,就可以做到,定义两个类,⼀个继承于RepositoryItemDateEdit,我们称之为gRide,代码如下:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualBasic;
using System.ComponentModel;
using DevExpress.XtraEditors.Drawing;
using DevExpress.XtraEditors.Registrator;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.ViewInfo;
namespace CustomEditor
{
[System.ComponentModel.DesignerCategory("")]
[UserRepositoryItem("Register")]
public class gRide : RepositoryItemDateEdit
{
static gRide()
{
Register();
}
public gRide()
: base()
{
}
internal const string EditorName = "CustomDateEdit";
public static void Register()
{
EditorRegistrationInfo.Default.Editors.Add(new EditorClassInfo(EditorName, typeof(gDateEdit), typeof(gRide), typeof(DateEditViewInfo), new ButtonEditPa        }
public override string EditorTypeName
{
get
{
return EditorName;
}
}
public override void Assign(RepositoryItem item)
{
this.VistaCalendarViewStyle = (item as RepositoryItemDateEdit).VistaCalendarViewStyle;
base.Assign(item);
}
}
}
然后,再定义⼀个类,叫gDateEdit,继承于DateEdit,⽤来设置RepositoryItemDateEdit或gRide的编辑器,代码如下:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraEditors.Repository;
using DevExpress.Utils.Drawing;
gridview不显示using DevExpress.XtraEditors.ViewInfo;
using DevExpress.XtraEditors.Drawing;
using DevExpress.XtraEditors.Calendar;
using DevExpress.Skins;
namespace CustomEditor
{
[ToolboxItem(true)]
[System.ComponentModel.DesignerCategory("")]
public class gDateEdit : DateEdit
{
static gDateEdit()
{
gRide.Register();
}
public gDateEdit()
:
base()
{
}
public override string EditorTypeName
{
get
{
return gRide.EditorName;
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]        public new gRide Properties
{
get
{
return base.Properties as gRide;
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
}
public override void ShowPopup()
{
base.ShowPopup();
}
protected override void DoShowPopup()
{
base.DoShowPopup();
}
}
}
这样在使⽤时,按如下代码即可。
List<MyClass> list = new List<MyClass>();
list.Add(new MyClass() { Time = DateTime.Today });
gridControl1.DataSource = list;
gRide nRide = new gRide(); //此处要注意,不要使⽤RepositoryItemDateEdit,⽽应使⽤改写后的gRide nRide.VistaCalendarViewStyle = DevExpress.XtraEditors.VistaCalendarViewStyle.YearView;
string sYm= "yyyy年MM⽉";
nRide.DisplayFormat.FormatString = sYm;
nRide.EditMask = sYm;
gridView1.Columns[0].ColumnEdit = nRide;
其中的gridControl1及gridView1,请⾃⼰从⼯具箱中添加到窗体中哦,MyClass类的定义如下:
public class MyClass
{
private DateTime privateTime;
public DateTime Time
{
get
{
return privateTime;
}
set
{
privateTime = value;
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。