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.
This topic is 5 years and 10 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
falconcurt
Posts: 19
Last visit: Mon Jan 29, 2024 11:17 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 2257 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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
Last visit: Mon Jan 29, 2024 11:17 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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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 153 times
User avatar
falconcurt
Posts: 19
Last visit: Mon Jan 29, 2024 11:17 am

Re: Validating cells in a column on a DataGridView

Post by falconcurt »

That is exactly the event i need. Thanks!
This topic is 5 years and 10 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