Validating cells in a column on a 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.
Locked
User avatar
falconcurt
Posts: 19
Joined: Wed Apr 20, 2016 9:35 am

Validating cells in a column on a DataGridView

Post by falconcurt »

I am looking to validate each cell within the Password column on my DataGridView. I have created the DataGridViewCellValidatingEventHandler event on my form and I have my function to make sure the password meets the complexity requirements but not sure exactly how to put the cells in an array for that column and what to put in my Foreach ($cell in $column) {}. Any help to get me start would help.
Attachments
DataGrid.JPG
DataGrid.JPG (19.48 KiB) Viewed 1502 times

jvierra
Posts: 14614
Joined: Tue May 22, 2007 9:57 am
Answers: 2
Has voted: 1 time
Been upvoted: 3 times
Contact:

Re: Validating cells in a column on a DataGridView

Post by jvierra »

The validate event execute for each column in the grid. YOu need to check the coloumn index to run your code only foro the password column. THe cell being validates is abtained from the RowIndex and COlumnIndex passed in the event.

Code: Select all

if($_.ColumnIndex -eq 3){
    # this is the password column
    # the following gets the cell being validated.
    $cell = $datagridview1.Rows[$_.RowIndex].Cells[3]
}

User avatar
falconcurt
Posts: 19
Joined: Wed Apr 20, 2016 9:35 am

Re: Validating cells in a column on a DataGridView

Post by falconcurt »

Ok, I guess this may not be the route i need. What i am trying to accomplish is importing a CSV file into the DataGridView window with all of the data. I would like it to highlight red if the Password doesn't meet the requirements. Is the DataGridViewCellValidatingEventHandler event the best route i need to take? It seems to only hightlight the cell after i click on the cell. I would like for it to populate without me clicking on it.

jvierra
Posts: 14614
Joined: Tue May 22, 2007 9:57 am
Answers: 2
Has voted: 1 time
Been upvoted: 3 times
Contact:

Re: Validating cells in a column on a DataGridView

Post by jvierra »

To do that we would use the "Paint" event to set the color of the cell. Just set the backcolor based on the cells contents.

Example:
Attachments
Test-CellValidating2.psf
(16.44 KiB) Downloaded 85 times

User avatar
falconcurt
Posts: 19
Joined: Wed Apr 20, 2016 9:35 am

Re: Validating cells in a column on a DataGridView

Post by falconcurt »

That is exactly the event i need. Thanks!

Locked