News
IBM to offer mobile security as a service IBM will start delivering mobile security-as-a-service (MaaS) through its own cloud infrastructure in India, a move that is expected to better address in-country data requirements... IBM Spectrum Computing: IBM has enlarged its portfolio of software-defined infrastructure solutions with cognitive features for workload management.  * LiFi internet technology has been introduced, the new type of wireless internet connection that gives 100 times faster than traditional WiFi -- Invented by Professor Harald Haas from the University of Edinburgh. * Sci. Rachid Yazami has developed a smart chip that charges smartphones in less than 10 minutes. BenQ has launched BlueCore projector - Consumer electronics major BenQ has launched its first BlueCore laser light source projector. For those unaware, devices with BlueCore laser technology have a high contrast output of 80000:1 with an extended lamp life and efficiency. ***
  Jun 18 2017 11:38PM     Michael
  0 Comments    1301 Views  
Here Michael provided a short description with example for binding gridview from CSV file.
Here simple example for bind Gridview from CSV file. 

CSV File: 


HTML Code:

Copy the HTML code and paste it in your design page.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h4>  
            The Developer Tips - Beyond the Knowledge
        </h4>  
        <hr >
        
        <h4>CSV File to Gridview</h4>
        <table>  
            <tr>  
                <td>  
                    Select File  
                </td>  
                <td>  
                    <asp:FileUpload ID="FileUpload1" runat="server" />  
                </td>  
                <td>  
                </td>  
                <td>  
                    <asp:Button ID="btnFileUpload" runat="server" Text="Upload" OnClick="btnFileUpload_Click" />  
                </td>  
            </tr>  
            <tr>
            <td>
            <asp:Label ID="lblError" runat="server" style="color:Red"></asp:Label>
            </td>
            </tr>
        </table>  
        <br >  
        <br >  
        <asp:GridView ID="grdCSV" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">  
            <AlternatingRowStyle BackColor="White" />  
            <EditRowStyle BackColor="#7C6357" />  
            <FooterStyle BackColor="#1C5E66" Font-Bold="True" ForeColor="White" />  
            <HeaderStyle BackColor="#1C5E66" Font-Bold="True" ForeColor="White" />  
            <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />  
            <RowStyle BackColor="#E3EAEB" />  
            <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />  
            <SortedAscendingCellStyle BackColor="#F8FAFA" />  
            <SortedAscendingHeaderStyle BackColor="#246B61" />  
            <SortedDescendingCellStyle BackColor="#D4DFE1" />  
            <SortedDescendingHeaderStyle BackColor="#15524A" />  
        </asp:GridView>  
    </div>  
    </form>  
</body>  
</html>


Namespace:

using System.Data;
using System.IO;

C# Code:

Method for Page Load Event

protected void Page_Load(object sender, EventArgs e)
{
       lblError.Visible = false;
}

Method for File Upload Button

protected void btnFileUpload_Click_Click(object sender, EventArgs e)
{

            //Creating object of datatable  
            DataTable dtTblcsv = new DataTable();

            try
            {
                //creating columns in datatable
                dtTblcsv.Columns.Add("Name");
                dtTblcsv.Columns.Add("City");
                dtTblcsv.Columns.Add("Address");
                dtTblcsv.Columns.Add("Designation");

                //getting full file path of Uploaded file  
                string CSVFilePath = Server.MapPath(FileUpload1.PostedFile.FileName);

                //Reading All text  
                string ReadCSV = File.ReadAllText(CSVFilePath);

                //spliting row after new line  
                foreach (string csvRow in ReadCSV.Split('\n'))
                {
                    if (!string.IsNullOrEmpty(csvRow))
                    {
                        //Adding each row into datatable  
                        dtTblcsv.Rows.Add();
                        int count = 0;
                        foreach (string FileRec in csvRow.Split(','))
                        {
                            dtTblcsv.Rows[dtTblcsv.Rows.Count - 1][count] = FileRec;
                            count++;
                        }
                    }

                    //Calling Bind Grid Functions  
                    Bind_Gridview(dtTblcsv);

                }
            }
            catch (Exception ex)
            {
                lblError.Visible = true;
                lblError.Text = ex.Message.ToString();
            }
        }

Method for Bind Gridview

private void Bind_Gridview(DataTable csvdt)
{
            try
            {
                grdCSV.DataSource = csvdt;
                grdCSV.DataBind();
            }
            catch (Exception ex)
            {
                lblError.Visible = true;
                lblError.Text = ex.Message.ToString();
            }            
}

Result:


 I hope it will helps. Thanks.

BackToTop
Comments



 
Search
Recent Posts
Get Google Map Lat Lng (Geo Point) By Pincode in C#
Nov 8 2017 10:26AM Posted By Pranav
Google URL Shortener in C#
Oct 26 2017 10:43AM Posted By Sanjay
Bind Gridview from CSV file in Asp.Net C#
Jun 18 2017 11:38PM Posted By Michael
Call WebService method from jQuery in every 1 minute
Feb 27 2017 11:51AM Posted By John
Gridview custom CSS in ASP.Net
Feb 25 2017 10:59AM Posted By Micheal Ryan
Read excel file and bind to Gridview in C#
Feb 25 2017 10:20AM Posted By Micheal
Read excel file and convert to List in C#
Feb 25 2017 6:26AM Posted By Michael
Tags
Follow us on Facebook
Follow us on Google +
Recent post in your Email inbox.
Enter your email address: