Click here to Skip to main content
15,887,356 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I show a file excel in datagridview and it has columns I want to check values of gender column including only 2 values are male or female. If the gender column does not have 2 values above it will error! I want to make a button to test the gender column. How I do code ? Help me !!!


What I have tried:

I don't know write program what it's like !
Posted
Updated 11-Jan-21 22:26pm

The best way to do that is when you receive the data, rather than when you are presenting it to the user.

But a DGV is pretty flexible, and there is a CellPainting event it fires when a cell is to be drawn to the screen - I use it to colour cells according to their value: Colouring DataGridView Cells According to their Content in WinForms[^] - as a visual warning to the user, you could use that to indicate "invalid gender" values (though you run the risk of alienating a lot of people who don't believe in binary genders).

When it comes to testing the whole column however, you shouldn't use the DGV content at all, but the underlying datasource - DGV's should be filterable, orderable, pageable so the displayed content may not cover the whole data set.
Using the underlying datasource is simpler - because you can use whatever collection of information you started with - and more maintainable - because you can replace the DGV with a different control and not affect your code.

So look at your DataSource - and validate that (preferably before it gets to the display!)
 
Share this answer
 
Comments
Huyyhaha 12-Jan-21 4:07am    
I do that to check an excel file that has quite large data (there are thousands of people) so I have to check its value fields to enter the database. !
can you help me ?
OriginalGriff 12-Jan-21 4:26am    
With what bit?
You have your data from excel, and you certainly shouldn't be just dropping it in a DGV - thousands of rows are way too much for a user to want to see - so start with your data source: the collection you read the excel data into. I can't see your code, so I have no idea where you store it!
You can use the DataGridView.DataError event, but this might only work on the currently displayed data, see: How can I validate DataGridView input?[^]
And: DataGridView.DataError Event (System.Windows.Forms) | Microsoft Docs[^]

If you want to check values on entry you can also use the CellValidating event, see:
Walkthrough: Validate data in DataGridView control - Windows Forms .NET Framework | Microsoft Docs[^]
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900