events with datagridview and keyboard

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 7 years and 3 months 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
supportMIB
Posts: 62
Last visit: Thu Feb 29, 2024 11:17 am

events with datagridview and keyboard

Post by supportMIB »

Hello, I see there is a $datagridview1_cellcontentclick event, but I'm trying to use the keyboard to move around in the grid and update information based on that.

Is there a similar event for keyboard movement through rows/cells?

When a row is clicked, it updates several labels with information from the row. I would like to be able to move up and down with the keyboard and also have it populate these labels, not just using the mouse.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: events with datagridview and keyboard

Post by jvierra »

You would likely want to use "CellEnter" and "CellLeave" events.
User avatar
supportMIB
Posts: 62
Last visit: Thu Feb 29, 2024 11:17 am

Re: events with datagridview and keyboard

Post by supportMIB »

I tried using the _CellEnter but no luck.
I still have to use a mouseclick to pull to the labels

Code: Select all

$datagridview1_CellEnter = [System.Windows.Forms.DataGridViewCellEventHandler]{
	
	
	#id 
	$label1.Text = $datagridview1.rows[$datagridview1.CurrentCell.RowIndex].Cells[0].Value.ToString()
	#log type
	$label2.Text = $datagridview1.rows[$datagridview1.CurrentCell.RowIndex].Cells[11].Value.ToString()
	#source
	$label3.Text = $datagridview1.rows[$datagridview1.CurrentCell.RowIndex].Cells[9].Value.ToString()
	#eventID
	$label4.Text = $datagridview1.rows[$datagridview1.CurrentCell.RowIndex].Cells[1].Value.ToString()
	#Level
	$label5.Text = $datagridview1.rows[$datagridview1.CurrentCell.RowIndex].Cells[22].Value.ToString()
	
	#User
	if ($datagridview1.rows[$datagridview1.CurrentCell.RowIndex].Cells[15].Value.ToString())
	{
		
		if ((Get-ADUser -filter * | ? { $_.sid -eq ($datagridview1.rows[$datagridview1.CurrentCell.RowIndex].Cells[15].Value.ToString()) }).samaccountname)
		{
			$label6.Text = (Get-ADUser -filter * | ? { $_.sid -eq ($datagridview1.rows[$datagridview1.CurrentCell.RowIndex].Cells[15].Value.ToString()) }).samaccountname
		}
		elseif ($datagridview1.rows[$datagridview1.CurrentCell.RowIndex].Cells[15].Value.ToString() -eq "S-1-5-7")
		{
			$label6.Text = "Anonymous"
		}
		elseif ($datagridview1.rows[$datagridview1.CurrentCell.RowIndex].Cells[15].Value.ToString() -eq "S-1-5-18")
		{
			$label6.Text = "Local System"
		}
		elseif ($datagridview1.rows[$datagridview1.CurrentCell.RowIndex].Cells[15].Value.ToString() -eq "S-1-5-19")
		{
			$label6.Text = "Local Service"
		}
		elseif ($datagridview1.rows[$datagridview1.CurrentCell.RowIndex].Cells[15].Value.ToString() -eq "S-1-5-20")
		{
			$label6.Text = "Network Service"
		}
	}
	else
	{
		$label6.Text = "N/A"
	}
	
	#OpCode
	$label7.Text = $datagridview1.rows[$datagridview1.CurrentCell.RowIndex].Cells[23].Value.ToString()
	#Logged
	$label8.Text = $datagridview1.rows[$datagridview1.CurrentCell.RowIndex].Cells[16].Value.ToString()
	
	#task category
	If ($datagridview1.rows[$datagridview1.CurrentCell.RowIndex].Cells[24].Value.ToString())
	{
		$label9.text = $datagridview1.rows[$datagridview1.CurrentCell.RowIndex].Cells[24].Value.ToString()
	}
	else
	{
		$label9.Text = "None"
	}
	#Keywords
	$label10.Text = $datagridview1.rows[$datagridview1.CurrentCell.RowIndex].Cells[25].Value.ToString()
	#Computer
	$label11.Text = $datagridview1.rows[$datagridview1.CurrentCell.RowIndex].Cells[14].Value.ToString()
	
	$label2.Visible = $true
	$label3.Visible = $true
	$label4.Visible = $true
	$label5.Visible = $true
	$label6.Visible = $true
	$label7.Visible = $true
	$label8.Visible = $true
	$label9.Visible = $true
	$label10.Visible = $true
	$label11.Visible = $true
	
	
	
	$eid = ($datagridview1.rows[$datagridview1.CurrentCell.RowIndex].Cells[1].Value).trim()
	$source = ($datagridview1.rows[$datagridview1.CurrentCell.RowIndex].Cells[9].Value).trim()
	
	
	$linklabel1.Text = "http://www.eventid.net/display.asp?eventid=$eid&source=$source"
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: events with datagridview and keyboard

Post by jvierra »

Works fine for me:

Do this:
  1. $datagridview1_CellEnter=[System.Windows.Forms.DataGridViewCellEventHandler]{
  2. #Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
  3.     Write-Host 'Enter'
  4. }
  5.  
  6. $datagridview1_CellLeave=[System.Windows.Forms.DataGridViewCellEventHandler]{
  7. #Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
  8.     Write-Host 'Leave' 
  9. }
Be sure the events are tied to the grid event.
This topic is 7 years and 3 months 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