Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to export the datas in datagridview to csv file I created a class for export option then call class from the button but I'm getting the error

using System.Windows.Forms;

namespace excel2

    internal class ExportHelper
    {
        public bool Export(DataGridView dgv)
        {
            bool exported = false;

            List<string> lines = new List<string>();
            DataGridViewColumnCollection column = dgv.Columns;
            bool firstDone = false;
            StringBuilder columnLine = new StringBuilder();
            foreach(DataGridViewColumn col in column)
            {
                if(!firstDone)
                {
                    columnLine.Append(col.DataPropertyName);
                    firstDone = true;
                }
                else
                {
                    columnLine.Append("," + col.DataPropertyName);
                }
            }
            lines.Add(columnLine.ToString());

            //data lines
            foreach(DataGridViewRow row in dgv.Rows)
            {
                StringBuilder dataLine = new StringBuilder();
                firstDone = false;
                foreach(DataGridViewCell cell in row.Cells)
                {
                    if (firstDone)
                    {
                        dataLine.Append(cell.Value);
                        firstDone= true;
                    }
                    else
                    {
                        dataLine.Append(","+cell.Value);
                    }
                    lines.Add(dataLine.ToString());
                }

            }
            string file = Path.Combine(Application.StartupPath, "Excel");
            File.WriteAllLines(file,lines); //this line giving error
            System.Diagnostics.Process.Start(file);

            return exported;
        }
    }


the code I attached to button from form

private void button8_Click(object sender, EventArgs e)
{
    new ExportHelper().Export(dataGridView1);
}


the errror message : System.UnauthorizedAccessException: ''C:\Users\Yusuf\Desktop\VisualStudio\25derste25uygulamali-proje\excel2\excel2\bin\Debug\Excel' Access denied to path.'

What I have tried:

tbh I don't know what can I try, I'm sorry
Posted
Updated 19-May-22 3:44am
v2
Comments
Richard MacCutchan 19-May-22 7:27am    
"this line giving error"
What error? We cannot help if we do not know what the problem is.
YusufA0 19-May-22 7:37am    
Oh sorry I forget to add here is the error message : System.UnauthorizedAccessException: ''C:\Users\Yusuf\Desktop\VisualStudio\25derste25uygulamali-proje\excel2\excel2\bin\Debug\Excel' Access denied to path.'
Richard MacCutchan 19-May-22 7:43am    
As stated in the message, you do not have permission to write in that location.
YusufA0 19-May-22 7:44am    
how can I fix that?
Richard MacCutchan 19-May-22 7:48am    
You need to check that location to see why it is denying access. This is not a coding problem, but something to do with Windows and your setup.

1 solution

Solved by changing
string file = Path.Combine(Application.StartupPath, "Excel");

to

string file = Path.Combine(Application.StartupPath, "Excel.csv");
 
Share this answer
 
Comments
PIEBALDconsult 19-May-22 15:35pm    
Please don't try to answer your own question. Use the Improve Question button to add detail to the question.

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