Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my application i have to load a csv file to DataGridView and in my csv file i dont have any inverted commas(" ") but when i try to load it in my DataGridView am automatically getting inverted commas (" "), I have no idea why am getting them.

C#
private void Load_Click(object sender, EventArgs e)
        {
            string delimiter = ",";
            string tablename = "export";
            string filename = ("c:\\izaz\\test.txt");


            DataSet dataset = new DataSet();
            StreamReader sr = new StreamReader(filename);

            dataset.Tables.Add(tablename);
            dataset.Tables[tablename].Columns.Add("Name");
            dataset.Tables[tablename].Columns.Add("Path");

            string allData = sr.ReadToEnd();
            string[] rows = allData.Split("\r".ToCharArray());

            foreach (string r in rows)
            {
                string[] items = r.Split(delimiter.ToCharArray());
                dataset.Tables[tablename].Rows.Add(items);
            }
            this.dataGridView1.DataSource = dataset.Tables[0].DefaultView;
        
            foreach (DataGridViewColumn column in dataGridView1.Columns)
                column.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            
           
        }



out put looks like this:

C#
  Name                                     Path     
"AWServer_3.1"	              "/vbnrt735w6/sdfasd34564/AWServer_3.1"
"CCW5.1.1_DEMO"               "/CCW5.1.1_DEMO"
"DEMO-EA"                     "/4564/DEMO-EA"
"MUSE_DEMO"                   "/vbnrt735w6/MUSE_DEMO"
"UV_CARDIO"                   "/asd/UV_CARDIO"
"UView-Web_Cardio_2015_v2"    "/UView-Web_Cardio_2015_v2"
Posted
Updated 16-Jul-15 23:49pm
v2
Comments
Thomas Daniels 17-Jul-15 5:55am    
By inverted commas, do you mean a double quotation mark?
https://en.wikipedia.org/wiki/Quotation_mark
Abhishek Pant 17-Jul-15 5:57am    
[no name] 17-Jul-15 5:57am    
I would guess that you CSV file has the quotation marks already in there. If you don't want them in your output, then filter them out.

The reason for your problem is that you have control characters in your strings, namely the new line character.Try doing it this way.


C#
char delimiter = ',';
string tablename = "export";
string filename = ("c:\\izaz\\test.txt");


DataSet dataset = new DataSet();
dataset.Tables.Add(tablename);
dataset.Tables[tablename].Columns.Add("Name");
dataset.Tables[tablename].Columns.Add("Path");

string[] rows = File.ReadAllLines(filename);
foreach (string r in rows)
{
    string[] items = r.Split(delimiter);
    dataset.Tables[tablename].Rows.Add(items);
}
 
Share this answer
 
Change

C#
string[] items = r.Split(delimiter.ToCharArray());


to

C#
string[] items = r.Replace("\"", string.Empty).Split(delimiter.ToCharArray());
 
Share this answer
 
v2
Be careful with CSV formatted data.

  1. Be aware that there is no official standard for CSV.
  2. If you mean double quotes with "inverted commas", then be also aware that they serve a certain purpose: the string between the quotes may contain the separator character as well as quotes character (mostly escaped by doubling the quotes character).

E.g. a 3-column CSV may be as follows (assuming comma as field separator):
a,b,c
d,,
"this is not a separator: ,", this is a "" in a quoted string",   spaces    may also be part of the field value
,,  "spaces around a quoted string may also be taken as part of the filed value"  ,

There may even be a new-line in a field, typically in a quoted string.

If you aim to read any CSV file that was produced by any kind of program, then you have to employ a proper CSV parser. That parser would take care of all the special cases as well as of removing and un-escaping quote characters, etc.

If you have the production of the CSV file under your full control, then you know how you may simplify the general purpose CSV grammar for your needs, e.g. by asserting that no separators nor newlines nor quotes are ever part of your fields and that all fields have content, etc. If the second case is given, you may employ the simple string.Split function.

Cheers
Andi
 
Share this answer
 

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