Datagridview - Go to row with letter on keydown

Ask your PowerShell-related questions, including questions on cmdlet development!
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 6 years and 1 month 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
Renehansen
Posts: 16
Last visit: Fri Apr 05, 2019 3:19 am

Datagridview - Go to row with letter on keydown

Post by Renehansen »

Studio 2016, 64bit, windows 7 64bit

Hi all.

I'm looking for a more elegant solution to jump to a row when a letter is typed on they keyboard.
My current solution "works" in the sense that typing B will select the first row where column Displayname starts with B.
But ideally i'd like it to accept letters in quick succession like typing "re" and then having it selecting "Rene Hansen" for example.

any ideas on improving it?

Code: Select all

$datagridview1_KeyDown=[System.Windows.Forms.KeyEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.KeyEventArgs]
	#TODO: Place custom script here
	#Add-Type -AssemblyName "System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
	
	for ($i = 0; $i -lt $datagridview1.RowCount; $i++)
	{
		if ($datagridview1.Rows[$i].Cells['DisplayName'].Value -like "$($_.KeyCode.ToString())*")
		{
			
			$datagridview1.CurrentCell = $datagridview1.Rows[$i].Cells['DisplayName']
			$datagridview1.FirstDisplayedScrollingRowIndex = $i
			$i = 999999
		}
		
	}
}

cody m

Re: Datagridview - Go to row with letter on keydown

Post by cody m »

[TOPIC MOVED BY MODERATOR]
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Datagridview - Go to row with letter on keydown

Post by jvierra »

Gather the searchable items into an autocomplete textbox and let the user select from the dynamically filtered list.

You can also use a DataTable source and use the row filter to force display of all matching values on each keystroke. The grid will display only all matching rows to the filter expression.
This topic is 6 years and 1 month 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