Filtering a DT for a Datagridview

Ask your 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.
This topic is 4 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
supportMIB
Posts: 62
Last visit: Thu Feb 29, 2024 11:17 am

Filtering a DT for a Datagridview

Post by supportMIB »

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: 62
Last visit: Thu Feb 29, 2024 11:17 am

Re: Filtering a DT for a Datagridview

Post by supportMIB »

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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Filtering a DT for a Datagridview

Post by jvierra »

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.
This topic is 4 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked