Monday 26 March 2018

Add multiple buttons in Datagrid


If you are reading this, that means you wanted to add a button inside a Datagrid in PowerShell. 
Let us suppose that you need to add one or multiple buttons inside a single column in a datagrid. Data will be loaded inside the gridview and at the same time: n-number of command buttons need to be added to each row. Something like this:



I already introduced an example in my previous post, but I will explain how it is done in this post.

·       XAML side

To achieve something like above, you need to include those three buttons inside a StackPanel with an horizontal Orientation. The template xaml looks like this:

<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Button x:Name="View"  Background="#FF44AFE3"  Content="view"
                    Height="28" Width="50" Cursor="Hand"  HorizontalAlignment="Center"
                    VerticalAlignment="Center" BorderThickness="0" Margin="1,0,1,0">
                </Button>
                <Button x:Name="Edit"  Background="#198C19"  Content="edit"
                    Height="28" Width="50" Cursor="Hand"  HorizontalAlignment="Center"
                    VerticalAlignment="Center" BorderThickness="0" Margin="1,0,1,0">
                </Button>
                <Button x:Name="Delete"  Background="#FFA500" Content="delete"
                    Height="28" Width="50" Cursor="Hand"  HorizontalAlignment="Center"
                    VerticalAlignment="Center"  BorderThickness="0" Margin="1,0,1,0">
                </Button>
            </StackPanel>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>


Also, add a Name to your datagrid with which you can access from the Powershell script. Something like this:


<DataGrid x:Name="gridLogs" …


·       PS1 script

The script will look like this:

[System.Windows.RoutedEventHandler]$EventonDataGrid = {

    # GET THE NAME OF EVENT SOURCE
    $button$_.OriginalSource.Name

    # THIS RETURN THE ROW DATA AVAILABLE
    # resultObj scope is the whole script
    $Script:resultObj = $datagridtest.CurrentItem

    # CHOOSE THE CORRESPONDING ACTION
    If ( $button -match "View" ){
        Write-Host "View row"
    }
    ElseIf ($button -match "Edit" ){   
        Write-Host "Edit row"
    }
    ElseIf ($button -match "Delete" ){
        Write-Host "Remove row"
    }

}
$datagridtest.AddHandler([System.Windows.Controls.Button]::ClickEvent, $EventonDataGrid)


First, we create something called RoutedeventHandler. As its name implies, it will handle the routed event: $EventOnDatagrid.

In addition, we register it to the datagrig to listen to every button click on it.


$datagridtest.AddHandler([System.Windows.Controls.Button]::ClickEvent, $EventonDataGrid)


How do we know which button is clicked?

The line below will get the current sender of the routedEvent, get the original source and then the name of the object (which can be “edit”, “delete, “view”” as we defined in the xaml for our buttons).

    
    # GET THE NAME OF EVENT SOURCE
    $button$_.OriginalSource.Name


The row is easy to find, we just have to get the current item in the datagrid because when we clicked on it, it is automatically considered as selected.  

    # THIS RETURN THE ROW DATA AVAILABLE
    $Script:resultObj = $datagridtest.CurrentItem


For the last part … well, I think there is nothing to be explained, just another big "if else" condition. J
    
    # CHOOSE THE CORRESPONDING ACTION
    If ( $button -match "View" ){
        Write-Host "View row"
    }
    ElseIf ($button -match "Edit" ){   
        Write-Host "Edit row"
    }
    ElseIf ($button -match "Delete" ){
        Write-Host "Remove row"
    }


And that’s it! Now you can add as much button as you want and manage easily the event on your datagrid.

Download Files
The source code is available here (Button style is bit different). 

Share:

0 commentaires:

Post a Comment

Popular Posts

Join us on Facebook

STATS

Contact Form

Name

Email *

Message *