DataGridView Search Template - Search thrown exceptions

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
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 11 years and 1 week 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
Srinath
Posts: 111
Last visit: Tue Feb 09, 2016 12:12 pm

DataGridView Search Template - Search thrown exceptions

Post by Srinath »

Hello,

While I'm trying to perform Search with the Default DataGridView Search Template, it throwing several same exceptions. The error message is given below:

The search works, but when it goes to end of the grid, it should show the default message. Instead it continues to search and throwing exceptions.

Any further help would be appreciated. Thank you.

Code Snippet from SearchGrid() function:
PowerShell Code
Double-click the code block to select all.
if($cell.Value.ToString().IndexOf($seachString, [StringComparison]::OrdinalIgnoreCase) -ne -1)
{
$datagridviewDisplayADUserInfo.CurrentCell = $cell
return
}
Error Message:
PowerShell Code
Double-click the code block to select all.
ERROR: You cannot call a method on a null-valued expression.
Display-ADUserInfo.pff (220): ERROR: At Line: 220 char: 7
ERROR: +                 if($cell.Value.ToString().IndexOf($seachString, [StringComparison]::OrdinalI ...
ERROR: +    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR:     + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
ERROR:     + FullyQualifiedErrorId : InvokeMethodOnNull
ERROR:
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: DataGridView Search Template - Search thrown exceptions

Post by davidc »

Add a null check at the beginning of the if statement and it will resolve the issue:

if($cell.Value -ne $null -and $cell.Value.ToString().IndexOf($seachString, [StringComparison]::OrdinalIgnoreCase) -ne -1)

I will update the template.

David
David
SAPIEN Technologies, Inc.
User avatar
Srinath
Posts: 111
Last visit: Tue Feb 09, 2016 12:12 pm

Re: DataGridView Search Template - Search thrown exceptions

Post by Srinath »

It works. The search behavior is different if I set the grid SelectionMode to FullRowSelect. i.e., it just selected the first result and didn't perform subsequent searches. Once I change the grid SelectionMode to CellSelect, it works.

Is there any simple fix for this? Thanks for your prompt response though.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: DataGridView Search Template - Search thrown exceptions

Post by davidc »

The search function depends on the selection to determine the current cell. If the full row is selected, it will not be able to determine the current cell and thus get stuck on the same row if multiple cells in the row are a match.

David
David
SAPIEN Technologies, Inc.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: DataGridView Search Template - Search thrown exceptions

Post by davidc »

FYI I moved the post to the GUI forum.
David
SAPIEN Technologies, Inc.
User avatar
Srinath
Posts: 111
Last visit: Tue Feb 09, 2016 12:12 pm

Re: DataGridView Search Template - Search thrown exceptions

Post by Srinath »

davidc wrote:The search function depends on the selection to determine the current cell. If the full row is selected, it will not be able to determine the current cell and thus get stuck on the same row if multiple cells in the row are a match.

David
I programmatically loaded the search textbox with specified row cells so that textbox will pre-populate keywords when searching.

However I'm having two issues:
1.) When search triggers, it highlighted a first matched row, but it's not actually selecting it. So I need to select another row and then again select the previous row.
2.) Search only occurs in one direction (Top to bottom), Is there anyway to make it Bottom to Top also?

The is the slightly modified code snippet of Search function:
PowerShell Code
Double-click the code block to select all.
function SearchGrid()
{
	$RowIndex = 0
	$ColumnIndex = 0
	$seachString = $textboxSearch.Text
	
	if($seachString -eq "")
	{
		return
	}
	
	if($datagridview1.SelectedCells.Count -ne 0)
	{
		$startCell = $datagridview1.SelectedCells[0];
		$RowIndex = $startCell.RowIndex
		$ColumnIndex = $startCell.ColumnIndex + 1
	}
	
	$columnCount = $datagridview1.ColumnCount
	$rowCount = $datagridview1.RowCount
	for(;$RowIndex -lt $rowCount; $RowIndex++)
	{
		$Row = $datagridview1.Rows[$RowIndex]
		
		for(;$ColumnIndex -lt $columnCount; $ColumnIndex++)
		{
			$cell = $Row.Cells[$ColumnIndex]
			
			if($cell.Value -ne $null -and $cell.Value.ToString().IndexOf($seachString, [StringComparison]::OrdinalIgnoreCase) -ne -1)
			{
				$datagridview1.Rows[$RowIndex].Selected = $true
				$datagridview1.CurrentCell = $datagridview1.CurrentRow.Cells[0]
				return
			}
		}
		
		$ColumnIndex = 0
	}
	$datagridview1.Rows[0].Selected = $true
	$datagridview1.CurrentCell = $datagridview1.Rows[0].Cells[0]
	return
}
Any further suggestions would be greatly appreciated. Thank you!
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: DataGridView Search Template - Search thrown exceptions

Post by davidc »

1. Do you want to add another row to the selection?

2. You can always modify these functions :) If you want reverse search then add a if statement and change the for loop to go in the opposite direction. Instead of incrementing you are decrementing.

David
David
SAPIEN Technologies, Inc.
User avatar
Srinath
Posts: 111
Last visit: Tue Feb 09, 2016 12:12 pm

Re: DataGridView Search Template - Search thrown exceptions

Post by Srinath »

davidc wrote:1. Do you want to add another row to the selection?

2. You can always modify these functions :) If you want reverse search then add a if statement and change the for loop to go in the opposite direction. Instead of incrementing you are decrementing.

David
1.) No. When DataGridView is set to FullRowSelect mode and when I perform search, it highlights the row but not selecting the row.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: DataGridView Search Template - Search thrown exceptions

Post by davidc »

Ok and the modified script is selecting the row. I will look into updating the Search function to include a direction for a future build.

David
David
SAPIEN Technologies, Inc.
User avatar
Srinath
Posts: 111
Last visit: Tue Feb 09, 2016 12:12 pm

Re: DataGridView Search Template - Search thrown exceptions

Post by Srinath »

davidc wrote:Ok and the modified script is selecting the row.
David
I'm struggling on figuring out the solution for this. If you already do so, can you please post it here? Thanks!
This topic is 11 years and 1 week 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