Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm importing an Excel file to ASP.NET Gridview at Runtime and a certain column includes dates (2/31/2000) with a cell format of (*d/mm/yyyy).

When I import the Excel file in ASP.NET, the data suddenly becomes like this in the gridview

12/31/200 12:00:00 AM

I can not add bound filed because I am loading data at run time my data loading code is:

C#
Filename1 = FileUpload1.FileName.ToString();
       FileUpload1.SaveAs(MapPath("~/Temp/"+Filename1));
       if (File.Exists(MapPath("~/Temp/"+Filename1)))
       {
           string[] Arr = null;
           Arr = Filename1.Split('.');
           if (Arr.Length > 0)
           {
               if (Arr[Arr.Length - 1] == "xls")
                   sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + (MapPath("~/Temp/"+Filename1)).ToString() + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
           }
           if (Arr[Arr.Length - 1] == "xlsx")
           {
               sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + (MapPath("~/Temp/"+Filename1)).ToString() + ";Extended Properties='Excel 12.0 Xml;HDR=YES';";
               //MessageBox.Show("excute constring");
           }
           if (Arr[Arr.Length - 1] == "xlsm")
           {
               sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + (MapPath("~/Temp/"+Filename1)).ToString() + ";Extended Properties='Excel 12.0 Xml;HDR=YES';";
               //MessageBox.Show("mecro constring");
           }
       }
       findsheetname1();
       FillGrid1();
Posted

 
Share this answer
 
Comments
Asif 7969814 28-Oct-14 0:29am    
Hi Friend I have solved this using that code

string date = "";
DateTime newdate,d1;

if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 1; i < e.Row.Cells.Count; i++)
{
//BoundField field = (BoundField)((DataControlFieldCell)e.Row.Cells[i]).ContainingField;
date = e.Row.Cells[i].Text.ToString();
if(date.Length>7)
{
if (DateTime.TryParse(date, out newdate))
{
//DateTime.TryParseExact(date, "mm/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out newdate);
d1 = newdate.Date;//DateTime.ParseExact(date, "MM/dd/yyyy", CultureInfo.InvariantCulture);

e.Row.Cells[i].Text = d1.Date.ToString("MM/dd/yyyy");//newdate.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture)
}
}

}

}
Hi Friend I have solved this using that code

C#
string date = "";
        DateTime newdate,d1;

        if (e.Row.RowType == DataControlRowType.DataRow)
            {
                for (int i = 1; i < e.Row.Cells.Count; i++)
                {
                    //BoundField field = (BoundField)((DataControlFieldCell)e.Row.Cells[i]).ContainingField;
                    date = e.Row.Cells[i].Text.ToString();
                    if(date.Length>7)
                    {
                        if (DateTime.TryParse(date, out newdate))
                        {
                            //DateTime.TryParseExact(date, "mm/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out newdate);
                            d1 = newdate.Date;//DateTime.ParseExact(date, "MM/dd/yyyy", CultureInfo.InvariantCulture);

                            e.Row.Cells[i].Text = d1.Date.ToString("MM/dd/yyyy");//newdate.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture)
                        }
                    }

                }

            }
 
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