I need to be able to generate a basic CSV file of the results returned from an SqlDataSource/GridView upon the user clicking a button, then allow them to save this file.
Is this simple to do?
Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Since you are using a SqlDataSource then you can get a DataTable out of it like this:
I wrote a set of extension methods to do DataTable to CSV, which you should be able to use easily.
The rest of your code would be pretty simple then:
Here is a full example using this very method.
The Markup:
And the code-behind:
There you go. The only thing not included here is the extension method source, but since I’ve linked to it, you should have no problems getting that into a static class.