MSDN Magazine - October 2008 - (Page 26) the customer fields in the UI (see the example in Figure 5). Remember that the GetTableSchema stored procedure will work for any table contained in the database, so you could even abstract Figure 6 Set Variables and Properties for Dynamic Data Entry Imports Imports Imports System.Windows.Markup Imports System.Data.SqlClient Imports System.Data Partial Public Class Window2 'This is the metadata table we created in the DataSet Designer Private TableSchema As New TableSchemaDataSet.TableSchemaDataTable 'ADO.NET objects used to load and save the table we're editing Private TableDataAdapter As New SqlDataAdapter Private TableConnection As New _ SqlConnection(My.Settings.NorthwindConnectionString) Private Table As DataTable 'This is the key field used in searching for a row in this example Private PKField As TableSchemaDataSet.TableSchemaRow 'This property can be set before the Form.Show() to edit any table Private m_tableName As String = "Shippers" Public Property TableName() As String Get Return m_tableName End Get Set(ByVal value As String) m_tableName = value End Set End Property End Class this further. For instance, what if you just want one single Window that can edit any maintenance table in the database, no matter what the schema? Dynamic Data Entry The previous example is still tied to the object model of Customer to load and edit the data. Even though the UI is dynamic, the data entry is not. What you probably want is a more abstracted form so that when you modify the database schema of any of the maintenance tables, you don’t have to update your object model and recompile all the code. One way to achieve this is to load and edit a simple untyped DataTable at run time. This works well using XML literals again, this time to generate the SELECT and UPDATE statements for the SqlDataAdapter. One thing to note is that this technique relies on the database to enforce any validation rules. This is why I would only use this type of dynamic form to edit very simple tables (such as maintenance or lookup tables). However, I suggest you play with the WPF Binding.ValidationRules property to declaratively add validation rules to the controls. You can create a typed DataTable for the TableSchema metadata as well, which eliminates the need for LINQ to SQL classes in the project. To do this, just right-click on the project, add a new item, and select DataSet. Name it TableSchemaDataSet and drag the GetTableSchema stored procedure onto the design surface from Solutions for every world. Visual Modeling for Business, IT and Systems Enterprise Architect from Sparx Systems redefines visual modeling with a huge set of built-in tools, technologies and capabilities, coupled with a lightweight footprint and great agility. With deep support for UML 2.1 and its related standards, Enterprise Architect 7.1 is the ideal tool to analyze, design, build and manage your next software project. 26 msdn magazine Basic Instincts http://www.sparxsystems.com
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.