Page 2 of 2

Re: DataGridView Search Template - Search thrown exceptions

Posted: Fri Mar 15, 2013 1:20 pm
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
}

Re: DataGridView Search Template - Search thrown exceptions

Posted: Mon Mar 18, 2013 8:54 am
by Srinath
I tried what you have suggested, but the result is same.

Re: DataGridView Search Template - Search thrown exceptions

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

Re: DataGridView Search Template - Search thrown exceptions

Posted: Mon Mar 18, 2013 10:14 am
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()
}
}

Re: DataGridView Search Template - Search thrown exceptions

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

Re: DataGridView Search Template - Search thrown exceptions

Posted: Mon Mar 18, 2013 11:05 am
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    }

Re: DataGridView Search Template - Search thrown exceptions

Posted: Mon Mar 18, 2013 11:43 am
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")
	
}

Re: DataGridView Search Template - Search thrown exceptions

Posted: Mon Mar 18, 2013 7:04 pm
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.