ARTICLE

How You can Use CSV File as Data Source of Gridview

Posted by Hiren Soni Articles | Windows Forms C# August 27, 2010
Here I will show how you can use csv file as data source of gridview.
Reader Level:

HTML clipboard

Here I will show how you can use csv file as data source of gridview.

Demo.csv file
firstname,lastname
hiren,soni
kirtan,patel
sanjay,parmar
ghanu,nayak

Output:

image.gif

Code and explanation:

        //  get all lines of csv file
        string[] str = File.ReadAllLines(Server.MapPath("demo.csv"));

        // create new datatable
        DataTable dt = new DataTable();

        // get the column header means first line
        string[] temp = str[0].Split(',');

        // creates columns of gridview as per the header name
        foreach(string t in temp)
        {
          dt.Columns.Add( t , typeof(string));
        }

         // now retrive the record from second line and add it to datatable
        for(int i=1;i<str.Length;i++)
        {
            string[] t = str[i].Split(',');
            dt.Rows.Add(t);

        }

        // assign gridview datasource property by datatable
        GridView1.DataSource = dt;

        // bind the gridview
        GridView1.DataBind();


Hope you understand it.

Thank You.

Login to add your contents and source code to this article
post comment
     

Sure!

Hope it will helps you

 

        //  get all lines of csv file

        string[] str = File.ReadAllLines(Server.MapPath("demo.csv"));

 

        // create new datatable

        DataTable dt = new DataTable();

 

        // get the column header means first line

        string[] temp = str[0].Split(',');

 

 

        // creates columns of gridview as per the header name

        foreach (string t in temp)

        {

            string tempstr = t;

                     tempstr = tempstr.Trim('\"');

            dt.Columns.Add(tempstr, typeof(string));

        }

 

        // now retrive the record from second line and add it to datatable

        for (int i = 1; i < str.Length; i++)

        {

 

            string[] t = str[i].Split(',');

           

         for(int j=0;j<t.Length;j++)

 

         {

             t[j] = t[j].Trim('\"');

         }

 

            dt.Rows.Add(t);

 

        }

 

        // assign gridview datasource property by datatable

        GridView1.DataSource = dt;

 

        // bind the gridview

        GridView1.DataBind();

 

Posted by Hiren Soni Aug 30, 2010

pls can you expand this solution to address csv file with double quote. tnx.

Posted by Ndidi Edmund Aug 29, 2010
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts