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.
User avatar
davidc
Posts: 5725
Joined: Thu Aug 18, 2011 4:56 am

Re: DataGridView Search Template - Search thrown exceptions

Post by davidc » Fri Mar 15, 2013 1:20 pm

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
Joined: Tue Oct 09, 2012 6:24 am

Re: DataGridView Search Template - Search thrown exceptions

Post by Srinath » Mon Mar 18, 2013 8:54 am

I tried what you have suggested, but the result is same.
Attachments
Untitled.png
DataGridView
Untitled.png (11.14 KiB) Viewed 4856 times

User avatar
davidc
Posts: 5725
Joined: Thu Aug 18, 2011 4:56 am

Re: DataGridView Search Template - Search thrown exceptions

Post by davidc » Mon Mar 18, 2013 9:59 am

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
Joined: Tue Oct 09, 2012 6:24 am

Re: DataGridView Search Template - Search thrown exceptions

Post by Srinath » Mon Mar 18, 2013 10:14 am

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: 5725
Joined: Thu Aug 18, 2011 4:56 am

Re: DataGridView Search Template - Search thrown exceptions

Post by davidc » Mon Mar 18, 2013 10:37 am

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
Joined: Tue Oct 09, 2012 6:24 am

Re: DataGridView Search Template - Search thrown exceptions

Post by Srinath » Mon Mar 18, 2013 11:05 am

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: 5725
Joined: Thu Aug 18, 2011 4:56 am

Re: DataGridView Search Template - Search thrown exceptions

Post by davidc » Mon Mar 18, 2013 11:43 am

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
Joined: Tue Oct 09, 2012 6:24 am

Re: DataGridView Search Template - Search thrown exceptions

Post by Srinath » Mon Mar 18, 2013 7:04 pm

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.

Locked