Ads 468x60px

Saturday, March 2, 2013

XAF : Set PredefinedValues from the code

  1. Create fake values for predefinedvalues using application model
  2. Create XAF view controller. i.e. PredefinedValuesController
  3. make it inherit from DevExpress.ExpressApp.ViewController(Of DetailView)
  4. override method Protected Overrides Sub OnActivated()
  5. Read PropertyEditor for required attribute.
    i.e. Dim editor As ASPxStringPropertyEditor = TryCast(View.FindItem(AttributeName), ASPxStringPropertyEditor)
  6. then, add the values to the editor
    - Dim control As ASPxComboBox = TryCast(editor, ASPxComboBox)
    -         If control IsNot Nothing Then
    -            AddHandler control.Load, Sub(s, e)
    -            control.Items.Clear()
    -            control.Items.Add("item 1")
    -            control.Items.Add("item 2")
    -       End Sub
    -       End If
Full Example:
- 'MobileDevice' is the name of buisness object
- GetPredefinedValues is an SqlServer Procedure that gets all distinct values for a business object attribute.
  in our example business object is 'MobileDevice' and the attribute is 'IMEI'
 
 

CREATE PROCEDURE [dbo].[GetPredefinedValues]
    -- Add the parameters for the stored procedure here
    @tbl nvarchar(200)
    , @col nvarchar(200)
AS
BEGIN
 
    declare @sql nvarchar(400)
    set @sql = 'SELECT DISTINCT '+@col+' FROM '+@tbl+' WHERE '+@col+' IS NOT NULL ORDER BY '+@col
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    print @sql
exec     sp_executesql @sql
    SET NOCOUNT ON;
 
    -- Insert statements for procedure here
END



   1:  Imports System
   2:  Imports System.ComponentModel
   3:  Imports System.Collections.Generic
   4:  Imports System.Diagnostics
   5:  Imports System.Text
   6:   
   7:  Imports DevExpress.ExpressApp
   8:  Imports DevExpress.ExpressApp.Actions
   9:  Imports DevExpress.Persistent.Base
  10:  Imports DevExpress.ExpressApp.Web.Editors.ASPx
  11:  Imports DevExpress.Web.ASPxEditors
  12:  Imports DevExpress.Persistent.BaseImpl
  13:  Imports DevExpress.Xpo
  14:  Imports DevExpress.Data.Filtering
  15:  Imports BillingSystem.Module.Devices
  16:   
  17:  Public Class PredefinedValuesController
  18:      Inherits DevExpress.ExpressApp.ViewController(Of DetailView)
  19:   
  20:      Public Sub New()
  21:          MyBase.New()
  22:   
  23:          'This call is required by the Component Designer.
  24:          InitializeComponent()
  25:          RegisterActions(components)
  26:   
  27:      End Sub
  28:   
  29:     
  30:   
  31:      Protected Overrides Sub OnActivated()
  32:          MyBase.OnActivated()
  33:          ' to add for more values we
  34:          If TypeOf View.CurrentObject Is MobileDevice Then
  35:              SetPreDefinedValues("MobileDevice", "IMEI")
  36:   
  37:          End If
  38:   
  39:      End Sub
  40:    
  41:      Private Sub SetPreDefinedValues(ByVal tableName As String, ByVal columnName As String)
  42:          Dim editor As ASPxStringPropertyEditor = TryCast(View.FindItem(columnName), ASPxStringPropertyEditor)
  43:          If editor IsNot Nothing AndAlso View.ViewEditMode = DevExpress.ExpressApp.Editors.ViewEditMode.Edit Then
  44:              If editor.Control IsNot Nothing Then
  45:                  Customize(editor.Editor, tableName, columnName)
  46:              Else
  47:                  AddHandler editor.ControlCreated, Sub(s, e) Customize(editor.Editor, tableName, columnName)
  48:              End If
  49:          End If
  50:      End Sub
  51:   
  52:      Private Sub Customize(ByVal p As Object, ByVal tableName As String, ByVal columnName As String)
  53:          Dim control As ASPxComboBox = TryCast(p, ASPxComboBox)
  54:          If control IsNot Nothing Then
  55:              AddHandler control.Load, Sub(s, e)
  56:                                           control.Items.Clear()
  57:                                           For Each item As String In GetPredefinedValuesFromTableColumn(tableName, columnName)
  58:                                               control.Items.Add(item)
  59:                                           Next
  60:                                       End Sub
  61:          End If
  62:      End Sub
  63:      Private Function GetPredefinedValuesFromTableColumn(ByVal tableName As String, ByVal columnName As String) As List(Of String)
  64:          Dim paramsOperand As New List(Of OperandValue)
  65:          paramsOperand.AddRange(New OperandValue() {tableName, columnName})
  66:          Dim session As Session = CType(SecuritySystem.CurrentUser, User).Session
  67:          Dim res As DB.SelectedData = session.ExecuteSproc("GetPredefinedValues", paramsOperand.ToArray)
  68:          Dim result As New List(Of String)
  69:          For Each row As DB.SelectStatementResultRow In res.ResultSet(0).Rows
  70:              result.Add(row.Values(0).ToString())
  71:          Next
  72:          Return result
  73:   
  74:      End Function
  75:   
  76:   
  77:  End Class