将单击事件添加到数据表(powershell)
因此,我有一个按钮,拉动这个功能来搜索机器上的所有Install.Log文件。 加载结果后,我希望在a行上有一个双击事件,它将在其中打开日志文件 我很难添加一个click事件,每当我试图找到与Datatables相关的东西时,我都会找到关于java的东西。任何指导或链接将不胜感激 谢谢你 通过在PS ISE中运行此命令,自行测试代码将单击事件添加到数据表(powershell),powershell,datatables,Powershell,Datatables,因此,我有一个按钮,拉动这个功能来搜索机器上的所有Install.Log文件。 加载结果后,我希望在a行上有一个双击事件,它将在其中打开日志文件 我很难添加一个click事件,每当我试图找到与Datatables相关的东西时,我都会找到关于java的东西。任何指导或链接将不胜感激 谢谢你 通过在PS ISE中运行此命令,自行测试代码 $ComputerName=“此处为您的计算机名” [void][System.Reflection.Assembly]::LoadWithPartialName(
$ComputerName=“此处为您的计算机名”
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML=@'
'@
#读取XAML
$reader=(新对象System.Xml.XmlNodeReader$xaml)
请尝试{$Software=[Windows.Markup.XamlReader]::Load($reader)}
catch{Write Host“无法加载Windows.Markup.XamlReader。此问题的一些可能原因包括:.NET Framework缺少PowerShell,必须使用PowerShell-sta启动PowerShell,遇到无效的XAML代码。退出}
#在PowerShell中存储表单对象
$xaml.SelectNodes(“//*[@Name]”)ForEach对象{
设置变量-Name($\u.Name)-Value$Software.FindName($\u.Name)
写入主机$\名称
}
$Fields=@(
“姓名”
“上次写作时间”
)
#$Services=Get WmiObject-Computer($prebox.text+$device.text)-类Win32reg_AddRemovePrograms |选择对象-属性*
$Services=Get ChildItem\\$ComputerName\c$\build\logs-Include*install*-recurse-ErrorAction Stop | Sort Object LastWriteTime-Descending
#向数据表添加服务
$Datatable=新对象System.Data.Datatable
[void]$Datatable.Columns.AddRange($Fields)
foreach($服务中的服务)
{
$Array=@()
Foreach($字段中的字段)
{
$array+=$Service.$Field
}
[void]$Datatable.Rows.Add($array)
}
#$filter=“类似于“B%”的显示名称”
#$Datatable.DefaultView.RowFilter=$filter
#创建datagrid对象并填充datatable
$DataGrid1.ItemsSource=$Datatable.DefaultView
$DataGrid1.CanUserAddRows=$False
$DataGrid1.IsReadOnly=$True
$DataGrid1.GridLinesVisibility=“无”
$DataGrid1.Add_CellMouseClick({gridClick})
函数gridClick(){
$rowIndex=$DataGrid1.CurrentRow.Index
$columnIndex=$DataGrid1.CurrentCell.columnIndex
写入主机$rowIndex
写入主机$columnIndex
写入主机$DataGrid1.Rows[$rowIndex]。单元格[0]。值
写入主机$DataGrid1.Rows[$rowIndex].Cells[$columnIndex].value}
$FilterTextBox.Add\u TextChanged({
$InputText=$FilterTextBox.Text
$filter=“名称类似于“$InputText%””
$Datatable.DefaultView.RowFilter=$filter
$DataGrid1.ItemsSource=$Datatable.DefaultView
$form.Controls.Add($DataGrid1)
$Software.Controls.Add($DataGrid1)
})
#显示表单
$statusBar1.Text=“完成。”
$Software.Add_显示({$Software.Activate()})
$Software.ShowDialog()|输出为空
--我尝试过的建议
[
好的。下面是gridview单元格单击事件的示例。您可以使用
$DataGrid1添加单元格双击事件。添加单元格鼠标单击({gridClick})
希望这会有所帮助
$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()
根据您的要求,这里是我使用WPF和PowerShell创建的另一个示例。您可以使用
$WPFListView.Add_MouseDoubleClick({gridClick})
绑定事件,并使用列$WPFListView.SelectedValue.OriginalFileName
在PowerShell ISE中尝试此功能
##示例数据表
$tabName=“SampleTable”
#创建表对象
$table=新对象system.Data.DataTable“$tabName”
#定义列
$col1=新对象system.Data.DataColumn OriginalFileName,([string])
$col2=新对象system.Data.DataColumn文件描述,([string])
$col3=新对象system.Data.DataColumn FileVersionRaw,([string])
#添加列
$table.columns.add($col1)
$table.columns.add($col2)
$table.columns.add($col3)
#创建一行
$row=$table.NewRow()
$row.OriginalFileName=“测试日志”
$row.FileDescription=“测试日志数据”
$row.FileVersionRaw=“v1.0”
$row1=$table.NewRow()
$row1.OriginalFileName=“IIS日志”
$row1.FileDescription=“IIS系统日志”
$row1.FileVersionRaw=“v2.0”
$row2=$table.NewRow()
$row2.OriginalFileName=“用户数据”
$row2.FileDescription=“用户数据详细信息”
$row2.FileVersionRaw=“v1.0”
$row3=$table.NewRow()
$row3.OriginalFileName=“系统信息”
$row3.FileDescription=“系统信息详细信息”
$row3.FileVersionRaw=“v2.0”
#将行添加到表中
$table.Rows.Add($row)
$table.Rows.Add($row1)
$table.Rows.Add($row2)
$table.Rows.Add($row3)
##示例数据表
$inputXML=@”
"@
$inputXML=$inputXML-replace'mc:Ignorable=“d”、'-replace“x:N”、'N'-replace'^等等。我只需要在数据表中双击它,就像在我的文件资源管理器中一样,因为默认情况下,它们会使用我们的LogViewer程序打开。我将该函数添加到脚本中,但我看不到它在我的终端中添加了任何内容。添加了对OP的编辑。发布有问题的更新代码。尝试运行代码,因为它正在使用电源Shell想得到一个我在你的帖子上运行的想法。但是我会更新我的OP。你使用的是哪个powershell版本?版本:5.1.17763.771
$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()
##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;