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
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 »

I was referring to your modified script. I think I found the problem. When you set CurrentCell it changes the selection. In your version of the function you can comment out the CurrentCell portion and make sure you start the search from the last item in the selection:
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[$datagridview1.SelectedCells.Count - 1];
        $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
}
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 »

I tried what you have suggested, but the result is same.
Attachments
DataGridView
DataGridView
Untitled.png (11.14 KiB) Viewed 8045 times
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 try this. I modified the original function:
PowerShell Code
Double-click the code block to select all.
function SearchGrid()
{
	$RowIndex = 0
	$ColumnIndex = 0
	$seachString = $textboxSeach.Text
	
	if($seachString -eq "")
	{
		return
	}
	
	if($datagridviewResults.CurrentCell -ne $null)
	{
		$RowIndex = $datagridviewResults.CurrentCell.RowIndex
		$ColumnIndex = $datagridviewResults.CurrentCell.ColumnIndex + 1
	}
	
	$columnCount = $datagridviewResults.ColumnCount
	$rowCount = $datagridviewResults.RowCount
	for(;$RowIndex -lt $rowCount; $RowIndex++)
	{
		$Row = $datagridviewResults.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)
			{
				$datagridviewResults.CurrentCell = $cell
				return
			}
		}
		
		$ColumnIndex = 0
	}
	
	$datagridviewResults.CurrentCell = $null
	[void][System.Windows.Forms.MessageBox]::Show("The search has reached the end of the grid.","String not Found")
	
}
Now if you don't want to search the same row use the suggestion I mentioned before:
PowerShell Code
Double-click the code block to select all.
if($datagridview1.SelectedCells.Count -ne 0)    {        $startCell = $datagridview1.SelectedCells[$datagridview1.SelectedCells.Count - 1];        $RowIndex = $startCell.RowIndex        $ColumnIndex = $startCell.ColumnIndex + 1    }
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 »

David,

It shows the same result. I've the following bit of code Selection Changed event to show a message box whenever the selection change occurs. Basically it's not triggering the Selection Changed event.
PowerShell Code
Double-click the code block to select all.
$datagridview1_SelectionChanged={
	if ($datagridview1.CurrentRow.Selected -eq $true) {
		# $contextmenustrip1.Enabled = $true
		$Script:CurrentSAM = $datagridview1.CurrentRow.Cells["SamAccountName"].Value.ToString()
}
}
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 selection will not change if the next found cell is in the same row and you have whole row selection enabled. In other words, the selection will only change if the selected cell is in a different row.

If you don’t want to search for multiple matches within the same row, then you will have to start the search using the last cell of the current selection.

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:The selection will not change if the next found cell is in the same row and you have whole row selection enabled. In other words, the selection will only change if the selected cell is in a different row.
I now understand.
davidc wrote:If you don’t want to search for multiple matches within the same row, then you will have to start the search using the last cell of the current selection.
To do that where do I need to insert the code?
PowerShell Code
Double-click the code block to select all.
if($datagridview1.SelectedCells.Count -ne 0)    {        $startCell = $datagridview1.SelectedCells[$datagridview1.SelectedCells.Count - 1];        $RowIndex = $startCell.RowIndex        $ColumnIndex = $startCell.ColumnIndex + 1    }
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 »

PowerShell Code
Double-click the code block to select all.
function SearchGrid()
{
	$RowIndex = 0
	$ColumnIndex = 0
	$seachString = $textboxSeach.Text
	
	if($seachString -eq "")
	{
		return
	}
	
#	if($datagridviewResults.CurrentCell -ne $null)
#	{
#		$RowIndex = $datagridviewResults.CurrentCell.RowIndex
#		$ColumnIndex = $datagridviewResults.CurrentCell.ColumnIndex + 1
#	}
	
	if($datagridviewResults.SelectedCells.Count -ne 0)    
	{        
		$startCell = $datagridviewResults.SelectedCells[$datagridviewResults.SelectedCells.Count - 1];        
		$RowIndex = $startCell.RowIndex        
		$ColumnIndex = $startCell.ColumnIndex + 1    
	}
	
	$columnCount = $datagridviewResults.ColumnCount
	$rowCount = $datagridviewResults.RowCount
	for(;$RowIndex -lt $rowCount; $RowIndex++)
	{
		$Row = $datagridviewResults.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)
			{
				$datagridviewResults.CurrentCell = $cell
				return
			}
		}
		
		$ColumnIndex = 0
	}
	
	$datagridviewResults.CurrentCell = $null
	[void][System.Windows.Forms.MessageBox]::Show("The search has reached the end of the grid.","String not Found")
	
}
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 »

I finally resolved the issue by moving my code snippet from SelectionChanged event to CurrentCellChanged event.

From MSDN documentation:When you change the value of this property, the SelectionChanged event occurs before the CurrentCellChanged event. Any SelectionChanged event handler accessing the CurrentCell property at this time will get its previous value.
PowerShell Code
Double-click the code block to select all.
if($cell.Value -ne $null -and $cell.Value.ToString().IndexOf($seachString, [StringComparison]::OrdinalIgnoreCase) -ne -1)
            {
				$datagridview1.FirstDisplayedScrollingRowIndex = $Rowindex
				$datagridview1.Refresh()
            	$datagridview1.CurrentCell = $cell
				$datagridview1.Rows[$Rowindex].Selected = $true
                return
            }
Thanks for your extended help and updated function though.
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