Select specified Column Cell Value in Datagridview

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
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 5 years and 9 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
SKunzmann
Posts: 4
Last visit: Mon Sep 10, 2018 12:47 am

Select specified Column Cell Value in Datagridview

Post by SKunzmann »

Hello, everybody,

I'm still quite new to Powershell and am trying to create a GUI to disable AD users. What I've done so far:

From an input field user names are written into a Datagridview and in the second column it is shown whether the user exists and if so whether he is activated or deactivated.

Image

As a next step I would like to read out the user names of the column "Username" by clicking on a button in order to process them further. And this is exactly where I'm getting stuck. I have read some instructions to similar things but I lack the knowledge to derive from them at my request. Always only how to process selected cells, rows or columns. I don't want to select anything manually but automatically check and process all users in the Username column.

Thanks in advance
Stephen
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Select specified Column Cell Value in Datagridview

Post by jvierra »

Enumerate the "Rows" of the grid and reference the column you need. You can reference the column by name.
User avatar
SKunzmann
Posts: 4
Last visit: Mon Sep 10, 2018 12:47 am

Re: Select specified Column Cell Value in Datagridview

Post by SKunzmann »

Yeah, I found that too, but I just can't figure out how to do it. Maybe I'm just dumb. Do I have to start with foreach?

Code: Select all

Foreach (Cell.Value in Datagridview1.Column[Username]) {
if ($Userstatus -eq $true) {
Disable-ADAccount -Identity $_.ADUser}
else { ... }
Of course this code doesn't work but I want to show where I'm not getting any further. $Userstatus and $Aduser are declared above in the code and their result is entered in the columns.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Select specified Column Cell Value in Datagridview

Post by jvierra »

No. Enumerate "rows" not columns.

Code: Select all

$datagridView1.Rows | 
        ForEach-Object{ 
            Write-Host $_.Cells['Username'].Value 
        }
User avatar
SKunzmann
Posts: 4
Last visit: Mon Sep 10, 2018 12:47 am

Re: Select specified Column Cell Value in Datagridview

Post by SKunzmann »

You are my hero! This looks like what I was missing and I think I got the point now and can go on with the next step. Thank you very much!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Select specified Column Cell Value in Datagridview

Post by jvierra »

Happy hunting.
User avatar
SKunzmann
Posts: 4
Last visit: Mon Sep 10, 2018 12:47 am

Re: Select specified Column Cell Value in Datagridview

Post by SKunzmann »

Hi, it's me again. After a few days I finally had time to work on the script again. So far everything works as desired, but I would also like to check what has been done in a text box.
For users who don't exist this fits so far. But as soon as the user actually exists, I get line breaks and blank lines in the output. So something seems to confuse the output of the variables.
  1. $buttonDisable_Click={
  2.    
  3.     #TODO: Place custom script here
  4.     $datagridview1.Rows |
  5.     ForEach-Object {
  6.        
  7.         $DisableUsername = $_.Cells['Username'].Value
  8.         if ($_.Cells['Accountstatus'].Value -like 'Enabled')
  9.         {
  10.             $richtextbox1.AppendText("$DisableUsername has been disabled `n") | Out-String
  11.            
  12.         }
  13.         else
  14.         {
  15.             $richtextbox1.AppendText("$DisableUsername already disabled or doesn't exist `n")
  16.         }
  17.     }
  18. }
Output:
Image
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Select specified Column Cell Value in Datagridview

Post by jvierra »

The best way to add line to a textbox is this:

$richtextbox1.Lines += "$DisableUsername has been disabled"

If you have line feeds in your original data then fix the original data.
This topic is 5 years and 9 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