Back to Devexpress

PersistentObject.Id Property

corelibraries-devexpress-dot-xtrascheduler-dot-persistentobject-74ed5c10.md

latest12.7 KB
Original Source

PersistentObject.Id Property

Gets a persistent object identifier previously retrieved from an external database or set at runtime.

Namespace : DevExpress.XtraScheduler

Assembly : DevExpress.XtraScheduler.v25.2.Core.dll

NuGet Package : DevExpress.Scheduler.Core

Declaration

csharp
public virtual object Id { get; }
vb
Public Overridable ReadOnly Property Id As Object

Property Value

TypeDescription
Object

An object that is the identifier.

|

Remarks

Appointment ID in WinForms Scheduler

For the WinForms Scheduler, the appointment identifier is not mapped, by default. In this situation, the Id property will always return null. This does not indicate any problem because an appointment object uses internal methods to retrieve its unique identifier from the data source. However, to get the correct value of the Id property, you can map the appointment identifier field using the AppointmentMappingInfo.AppointmentId property or Appointment Mappings Wizard. Usually, you map an appointment id to meet the requirements of the Gantt View, so the Wizard has a “Gantt view mappings” check box. You have to select it to specify this mapping.

When appointment ID mapping is specified, the scheduler will try to save the appointment identifier back to the data store. You can change this behavior using the AppointmentStorage.CommitIdToDataSource property. If an appointment identifier is generated by the data source, set the CommitIdToDataSource property to false.

If the appointment identifier is provided by the data source (e.g., an autoincremented field), modify the application code as described below:

This example demonstrates the WinForms Scheduler control bound to a Microsoft SQL CE database.

csharp
using DevExpress.XtraScheduler;
using System;
using System.Data;
using System.Data.SqlServerCe;
using System.Windows.Forms;

namespace Scheduler_SQLCE_Example {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
            this.schedulerStorage1.Appointments.ResourceSharing = true;

            //this.schedulerStorage1.Appointments.CommitIdToDataSource = false;
            this.schedulerStorage1.AppointmentsInserted += schedulerStorage1_AppointmentsInserted;
            appointmentsTableAdapter.Adapter.RowUpdated += new SqlCeRowUpdatedEventHandler(appointmentsTableAdapter_RowUpdated);
        }
        private void appointmentsTableAdapter_RowUpdated(object sender, SqlCeRowUpdatedEventArgs e) {
            // Gets ID for a new appointment from the SQL CE data source
            if (e.Status == UpdateStatus.Continue && e.StatementType == StatementType.Insert) {
                int id = 0;
                using (SqlCeCommand cmd = new SqlCeCommand("SELECT @@IDENTITY",
                    appointmentsTableAdapter.Connection)) {
                    id = Convert.ToInt32(cmd.ExecuteScalar());
                }
                e.Row["UniqueID"] = id;
            }
        }

        private void Form1_Load(object sender, EventArgs e) {
            // TODO: This line of code loads data into the 'dXDBDataSet2.Resources' table. You can move, or remove it, as needed.
            this.resourcesTableAdapter.Fill(this.dXDBDataSet2.Resources);
            // TODO: This line of code loads data into the 'dXDBDataSet2.Appointments' table. You can move, or remove it, as needed.
            this.appointmentsTableAdapter.Fill(this.dXDBDataSet2.Appointments);
        }

        private void schedulerStorage1_AppointmentChanging(object sender, DevExpress.XtraScheduler.PersistentObjectCancelEventArgs e) {
            string apt_id = string.Empty;
            if (((Appointment)e.Object).Id == null)
                apt_id = "null";
            else
                apt_id = ((Appointment)e.Object).Id.ToString();
            MessageBox.Show("AppointmentChanging event for " + apt_id);
        }

        private void OnApptChangedInsertedDeleted(object sender, PersistentObjectsEventArgs e) {
            UpdateSource();
        }

        void schedulerStorage1_AppointmentsInserted(object sender, PersistentObjectsEventArgs e) {
            UpdateSource();
            // Synchronize appointment IDs in the storage with the SQL CE data source
            AppointmentBaseCollection apts = e.Objects as AppointmentBaseCollection;
            foreach (Appointment apt in apts)
            {
                DataRowView dataRow = apt.GetSourceObject(this.schedulerStorage1) as DataRowView;
                this.schedulerStorage1.SetAppointmentId(apt, dataRow.Row["UniqueID"]);
            }
        }

        void UpdateSource() {
            appointmentsTableAdapter.Update(dXDBDataSet2);
            dXDBDataSet2.AcceptChanges();
        }

    }
}
vb
Imports Microsoft.VisualBasic
Imports DevExpress.XtraScheduler
Imports System
Imports System.Data
Imports System.Data.SqlServerCe
Imports System.Windows.Forms

