943,733 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 11751
  • ASP.NET RSS
Feb 15th, 2009
0

format bound field in gridview

Expand Post »
hi
a have a gridview with datafield column which is bound to a datetime column.
i want the grid column to show the date when the day is first, like this: 24/2/2009

i tried the following format:
asp Syntax (Toggle Plain Text)
  1. DataFormatString="{0:dd/MM/YYYY}"

but the grid is still shows the date when the month is first.
where was i wrong ?
Reputation Points: 20
Solved Threads: 0
Junior Poster
emilio is offline Offline
162 posts
since Nov 2007
Feb 15th, 2009
0

Re: format bound field in gridview

it is case sensitive, try this :

ASP.NET Syntax (Toggle Plain Text)
  1. {0:dd/MM/yyyy}
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Feb 15th, 2009
0

Re: format bound field in gridview

the total example is this :

Default.aspx :

ASP.NET Syntax (Toggle Plain Text)
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head runat="server">
  7. <title></title>
  8. </head>
  9. <body>
  10. <form id="form1" runat="server">
  11. <div>
  12.  
  13. </div>
  14. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
  15. DataKeyNames="PKID" DataSourceID="SqlDataSource1"
  16. EmptyDataText="There are no data records to display.">
  17. <Columns>
  18. <asp:BoundField DataField="PKID" HeaderText="PKID" ReadOnly="True"
  19. SortExpression="PKID" />
  20. <asp:BoundField DataField="date" DataFormatString="{0:dd/MM/yyyy}"
  21. HeaderText="date" SortExpression="date" />
  22. </Columns>
  23. </asp:GridView>
  24. <asp:SqlDataSource ID="SqlDataSource1" runat="server"
  25. ConnectionString="<%$ ConnectionStrings:denemeConnectionString1 %>"
  26. DeleteCommand="DELETE FROM [Table1] WHERE [PKID] = @PKID"
  27. InsertCommand="INSERT INTO [Table1] ([date]) VALUES (@date)"
  28. ProviderName="<%$ ConnectionStrings:denemeConnectionString1.ProviderName %>"
  29. SelectCommand="SELECT [PKID], [date] FROM [Table1]"
  30. UpdateCommand="UPDATE [Table1] SET [date] = @date WHERE [PKID] = @PKID">
  31. <DeleteParameters>
  32. <asp:Parameter Name="PKID" Type="Int32" />
  33. </DeleteParameters>
  34. <InsertParameters>
  35. <asp:Parameter DbType="Date" Name="date" />
  36. </InsertParameters>
  37. <UpdateParameters>
  38. <asp:Parameter DbType="Date" Name="date" />
  39. <asp:Parameter Name="PKID" Type="Int32" />
  40. </UpdateParameters>
  41. </asp:SqlDataSource>
  42. </form>
  43. </body>
  44. </html>

Default.aspx.cs :

ASP.NET Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7.  
  8. public partial class _Default : System.Web.UI.Page
  9. {
  10. protected void Page_Load(object sender, EventArgs e)
  11. {
  12.  
  13. }
  14. }
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Feb 17th, 2009
0

Re: format bound field in gridview

i tryed it the way you suggested with small letters:
DataFormatString="{0:dd/MM/yyyy}"

it still doesnot work.

my full code line is:
asp Syntax (Toggle Plain Text)
  1. <asp:BoundField DataField="date" DataFormatString="{:dd/MM/yyyy}" HeaderText="date" />
Reputation Points: 20
Solved Threads: 0
Junior Poster
emilio is offline Offline
162 posts
since Nov 2007
Feb 17th, 2009
0

Re: format bound field in gridview

The problem is the by default, the HtmlEncode property of the boundfield attribute is set to True. This helps prevent cross-site scripting attacks and malicious content from being displayed. Microsoft recommends that the HtmlEncode attribute be enabled whenever possible.

The problem is that if this field is enabled, you can not pass format information to the boundfield control. To solve this issue simply set HtmlEncode to false

Try this:


asp Syntax (Toggle Plain Text)
  1. <asp:BoundField DataField="date" DataFormatString="{0:dd/MM/yyyy}" HtmlEncode="false" HeaderText="date" />
Reputation Points: 16
Solved Threads: 18
Junior Poster
Aneesh_Argent is offline Offline
104 posts
since Dec 2008
Feb 17th, 2009
0

Re: format bound field in gridview

well i tryed this and it still doesnot work.
Reputation Points: 20
Solved Threads: 0
Junior Poster
emilio is offline Offline
162 posts
since Nov 2007
Apr 19th, 2009
0

Re: format bound field in gridview

Still not resolved??????????
Reputation Points: 26
Solved Threads: 44
Posting Whiz in Training
mail2saion is offline Offline
247 posts
since Apr 2009
Apr 20th, 2009
0

Re: format bound field in gridview

just try this:

ASP.NET Syntax (Toggle Plain Text)
  1. {d:0}

Works for me!
Reputation Points: 22
Solved Threads: 9
Junior Poster
ninjaimp is offline Offline
129 posts
since Apr 2008
Apr 21st, 2009
0

Re: format bound field in gridview

Click to Expand / Collapse  Quote originally posted by emilio ...
well i tryed this and it still doesnot work.
Hi

try this example:

asp.net Syntax (Toggle Plain Text)
  1. <asp:BoundField DataField=”date_from” HeaderText=”DateApply”
  2. HeaderStyle-Width =”350px” DataFormatString=”{0:MM/dd/yyyy}” HtmlEncode=”false”>
  3.  
  4. //—–another way
  5.  
  6. <asp:TemplateField HeaderText=”Date Submission” SortExpression=”DateSubmission”>
  7. <ItemTemplate>
  8. <asp:Label ID=”Label1″ runat=”server” Text=’<%# Bind(”DateSubmission”, “{0:dd/MM/yyyy}”) %>’></asp:Label>
  9. </ItemTemplate>
  10. <ItemStyle HorizontalAlign=”Center” />
  11. </asp:TemplateField>
Last edited by peter_budo; Apr 25th, 2009 at 5:17 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
nil
Reputation Points: 10
Solved Threads: 0
Light Poster
nil is offline Offline
30 posts
since Nov 2006
Apr 22nd, 2009
0

Re: format bound field in gridview

Reputation Points: 26
Solved Threads: 44
Posting Whiz in Training
mail2saion is offline Offline
247 posts
since Apr 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: To search records
Next Thread in ASP.NET Forum Timeline: ASP Site crashes after sitting idle for 30 minutes.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC