Click here to Skip to main content
15,890,043 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I'm importing data from excel to datagridview, I've completed that. But I need to do certain validations. I need to check whether file imported using OpenFileDialog is an excel file or not, if its not excel then I should show a messagebox showing "check file format Import failed". Is there something to do with Filter and compare it with filename pls help me

Thanks in advance
Posted

1 solution

OpenFileDialog.Filter allows you to filter files by their extension. E.g.
C#
openFileDialog.Filter = "Excel files (*.xls, *.xlsx)|*.xls;*.xlsx"


However any file can have any extension, so that doesn't ensure it's an excel file, so when you start your import you'll want to do so in a try-catch block so you can throw a message in the head of the user telling him to give you a correct file.
 
Share this answer
 

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

  Print Answers RSS


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