2010-10-20 64 views
0

我正在使用SQL Server 2005,C#.NET。在网格视图页面上没有时间显示

我只想在我的网格视图页面上显示日期,目前它显示的时间也包括在内。 我正在使用文件上传按钮点击功能恢复表中的日期。 在我的表中,我加载日期列数据类型是DateTime。 的exaple在表列中的表现一样10/19/2010 12:00:00 AM

在我的网格视图页面还我收到日期栏一样10/19/2010 12:00:00 AM

我想要像10/19/2010一样在我的网格上展示。

string strDate = DateTime.Now.ToShortDateString(); 
cmd.Parameters.Add("@LoadDate", SqlDbType.DateTime).Value = strDate.ToString(); 

以下是我的代码。请任何人都可以帮助我如何删除我的网格视图页面上的时间。

protected void btnUpload_Click(object sender, EventArgs e) 
     { 


      // Read the file and convert it to Byte Array 
      string strClaimNumber = lblFileUploadCliamNumber.Text.ToUpper(); 
      string strDate = DateTime.Now.ToShortDateString(); 

      string strDescription = txtDescription.Text.ToString(); 
      string filePath = FileUpload1.PostedFile.FileName; 
      string filename = Path.GetFileName(filePath); 
      string ext = Path.GetExtension(filename); 
      string contenttype = String.Empty; 

      //Set the contenttype based on File Extension 

      switch (ext) 
      { 
       case ".doc": 
        contenttype = "application/vnd.ms-word"; 
        break; 
       case ".docx": 
        contenttype = "application/vnd.ms-word"; 
        break; 
       case ".xls": 
        contenttype = "application/vnd.ms-excel"; 
        break; 
       case ".xlsx": 
        contenttype = "application/vnd.ms-excel"; 
        break; 
       case ".jpg": 
        contenttype = "image/jpg"; 
        break; 
       case ".png": 
        contenttype = "image/png"; 
        break; 
       case ".gif": 
        contenttype = "image/gif"; 
        break; 
       case ".pdf": 
        contenttype = "application/pdf"; 
        break; 

      } 

      if (contenttype != String.Empty) 
      { 


       Stream fs = FileUpload1.PostedFile.InputStream; 
       BinaryReader br = new BinaryReader(fs); 
       Byte[] bytes = br.ReadBytes((Int32)fs.Length); 


       //insert the file into database 

       string strQuery = "insert into tblFiles(Name, ContentType, Data, Description, ClaimNumber, LoadDate)" + 
        " values (@Name, @ContentType, @Data, @Description, @ClaimNumber, @LoadDate)"; 
       SqlCommand cmd = new SqlCommand(strQuery); 
       cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename; 
       cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value 
        = contenttype; 
       cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes; 
       cmd.Parameters.Add("@Description", SqlDbType.VarChar).Value = strDescription.ToString(); 
       cmd.Parameters.Add("@ClaimNumber", SqlDbType.NVarChar).Value = strClaimNumber.ToUpper(); 
       cmd.Parameters.Add("@LoadDate", SqlDbType.DateTime).Value = strDate.ToString(); 
       InsertUpdateData(cmd); 
       lblMessage.ForeColor = System.Drawing.Color.Green; 
       lblMessage.Text = "File Uploaded Successfully"; 
       GridView1.DataBind(); 
      } 

      else 
      { 

       lblMessage.ForeColor = System.Drawing.Color.Red; 
       lblMessage.Text = "File format not recognised." + 
        " Upload Word/PDF/Excel formats"; 

      } 

     } 

回答

0

尝试这样的事情你的GridView模板内:

<asp:Label ID="lblDate" Text='<%# String.Format("{0:M/d/yyyy}", Eval("DateColumnName")) %>' runat="server" /> 
+0

谢谢Shaji。真棒。 – Ravi 2010-10-20 18:19:20

0

如果您选择从数据库表(retriving所保存的日期)的日期,并在DataGridView中使用显示它下面提到查询,

Select Convert(Varchar(15),Datecolumn,1) from Tablename 

上述查询将以05/09/13格式显示日期。