format bound field in gridview

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 144
Reputation: emilio is an unknown quantity at this point 
Solved Threads: 0
emilio emilio is offline Offline
Junior Poster

format bound field in gridview

 
0
  #1
Feb 15th, 2009
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:
  1. DataFormatString="{0:dd/MM/YYYY}"

but the grid is still shows the date when the month is first.
where was i wrong ?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 118
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: format bound field in gridview

 
0
  #2
Feb 15th, 2009
it is case sensitive, try this :

  1. {0:dd/MM/yyyy}
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 118
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: format bound field in gridview

 
0
  #3
Feb 15th, 2009
the total example is this :

Default.aspx :

  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 :

  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. }
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 144
Reputation: emilio is an unknown quantity at this point 
Solved Threads: 0
emilio emilio is offline Offline
Junior Poster

Re: format bound field in gridview

 
0
  #4
Feb 17th, 2009
i tryed it the way you suggested with small letters:
DataFormatString="{0:dd/MM/yyyy}"

it still doesnot work.

my full code line is:
  1. <asp:BoundField DataField="date" DataFormatString="{:dd/MM/yyyy}" HeaderText="date" />
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 104
Reputation: Aneesh_Argent is an unknown quantity at this point 
Solved Threads: 18
Aneesh_Argent Aneesh_Argent is offline Offline
Junior Poster

Re: format bound field in gridview

 
0
  #5
Feb 17th, 2009
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:


  1. <asp:BoundField DataField="date" DataFormatString="{0:dd/MM/yyyy}" HtmlEncode="false" HeaderText="date" />
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 144
Reputation: emilio is an unknown quantity at this point 
Solved Threads: 0
emilio emilio is offline Offline
Junior Poster

Re: format bound field in gridview

 
0
  #6
Feb 17th, 2009
well i tryed this and it still doesnot work.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 220
Reputation: mail2saion is an unknown quantity at this point 
Solved Threads: 33
mail2saion's Avatar
mail2saion mail2saion is offline Offline
Posting Whiz in Training

Re: format bound field in gridview

 
0
  #7
Apr 19th, 2009
Still not resolved??????????
MARK AS SOLVED if its help you.

REGARDS
MCTS - Shawpnendu bikash maloroy
http://shawpnendu.blogspot.com
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 115
Reputation: ninjaimp is an unknown quantity at this point 
Solved Threads: 6
ninjaimp ninjaimp is offline Offline
Junior Poster

Re: format bound field in gridview

 
0
  #8
Apr 20th, 2009
just try this:

  1. {d:0}

Works for me!
Please mark this post as 'Solved' if it has helped and fixed your issue.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 4
Reputation: nil is an unknown quantity at this point 
Solved Threads: 0
nil nil is offline Offline
Newbie Poster

Re: format bound field in gridview

 
0
  #9
Apr 21st, 2009
Originally Posted by emilio View Post
well i tryed this and it still doesnot work.
Hi

try this example:

  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.
Regards,
Nil
"If you like me raise your hand, if not raise your standard"
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 220
Reputation: mail2saion is an unknown quantity at this point 
Solved Threads: 33
mail2saion's Avatar
mail2saion mail2saion is offline Offline
Posting Whiz in Training

Re: format bound field in gridview

 
0
  #10
Apr 22nd, 2009
MARK AS SOLVED if its help you.

REGARDS
MCTS - Shawpnendu bikash maloroy
http://shawpnendu.blogspot.com
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the ASP.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC