SIGN UP MEMBER LOGIN:    
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
share 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
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Team Foundation Server Hosting
Become a Sponsor