0

I uploaded my CSV file into DataGrid using ASP.net and C# and it worked, but I have tried to use Table instead of DataGrid and dose not work with me. So what I have to change in my code to view the data into the table theses two lines of code where I used the dataGrid

 DataGrid.DataSource = GetDataTableFromCSVFile(currentPath);
 DataGrid.DataBind();

and here all the Code

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Web.UI;
 using System.Web.UI.WebControls;
 using System.Data;
 using System.IO;
 using Microsoft.VisualBasic.FileIO;

 namespace Csvfile
 {
 public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void btCSVUpload_Click(object sender, EventArgs e)
    {
        if (FileUploadControl.HasFile)
        {
            try
            {
                string currentPath = Server.MapPath("~/") +
                              Path.GetFileName(FileUploadControl.FileName);
                FileUploadControl.SaveAs(currentPath);

                DataGrid.DataSource = GetDataTableFromCSVFile(currentPath);
                DataGrid.DataBind();
                lbStatus.Text = "CSV Upload status: File uploaded!";

                File.Delete(currentPath);
            }
            catch (Exception ex)
            {
                lbStatus.Text = @"CSV Upload status: The file could not be  uploaded. 
                The following error has occured: " + ex.Message;
            }
        }
        else
        {
            lbStatus.Text = "CSV Upload status: File not found.";
        }
    }

    private static DataTable GetDataTableFromCSVFile(string csvfilePath)
    {
        DataTable csvData = new DataTable();
        using (TextFieldParser csvReader = new TextFieldParser(csvfilePath))
        {
            csvReader.SetDelimiters(new string[] { "," });
            csvReader.HasFieldsEnclosedInQuotes = true;

            //Read columns from CSV file, remove this line if columns not exits  
            string[] colFields = csvReader.ReadFields();

            foreach (string column in colFields)
            {
                DataColumn datecolumn = new DataColumn(column);
                datecolumn.AllowDBNull = true;
                csvData.Columns.Add(datecolumn);
            }

            while (!csvReader.EndOfData)
            {
                string[] fieldData = csvReader.ReadFields();
                //Making empty value as null
                for (int i = 0; i < fieldData.Length; i++)
                {
                    if (fieldData[i] == "")
                    {
                        fieldData[i] = null;
                    }
                }
                csvData.Rows.Add(fieldData);
            }
        }
        return csvData;
    }
}

}

Don Coraliony
  • 31
  • 1
  • 4

1 Answers1

0

ASP.NET translates a DataGrid into an HTML table when it builds the response. If you view the source from the browser, you can see that your grid is rendered as an HTML table.

Clint B
  • 4,610
  • 2
  • 18
  • 22