Filtering a DT for a Datagridview

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Post Reply
User avatar
supportMIB
Posts: 52
Joined: Thu Aug 25, 2016 1:24 pm

Filtering a DT for a Datagridview

Post by supportMIB » Tue Sep 17, 2019 7:41 am

I have a datatable that is populated from a text file on form load using the below code. It populates the last 1000 lines of a log file to a datagridview which works fine, but I'd like to have a checkbox which filters a single column but I'm unsure how to create a filter on the DT to achieve this.

Code: Select all

$form1_Load = {
	
	$dataGridView1.ColumnHeadersVisible = $true
	
	#TODO: Initialize Form Controls here
	$logs = Get-Content "logfile.log" -Tail 1000
	
	#Creates Datatable
	$logtable = "LogTable"
	
	#Create Table object
	$global:table = New-Object system.Data.DataTable "$logtable"
	
	#Define Columns
	$Datecol = New-Object system.Data.DataColumn Date, ([string])
	$TimeZonecol = New-Object system.Data.DataColumn TimeZone, ([string])
	$SourceIPcol = New-Object system.Data.DataColumn SourceIP, ([string])
	$StatusCol = New-Object system.Data.DataColumn Status, ([string])
	$PortCol = New-Object system.Data.DataColumn Port, ([string])
	$GetOrPostCol = New-Object system.Data.DataColumn GetOrPost, ([string])
	$WebsiteCol = New-Object system.Data.DataColumn Website, ([string])
	
	#Add the Columns
	$table.columns.add($Datecol)
	$table.columns.add($TimeZoneCol)
	$table.columns.add($SourceIPCol)
	$table.columns.add($StatusCol)
	$table.columns.add($PortCol)
	$table.columns.add($GetOrPostCol)
	$table.columns.add($WebsiteCol)

	
	foreach ($line in $logs)
	{
		
		#Create a row
		$row = $table.NewRow()
	
		$Date = ($line.split("") | ?{ $_ -ne "" -and $_ -ne "-" })[0]
		$TimeZone = ($line.split("") | ?{ $_ -ne "" -and $_ -ne "-" })[1]
		$SourceIP = ($line.split("") | ?{ $_ -ne "" -and $_ -ne "-" })[2]
		$Status = ($line.split("") | ?{ $_ -ne "" -and $_ -ne "-" })[3]
		$Port = ($line.split("") | ?{ $_ -ne "" -and $_ -ne "-" })[4]
		$GetOrPost = ($line.split("") | ?{ $_ -ne "" -and $_ -ne "-" })[5]
		$Website = ($line.split("") | ?{ $_ -ne "" -and $_ -ne "-" })[6]
		
		#Enter data in the row
		$row.$DateCol = $Date
		$row.$TimeZoneCol = $TimeZone
		$row.$SourceIPCol = $SourceIP
		$row.$StatusCol = $Status
		$row.$PortCol = $Port
		$row.$GetOrPostCol = $GetOrPost
		$row.$WebsiteCol = $Website
		
		#Add the row to the table
		$table.Rows.Add($row)
	}
	
	$datagridview1.DataSource = $table
	
}
I tried this to filter the DT where $_.status -like "*DENIED*" but it doesnt seem to change anything.

Code: Select all

$checkboxDenied_CheckedChanged={
	#TODO: Place custom script here
	if ($checkboxUniqueWebsites.Checked -eq $true)
	{
		$dv = New-Object System.Data.DataView($table)
		$DV.RowFilter = "Status LIKE '*DENIED*'"
		
		$datagridview1.DataSource = $dv
		}
	}
}
Any help would be appreciated.

User avatar
supportMIB
Posts: 52
Joined: Thu Aug 25, 2016 1:24 pm

Re: Filtering a DT for a Datagridview

Post by supportMIB » Tue Sep 17, 2019 9:08 am

The above code worked on another checkbox, so I assume I must have screwed the one I was testing on up.

Is there a way to pull a distinct value from a DT?

I have a column called 'website' that I would like to go through the DT and only pull unique values for

User avatar
jvierra
Posts: 13793
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Filtering a DT for a Datagridview

Post by jvierra » Tue Sep 17, 2019 11:08 am

To filter the grid must be sourced from a table then just set the default view on the table.

$datagridview.DataSource = $dt
$dt.DefaultView.RowFilter = "Status LIKE '*DENIED*'"


This will immediately filter the rows.

Post Reply