MSDN Magazine - October 2008 - (Page 27) the Server Explorer. This automatically creates a typed DataTable for you with no fuss. Rename the DataTable to TableSchema and save it. Now you need to load an untyped DataTable into the dynamically generated UI to edit the data. You don’t want to make any assumptions about the schema of the table you’re editing except that there is a primary key of some sort. DataTables and DataSets work well with WPF, but there are a couple of things you need to set up manually since you’re loading this all at run time. A simple thing to do is to set up a public property called TableName on the form to hold the name of the table you want to edit. This can be set easily from calling code. Then create some private class-level variables to reference the ADO.NET objects you need. The XAML markup of the Window for this example is exactly the same as before. Figure 6 shows the class-level variables you need and the TableName property set to a default value of Shippers. In the Loaded event handler you can now load the metadata, create and load the XAML to display the UI just like before, and then Figure 7 Generate the Dynamic Data Entry UI Private Sub Window1_Loaded() Handles MyBase.Loaded Try 'Get the schema of the database table we want to edit Dim taSchema As New _ TableSchemaDataSetTableAdapters. TableSchemaTableAdapter taSchema.Fill(Me.TableSchema, Me.TableName) 'Create the DataTable that will hold the record we're editing Me.Table = New DataTable(Me.TableName) Me.Title = Me.TableName Me.LoadUI() Me.SetPrimaryKey() Me.SetUpdateCommand() Catch ex As Exception MsgBox(ex.ToString) Me.Close() End Try End Sub Private Sub LoadUI() _ Dim UI = _ <%= From column In Me.TableSchema _ Where column.IsPrimaryKey = 0 AndAlso _ column.DataType "timestamp" _ Select <Label Height="28" Name= HorizontalContentAlignment="Right"> : %> <%= From column In Me.TableSchema _ Dynamically generating a Ui isn’t unique to WPF. You can do this in Windows Forms, but it is an exercise in coding the layout by hand. set the UpdateCommand on the TableDataAdapter. Figure 7 shows the Loaded event handler and the code that generates the XAML. Now that you have the XAML UI generated, set the primary key field (which is a TableSchemaDataRow object) so that you can use it in your UPDATE statement as well as in the SELECT query when the user clicks the Find button on the form. Generally primary keys are surrogate keys (like auto-incrementing integers) and mean nothing to the user, so instead you may want to create another public property that captures the search field name. Assuming that every table has only one primary key field defined you can use the indexer 0 on the query, which returns the first of the sequence: Private Sub SetPrimaryKey() 'Grab the Primary Key column of the table we want to ' edit so we can use it in the search Me.PKField = (From column In Me.TableSchema _ Where column.IsPrimaryKey = 1)(0) End Sub To create the SELECT statement, use XML literals again, but this time without creating XML. Instead you can create a string by calling the XElement’s .Value property. You then can create Where column.IsPrimaryKey = 0 AndAlso _ column.DataType "timestamp" _ Select GetUIElement(column) %> Me.DynamicContent.Content = XamlReader.Load(UI.CreateReader()) End Sub Private Function GetUIElement(ByVal columnInfo As _ TableSchemaDataSet.TableSchemaRow) As XElement Select Case columnInfo.DataType.ToLower Case "datetime", "int", "smallint", "money" Return <TextBox Height="28" Name= Text= /> Case "bit" Return <CheckBox HorizontalContentAlignment="Left" Name= IsChecked= > Case "image" Return <Image Height="150" Width="150" Stretch="Fill" HorizontalAlignment="Left" Name= Source= /> Case Else Return <TextBox Height="28" Name= MaxLength= Text= /> End Select End Function msdnmagazine.com October 2008 27 http://www.msdnmagazine.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.