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++
}
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')
}