首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将单击事件添加到DataTable (powershell)

将单击事件添加到DataTable (powershell)
EN

Stack Overflow用户
提问于 2020-01-15 04:54:55
回答 2查看 646关注 0票数 1

所以我有一个按钮,可以拉出这个函数来搜索机器上的所有Install.Log文件。加载结果后,我希望在a行上有一个双击事件,它将在其中打开日志文件。

我很难添加一个单击事件,每当我尝试查找与Datatables相关的内容时,我都会找到有关java的内容。任何指导或链接将不胜感激。

提前感谢

通过在PS ISE中运行以下代码来测试您自己的代码

代码语言:javascript
复制
$ComputerName = "your computer name here"
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML = @'
<Window Name="Form"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Title="Install Logs" Height="488.773" Width="797.65" Icon = "\\bconac01\ds-support\GS_IT\Tools\Test Tools (Alx)\Tool\icon.ico" ShowInTaskbar="False"> 
    <Grid Margin="0,0,-8,-21">
        <DataGrid Name="DataGrid1" HorizontalAlignment="Left" Height="368" VerticalAlignment="Top" Width="772" Margin="10,41,0,0"/>
        <Label Content="Filter" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
        <TextBox Name="FilterTextBox" HorizontalAlignment="Left" Height="26" Margin="78,10,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="172"/>
    </Grid>
</Window>
'@

#Read XAML
$reader=(New-Object System.Xml.XmlNodeReader $xaml) 
try{$Software=[Windows.Markup.XamlReader]::Load( $reader )}
catch{Write-Host "Unable to load Windows.Markup.XamlReader. Some possible causes for this problem include: .NET Framework is missing PowerShell must be launched with PowerShell -sta, invalid XAML code was encountered."; exit}

# Store Form Objects In PowerShell
$xaml.SelectNodes("//*[@Name]") | ForEach-Object{
    Set-Variable -Name ($_.Name) -Value $Software.FindName($_.Name)
    Write-host $_.Name
}

$Fields = @(
    'Name'
    'LastWriteTime'
)



#$Services = Get-WmiObject -Computer ($prebox.text + $device.text) -Class Win32reg_AddRemovePrograms | Select-object -Property *
$Services = Get-ChildItem \\$ComputerName\c$\build\logs -Include *install* -recurse -ErrorAction Stop | Sort-Object LastWriteTime -Descending


# Add Services to a datatable
$Datatable = New-Object System.Data.DataTable
[void]$Datatable.Columns.AddRange($Fields)
foreach ($Service in $Services)
{
    $Array = @()
    Foreach ($Field in $Fields)
    {
        $array += $Service.$Field
    }
    [void]$Datatable.Rows.Add($array)
}
#$filter = "DisplayName LIKE 'B%'"
#$Datatable.DefaultView.RowFilter = $filter



# Create a datagrid object and populate with datatable
$DataGrid1.ItemsSource = $Datatable.DefaultView
$DataGrid1.CanUserAddRows = $False
$DataGrid1.IsReadOnly = $True
$DataGrid1.GridLinesVisibility = "None"
$DataGrid1.Add_CellMouseClick({gridClick})

function gridClick(){
$rowIndex = $DataGrid1.CurrentRow.Index
$columnIndex = $DataGrid1.CurrentCell.ColumnIndex
Write-Host $rowIndex
Write-Host $columnIndex 
Write-Host $DataGrid1.Rows[$rowIndex].Cells[0].value
Write-Host $DataGrid1.Rows[$rowIndex].Cells[$columnIndex].value}

$FilterTextBox.Add_TextChanged({
    $InputText = $FilterTextBox.Text
    $filter = "Name LIKE '$InputText%'"
    $Datatable.DefaultView.RowFilter = $filter
    $DataGrid1.ItemsSource = $Datatable.DefaultView
    $form.Controls.Add($DataGrid1)
$Software.Controls.Add($DataGrid1)
})

# Shows the form
$statusBar1.Text = "Done."

$Software.Add_Shown({$Software.Activate()})
$Software.ShowDialog() | out-null

--我尝试过的东西都是建议的。

