Help with Datagridview event

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 1 year and 4 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

Help with Datagridview event

Post by supportMIB »

I have a form that has a datagridview on it.

The columns are prebuilt and column 'isApproved' is a combo box with a drop down of 'approved' or 'denied'.

I'm creating a datatable from SQL and then row by row adding it to the datagridview so I can manipulate the combox (if the database has a 0 then it shows 'denied' if the database has a 1 then it shows 'approved'). The code is seen below:

Code: Select all

$userSystems = Invoke-Sqlcmd -ServerInstance SERVERNAME -Database iam -Query "select * from systems where userid = $userid"
	
	$UserPermissionTable = New-Object system.Data.DataTable 'UserDataTable2'
	
	$newcol = New-Object system.Data.DataColumn UserID, ([int]); $UserPermissionTable.columns.add($newcol)
	$newcol = New-Object system.Data.DataColumn System, ([string]); $UserPermissionTable.columns.add($newcol)
	$newcol = New-Object system.Data.DataColumn isApproved, ([string]); $UserPermissionTable.columns.add($newcol)
	$newcol = New-Object system.Data.DataColumn DateApproved, ([string]); $UserPermissionTable.columns.add($newcol)
	$newcol = New-Object system.Data.DataColumn TicketID, ([string]); $UserPermissionTable.columns.add($newcol)
	$newcol = New-Object system.Data.DataColumn ApprovedBy, ([string]); $UserPermissionTable.columns.add($newcol)
	$newcol = New-Object system.Data.DataColumn ApprovalDocumentLink, ([string]); $UserPermissionTable.columns.add($newcol)
	
	foreach ($system in $userSystems)
	{
		# Add a DataTable row
		$row = $UserPermissionTable.NewRow()
		$row.System = $system.system
		$row.isApproved = $system.isApproved
		$row.DateApproved = $system.DateApproved
		$row.TicketID = $system.TicketID
		$row.ApprovedBy = $system.ApprovedBy
		$row.ApprovalDocumentLink = $system.ApprovalDocumentLink
		$UserPermissionTable.Rows.Add($row)
	}
	
	
	$i = 0
	foreach ($row in $UserPermissionTable)
	{
		$datagridview1.Rows.Add()
		#populate combobox for approvals
		
		$datagridview1.Rows[$i].Cells[0].Value = ($Usertable | ? { $_.userid -eq $userid }).LastName
		$datagridview1.Rows[$i].Cells[1].Value = ($Usertable | ? { $_.userid -eq $userid }).FirstName
		$datagridview1.Rows[$i].Cells[2].Value = $row.System
		
		if ($row.isapproved -eq 0)
		{
			$datagridview1.Rows[$i].Cells[3].Value = 'Denied'
		}
		else
		{
		$datagridview1.Rows[$i].Cells[3].Value = 'Approved'
		}
		
		$datagridview1.Rows[$i].Cells[4].Value = $row.DateApproved
		$datagridview1.Rows[$i].Cells[5].Value = $row.TicketID
		$datagridview1.Rows[$i].Cells[6].Value = $row.ApprovedBy
		
		$i++
		}
That loads the data into the datagridview all well, but I want to add an event to trigger when the dropdown box in the combo box is changed, so I added a 'CellvalueChanged' event but it triggers when the datagridview is populated. I assume because it's changing the cells one at time.

How can I set it up so the datagridview is populated but only triggers when the combo box is toggled to a new option? I have also attached a screenshot of the form.

Below is the code for my events associated with the datagridview

Code: Select all

$datagridview1_CellValueChanged=[System.Windows.Forms.DataGridViewCellEventHandler]{
	
	$newval = $datagridview1.CurrentCell.EditedFormattedValue
	
	if ($newval -eq 'Approved')
	{
		[System.Windows.Forms.MessageBox]::show("Approved!!")
		#Update the SQL database and do other stuff
	}
	else
	{
		[System.Windows.Forms.MessageBox]::show("DENIED!!")
		#Update the SQL database and do other stuff
	}
	
}

$datagridview1_CurrentCellDirtyStateChanged={
	
	$datagridview1.CommitEdit('Commit')
}

Attachments
user form.png
user form.png (130.76 KiB) Viewed 1034 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Help with Datagridview event

Post by jvierra »

You have to filter for the column you want. Why do you need to trigger at all. When any changes happen, they are posted to the DataTable.

The Cell handler event args give you the cell object and the cell index.
User avatar
supportMIB
Posts: 62
Last visit: Thu Feb 29, 2024 11:17 am

Re: Help with Datagridview event

Post by supportMIB »

I wanted to add an event that when the drop box is changed to either approve or denied it would send an SQL update command to a table on a SQL server
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Help with Datagridview event

Post by jvierra »

Why is that a problem? My answer tells you all you need to know to get the new value. There is no event other than the one you are using.
This topic is 1 year and 4 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