Page 1 of 2

DataGridView Search Template - Search thrown exceptions

Posted: Sat Mar 09, 2013 2:53 pm
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:

Re: DataGridView Search Template - Search thrown exceptions

Posted: Mon Mar 11, 2013 9:26 am
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

Re: DataGridView Search Template - Search thrown exceptions

Posted: Mon Mar 11, 2013 9:49 am
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.

Re: DataGridView Search Template - Search thrown exceptions

Posted: Mon Mar 11, 2013 10:13 am
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

Re: DataGridView Search Template - Search thrown exceptions

Posted: Mon Mar 11, 2013 10:14 am
by davidc
FYI I moved the post to the GUI forum.

Re: DataGridView Search Template - Search thrown exceptions

Posted: Wed Mar 13, 2013 7:13 pm
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!

Re: DataGridView Search Template - Search thrown exceptions

Posted: Fri Mar 15, 2013 10:08 am
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

Re: DataGridView Search Template - Search thrown exceptions

Posted: Fri Mar 15, 2013 11:25 am
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.

Re: DataGridView Search Template - Search thrown exceptions

Posted: Fri Mar 15, 2013 12:24 pm
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

Re: DataGridView Search Template - Search thrown exceptions

Posted: Fri Mar 15, 2013 12:55 pm
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!