Namespace Scheduler_SQLCE_Example
    Partial Public Class Form1
        Inherits Form
        Public Sub New()
            InitializeComponent()
            Me.schedulerStorage1.Appointments.ResourceSharing = True
            Me.schedulerStorage1.Appointments.CommitIdToDataSource = False
            AddHandler Me.schedulerStorage1.AppointmentsInserted, AddressOf schedulerStorage1_AppointmentsInserted
            AddHandler appointmentsTableAdapter.Adapter.RowUpdated, AddressOf appointmentsTableAdapter_RowUpdated
        End Sub
        Private Sub appointmentsTableAdapter_RowUpdated(ByVal sender As Object, ByVal e As SqlCeRowUpdatedEventArgs)
            ' Gets ID for a new appointment from the SQL CE data source
            If e.Status = UpdateStatus.Continue AndAlso e.StatementType = StatementType.Insert Then
                Dim id As Integer = 0
                Using cmd As New SqlCeCommand("SELECT @@IDENTITY", appointmentsTableAdapter.Connection)
                    id = Convert.ToInt32(cmd.ExecuteScalar())
                End Using
                e.Row("UniqueID") = id
            End If
        End Sub

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            ' TODO: This line of code loads data into the 'dXDBDataSet2.Resources' table. You can move, or remove it, as needed.
            Me.resourcesTableAdapter.Fill(Me.dXDBDataSet2.Resources)
            ' TODO: This line of code loads data into the 'dXDBDataSet2.Appointments' table. You can move, or remove it, as needed.
            Me.appointmentsTableAdapter.Fill(Me.dXDBDataSet2.Appointments)
        End Sub

        Private Sub schedulerStorage1_AppointmentChanging(ByVal sender As Object, ByVal e As DevExpress.XtraScheduler.PersistentObjectCancelEventArgs) Handles schedulerStorage1.AppointmentChanging
            Dim apt_id As String = String.Empty
            If (CType(e.Object, Appointment)).Id Is Nothing Then
                apt_id = "null"
            Else
                apt_id = (CType(e.Object, Appointment)).Id.ToString()
            End If
            MessageBox.Show("AppointmentChanging event for " & apt_id)
        End Sub

        Private Sub OnApptChangedInsertedDeleted(ByVal sender As Object, ByVal e As PersistentObjectsEventArgs) Handles schedulerStorage1.AppointmentsChanged, schedulerStorage1.AppointmentsDeleted
            UpdateSource()
        End Sub

        Private Sub schedulerStorage1_AppointmentsInserted(ByVal sender As Object, ByVal e As PersistentObjectsEventArgs)
            UpdateSource()
            ' Synchronize appointment IDs in the storage with the SQL CE data source
            Dim apts As AppointmentBaseCollection = TryCast(e.Objects, AppointmentBaseCollection)
            For Each apt As Appointment In apts
                Dim dataRow As DataRowView = TryCast(apt.GetSourceObject(Me.schedulerStorage1), DataRowView)
                Me.schedulerStorage1.SetAppointmentId(apt, dataRow.Row("UniqueID"))
            Next apt
        End Sub

        Private Sub UpdateSource()
            appointmentsTableAdapter.Update(dXDBDataSet2)
            dXDBDataSet2.AcceptChanges()
        End Sub
    End Class
End Namespace

If an appointment identifier is generated by the application , modify the application code as described below:

Appointment ID in ASPxScheduler

For the ASP.NET Scheduler, the appointment identifier should be mapped to the corresponding identity field of the data source using the using the ASPxAppointmentMappingInfo.AppointmentId property. To set the identifier for the newly created appointment, you can use the ASPxSchedulerStorage.SetAppointmentId method and technique described in the How to: Bind an ASPxScheduler to MS SQL Server Database (Step-by-Step Guide) document.

However, you can use the ASPxAppointmentStorage.AutoRetrieveId property instead of manually retrieving and setting the appointment identifier. Set the AutoRetrieveId value to true and all the necessary operations will be accomplished automatically, as long as the bound data source is one of the following controls: the SqlDataSource, the AccessDataSource or the ObjectDataSource.

The Appointment Mappings Wizard allows you to select the check box labeled “Retrieve and update ID automatically”. This action sets the AutoRetrieveId property at design time.

Implements

Id

See Also

PersistentObject Class

PersistentObject Members

DevExpress.XtraScheduler Namespace