DataGrid in .Net 4 and CRUD operations

Aug 12, 2013 at 6:18 PM
Dear all,

I've seen multiple examples of using a ListView and ways of sorting the columns, but it seems that DataGrid is more appropriate for many applications on sets of data.

I've experimented with it and managed to make some progress and can successfully bind to data and display it - however I'm at a loss as to how to implement the appropriate infrastructure to update, insert or delete it.

Here is an example of what I have so far:
# Current aim:
#   Augment a DataGrid with callbacks that implement CRUD operations on the data source.
#   For the time being I am assuming that the data source is an array of hashes.
New-StackPanel -Orientation Vertical {

    New-DataGrid -Name DG -Columns {
        New-DataGridTextColumn -Binding {Binding 'ID' } -Header 'ID' 
        New-DataGridTextColumn -Binding {Binding 'Name' } -Header 'Name' 
    } -On_RowEditEnding {
        # Need to differentiate between adding a row and editing a row.  Editing a row seems fine, adding does not.
        $Status.Text = 'Row Edit ending'
        Write-Error $this.CurrentItem
        Write-Error $this.Count ## When does the number of rows increment?
        #$DG.ItemsSource.Add( $this.CurrentItem ) ## Hoping that $this in this context is the added row.
        Write-Error -Message "The message is $this.CurrentItem "  ## Trying to figure out what $this is in this context.  Expect something that returns the row details.
        #$Window.Close() ## If I don't add this, when an exception is thrown the window can end up in a zombie state when something bad happens.
    } -ItemsSource {
        New-Object System.Collections.ObjectModel.ObservableCollection[PSObject]
    } -On_Loaded {
        # Start by including some default data.
        $DG.ItemsSource.Add( ( New-Object PSObject -Property @{ 'ID' = 1; 'Name' = 'John' } ) )
        $DG.ItemsSource.Add( ( New-Object PSObject -Property @{ 'ID' = 2; 'Name' = 'Alice' } ) )
    New-TextBlock -Name Status -Text 'Status'
} -show
This works in the sense that it displays the data and the On_RowEditEnding event is captured and correctly firing. What I don't understand is how to modify it so that adding a row that doesn't exist works.

Currently I am getting the following type of errors:

At line:218 char:31
  • return Show-Window <<<< $Object
    The message is System.Windows.Controls.DataGrid Items.Count:3.CurrentItem
    New-StackPanel : Exception calling "ShowDialog" with "0" argument(s): "Two-way binding requires Path or XPath."
    At C:\Users\egrace\Documents\Datagrids.ps1:13 char:15
  • New-StackPanel <<<< -Orientation Vertical {
    • CategoryInfo : NotSpecified: (:) [New-StackPanel], MethodInvocationException
    • FullyQualifiedErrorId : EmbeddedProcessRecordError,AutoGenerateCmdlets1770357848.NewStackPanelCommand
I have found other examples where this issue has cropped up - but I don't understand the principles so cannot make sense of it in this context.


The following Google Search terms return just two references (excluding this one) to this problem:

showui showdialog "two-way binding requires path or xpath"

Any ideas? For now I just want to do "CRUD" on the array of hashes, and worry about how to bind to actual DBs later!

P.S. This is a great project - I'd love to see some worked examples with a business logic layer and data access layer with a simple CRUD framework. This would be a massive benefit to many people.