[

EN

回答 2

Stack Overflow用户

发布于 2020-01-15 06:02:42

好的。下面是一个带有网格视图单元格单击事件的示例。您可以使用$DataGrid1.Add_CellMouseClick({gridClick})添加单元格双击事件

希望这能有所帮助

代码语言:javascript
复制
$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(900,600)
$DataGrid1 = New-Object System.Windows.Forms.DataGridView
$DataGrid1.Size=New-Object System.Drawing.Size(800,400)

$DataGrid1.Add_CellMouseClick({gridClick})

$form.Controls.Add($DataGrid1)


#Create an unbound DataGridView by declaring a column count.
$DataGrid1.ColumnCount = 4
$DataGrid1.ColumnHeadersVisible = $true

#Set the column header names.
$DataGrid1.Columns[0].Name = "Recipe"
$DataGrid1.Columns[1].Name = "Category"
$DataGrid1.Columns[2].Name = "Third COlumn"
$DataGrid1.Columns[3].Name = "Rating"

#Populate the rows.
$row1 = @("Meatloaf","Main Dish", "boringMeatloaf", "boringMeatloafRanking")
$row2 = @("Key Lime Pie","Dessert", "lime juice evaporated milk", "****")
$row3 = @("Orange-Salsa Pork Chops","Main Dish", "pork chops, salsa, orange juice", "****")
$row4 = @("Black Bean and Rice Salad","Salad", "black beans, brown rice", "****")
$row5 = @("Chocolate Cheesecake","Dessert", "cream cheese", "***")
$row6 = @("Black Bean Dip", "Appetizer","black beans, sour cream", "***")
$rows = @( $row1, $row2, $row3, $row4, $row5, $row6 )

foreach ($row in $rows){
    $DataGrid1.Rows.Add($row)
}


function gridClick(){
$rowIndex = $DataGrid1.CurrentRow.Index
$columnIndex = $DataGrid1.CurrentCell.ColumnIndex
Write-Host $rowIndex
Write-Host $columnIndex 
Write-Host $DataGrid1.Rows[$rowIndex].Cells[0].value
Write-Host $DataGrid1.Rows[$rowIndex].Cells[$columnIndex].value}


$form.ShowDialog()
票数 1
EN

Stack Overflow用户

发布于 2020-01-17 05:01:21

根据你的要求,这是我用PowerShell用WPF创建的另一个示例。您可以使用$WPFListView.Add_MouseDoubleClick({gridClick})绑定事件,并使用$WPFListView.SelectedValue.OriginalFileName之类的列访问选定的单元格值

按照在PowerShell ISE中的方式进行尝试。This is how it looks

代码语言:javascript
复制
##Sample DataTable 

$tabName = "SampleTable"

#Create Table object
$table = New-Object system.Data.DataTable “$tabName”

#Define Columns
$col1 = New-Object system.Data.DataColumn OriginalFileName,([string])
$col2 = New-Object system.Data.DataColumn FileDescription,([string])
$col3 = New-Object system.Data.DataColumn FileVersionRaw,([string])

#Add the Columns
$table.columns.add($col1)
$table.columns.add($col2)
$table.columns.add($col3)

#Create a row
$row = $table.NewRow()
$row.OriginalFileName = "Test Log"
$row.FileDescription = "Test log data"
$row.FileVersionRaw = "v1.0"

$row1 = $table.NewRow()
$row1.OriginalFileName = "IIS Log"
$row1.FileDescription = "IIS Sys log"
$row1.FileVersionRaw = "v2.0"

$row2 = $table.NewRow()
$row2.OriginalFileName = "User Data"
$row2.FileDescription = "User data details"
$row2.FileVersionRaw = "v1.0"

$row3 = $table.NewRow()
$row3.OriginalFileName = "Sys Info"
$row3.FileDescription = "System Info Details"
$row3.FileVersionRaw = "v2.0"

#Add the row to the table
$table.Rows.Add($row)
$table.Rows.Add($row1)
$table.Rows.Add($row2)
$table.Rows.Add($row3)

##Sample DataTable 

$inputXML = @"
<Window x:Class="FileVersionChecker.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:FileVersionChecker"
        mc:Ignorable="d"
        Title="FileVersionChecker" Height="350" Width="525">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="115*"/>
                <ColumnDefinition Width="373*"/>
                <ColumnDefinition Width="29*"/>
            </Grid.ColumnDefinitions>
        <ListView Name="ListView" Grid.Column="1" HorizontalAlignment="Left" Height="150" Margin="10,10,0,0" VerticalAlignment="Top" Width="350">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="OriginalFileName" DisplayMemberBinding ="{Binding 'OriginalFileName'}" Width="100"/>
                    <GridViewColumn Header="FileDescription" DisplayMemberBinding ="{Binding 'FileDescription'}" Width="100"/>
                    <GridViewColumn Header="FileVersionRaw" DisplayMemberBinding ="{Binding 'FileVersionRaw'}" Width="100"/>
                </GridView>
            </ListView.View>
        </ListView>

    </Grid>
</Window>
"@       

$inputXML = $inputXML -replace 'mc:Ignorable="d"', '' -replace "x:N", 'N' -replace '^<Win.*', '<Window'

[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML = $inputXML
#Read XAML

$reader = (New-Object System.Xml.XmlNodeReader $xaml)
try {
    $Form = [Windows.Markup.XamlReader]::Load( $reader )
}
catch {
    Write-Output "Unable to load Windows.Markup.XamlReader. Double-check syntax and ensure .net is installed."
}
 $xaml.SelectNodes("//*[@Name]") | ForEach-Object {Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name)}


$WPFListView.ItemsSource  = $table.DefaultView
$WPFListView.Add_MouseDoubleClick({gridClick})

function gridClick()
{
  Write-Host ""  
  Write-Host "$($WPFListView.SelectedValue.OriginalFileName) ,  $($WPFListView.SelectedValue.FileDescription), $($WPFListView.SelectedValue.FileVersionRaw)"
}


$Form.ShowDialog() | out-null;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59741657

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档