Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

Need help with Importing CSV to DataGridView

$
0
0

Hi all,

I'm trying to create a stand-alone program so we can upload CSV files into a Datagridview.

The issue is when I try to upload tester.csv (Has 10 columns with 2 rows, including header row) I get an Exception error: "Empty path name is not legal."  Then when I go back into the openfiledialog browse window, the file extension isn't there. I am able to manually add it in and then everything works for the program.

Anyone have any thoughts??  The code is below.

public partial class CSVtoNATCHA : Form    {        public CSVtoNATCHA()        {            InitializeComponent();        }        private string readFile(string filePath)        {            try            {                StreamReader streamReader = new StreamReader(filePath); //Sets the StreamReader                return streamReader.ReadToEnd(); // Opens and reads the file to be updated.            }            catch (Exception e) //Shows the error if the StreamReader fails.            {                MessageBox.Show(e.Message, "Error!"MessageBoxButtons.OK, MessageBoxIcon.Error);                return e.Message.ToString();            }        }        //Helper method to generate the uploaded file into the datagridview        private DataTable generateDataTableFromCSV(string fileContent)        {            DataTable dt = new DataTable();            string[] row = fileContent.Split("\r\n".ToCharArray());            string rowstr = row[0];            string[] col = rowstr.Split(',');            int colCount = 1;            foreach (string colstr in col)            {                dt.Columns.Add(new DataColumn("Column" + colCount));                colCount++;            }            dt.AcceptChanges();            return dt;        }        public void btnBrowse_Click(object sender, EventArgs e)        {            // Start new instance of the OpenFileDialog controller            //OpenFileDialog ofd = new OpenFileDialog();            // Declare local variables.            string file = @ofd.FileName;            //Filter to only show CSV files to upload            ofd.Filter = "CSV Files (*.csv)|*.csv";            //Set ofd title            ofd.Title = "Browse to upload CSV";            // Show dialog to allow user to open a file.            bool? userClickedOK = ofd.ShowDialog() == DialogResult.OK;            if (userClickedOK == true)            {                string fileName = ofd.FileName;                txtFile.Text = file.ToString(); //Shows the string path inside the textbox that is being uploaded to datagridview.                readFile(file); //pass the File uploaded string to the method                csvUpload(file); //pass the File uploaded string to the method            }        }        //helper method for when the csv file is uploaded.        private string csvUpload(string csvFile)        {            string csvUpload = csvFile;            return csvUpload; //will return the string path of the file being uploaded.        }        private void btnExport_Click(object sender, EventArgs e)        {            MessageBox.Show("Currently a work in progress.""In Progress"MessageBoxButtons.OK, MessageBoxIcon.Exclamation);        }        private void closeToolStripMenuItem_Click(object sender, EventArgs e)        {            //Closes the program.            this.Close();        }        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)        {            //Shows help information            MessageBox.Show("This app will allow us to upload a CSV file and export it as NACHA format.\n\nVersion: 1.0\nBeta Version""About app"MessageBoxButtons.OK, MessageBoxIcon.Information);        }        private void btnUpload_Click(object sender, EventArgs e)        {            // MessageBox.Show("Function coming soon.", "Not ready yet", MessageBoxButtons.OK, MessageBoxIcon.Information);            string fileContent = this.readFile(ofd.FileName);            DataTable dt = this.generateDataTableFromCSV(fileContent);            string[] row = fileContent.Split("\r\n".ToCharArray());            foreach (string rowstr in row)            {                DataRow myRow = dt.NewRow();                int colCount = 0;                string[] col = rowstr.Split(',');                foreach (string colstr in col)                {                    myRow[colCount] = colstr;                    colCount++;                }                dt.Rows.Add(myRow);            }            gvData.DataSource = dt;        }           }


___________

Allen D.


Viewing all articles
Browse latest Browse all 31927

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>