Visual Studio V2019
Net.Framework V4.6.1
创建一个WinForm应用程序
Step 1:修改Program.cs
using DevExpress.Xpo;
using DevExpress.Xpo.DB;
static class Program
{
//public const string ConnectionString = @"XpoProvider=MSSqlServer;data source=(local);integrated security=SSPI;initial catalog=XpoTutorial1";
public const string ConnectionString = @"XpoProvider=MSAccess;Provider=Microsoft.Jet.OLEDB.4.0;Mode=Share Deny None;data source=E:\DEV_TEST\Report_Test\Report_Test\test.MDB;user id=;password=;";
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
XpoDefault.DataLayer = XpoDefault.GetDataLayer(ConnectionString, AutoCreateOption.DatabaseAndSchema);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new XtraForm1());
}
}
Step 2:创建Code First:
public class Customer : XPObject
{
public Customer() : base()
{
// This constructor is used when an object is loaded from a persistent storage.
// Do not place any code here.
}
public Customer(Session session) : base(session)
{
// This constructor is used when an object is loaded from a persistent storage.
// Do not place any code here.
}
string fName;
public string Name
{
get { return fName; }
set { SetPropertyValue<string>("Name", ref fName, value); }
}
ushort fAge;
public ushort Age
{
get { return fAge; }
set { SetPropertyValue<ushort>("Age", ref fAge, value); }
}
public override void AfterConstruction()
{
base.AfterConstruction();
// Place here your initialization code.
}
}
Step 3:添加XtraForm1
拖控件:
拖GridControl
设置XPCollection属性
设置GridControl属性:
代码:
public partial class XtraForm1 : DevExpress.XtraEditors.XtraForm
{
public XtraForm1()
{
InitializeComponent();
}
private void XtraForm1_Load(object sender, EventArgs e)
{
if (xpCollection1.Count == 0)
{
var customer1 = new Customer(session1);
customer1.Name = "John";
customer1.Age = 21;
customer1.Save();
xpCollection1.Add(customer1);
var customer2 = new Customer(session1);
customer2.Name = "Bob";
customer2.Age = 37;
customer2.Save();
xpCollection1.Add(customer2);
}
}
效果:
使用代码设置属性:
XPCollection xpCollectionPerson = new XPCollection(typeof(Person));
xpCollectionPerson.DisplayableProperties = "Name;Group.GroupName";
gridControl1.DataSource = xpCollectionPerson;
gridControl1.UseEmbeddedNavigator = true;
// Create an XPClassInfo object corresponding to the Person_Contact class.
XPClassInfo classInfo = XpoDefault.Session.GetClassInfo(typeof(Person_Contact));
// Create a filter that selects records which contain names starting with 'A'
// in the LastName column
CriteriaOperator criteria = CriteriaOperator.Parse("[LastName] LIKE ?", "A%");
//Create a data source.
XPServerCollectionSource serverModeDS = new XPServerCollectionSource(
XpoDefault.Session, classInfo, criteria);