Date format without time in ASP.NET Gridview - c#

in ASP.NET gridview binding two dates. I want to display dd/MM/yyyy but it displays 10/03/2014 00:00:00.
<asp:TemplateField HeaderText ="Fromdate" >
<ItemTemplate >
<asp:Label ID="lblFromDate" runat="server"
DataFormatString="{0:dd/MM/yyyy}"
HtmlEncode="false"
Text='<%# Eval("Fromdate") %>' />
</ItemTemplate>
</asp:TemplateField>

DataFormatString is a property of BoundField control and doesn't affect any other control. You can specify the format in an Eval expression:
Text='<%# Eval("Fromdate", "{0:dd/MM/yyyy}") %>' />

According to this article on MSDN the DataFormatString attribute has a limited number of variants for DateTime data.
What you are looking for is:
DataFormatString="{0:d}"
which is the short date pattern.
In order to get the dd/MM/yyyy format, you need to also set your culture info properly (for example to **en-GB**).

I achieved it with {0:dd/MM/yyyy} on the DataFormatString property of the field
It works well

DataFormatString is a property of BoundField control and doesn't affect any other control. You can specify the format right in the Eval expression:
Text='<%# Eval("Fromdate", "{0:dd/MM/yyyy}") %>' />

<asp:TemplateField HeaderText="Fromdate">
<ItemTemplate>
<asp:Label ID="lblFromdate" runat="server"
Text='<%#Eval("Fromdate", "{0:dd/MM/yyyy}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

Blockquote
Eval("Date","{0:dd-MM-yyyy}") %>' />

Related

change date format while binding the data to the gridview

I want to change the format of the date while binding the value of the date to the gridview (02-oct-2019) like this
I have tried this :
Text='<%# Eval("InsertDateTime","{0:dd-mmm-yyy}") %>'
"this label is inside the gridview"
<asp:Label ID="lblInsertDateTime" runat="server" Text='<%# Eval("InsertDateTime","{0:dd-mmm-yyy}") %>'></asp:Label>

How to present year instead of date in Hyperlink control in webForms

I need to show year instade of date string in a HyperLinkField in asp.net WebForms
<asp:HyperLinkField HeaderText="Economic year" DataTextField="Date" SortExpression="Date"DataNavigateUrlFields="Id" DataNavigateUrlFormatString="P_ProfitLossADD.aspx?Id={0}"></asp:HyperLinkField>
but HyperLinkField does not take DataFormatString="{0:d} as property of the control .
<asp:HyperLinkField HeaderText="Economic year" DataTextField="Date" DataTextFormatString="{0:YYYY}" SortExpression="Date"DataNavigateUrlFields="Id" DataNavigateUrlFormatString="P_ProfitLossADD.aspx?Id={0}"></asp:HyperLinkField>
Got it you can just use
<asp:TemplateField HeaderText="Economic year">
<ItemTemplate>
<asp:HyperLink ID="Id" runat="server" NavigateUrl='<%# Eval("Id", "P_ProfitLossADD.aspx?id={0}") %>'
Text='<%# Eval("Date","{0:yyyy}") %>'></asp:HyperLink>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" Wrap="False" />
</asp:TemplateField>

How to set DataFormatString to Textbox in ASP.NET?

I am using Datagrid and I have bound column as below
<asp:BoundColumn DataField="Salary" HeaderText="Salary" DataFormatString="{0:#,##0.00}">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
I want to use above dataformat string for below TextBox.
<asp:TextBox DataFormatString="{0:#,##0.00}" ID="textbox1" runat="server" MaxLength="150"></asp:TextBox>
If I try to set Dataformatstring to TextBox it is not working. How can I add dataformatstring to TextBox in ASP.NET or Javascript code in order to to same issue?
If you set the TextBox.Text value in code behind, you can do like this:
textBox1.Text = string.Format("{0:#,##0.00}", yourValue);
Check it on dotnetfiddle
If you set in aspx side, you can with Eval:
<asp:TextBox Text='<%# Eval("myField", "{0:#,##0.00}") %>' ID="textbox1" runat="server" MaxLength="150"></asp:TextBox>

show just the date when bind a datetime Field in label in repeater

i have a repeater in my asp.net page and bind a data source to this repeater
there is a label in this repeater and the text of label is a datetime field
i want that label only show me the date not date and time
<asp:Label ID="lblDate" runat="server" Text='<%# Eval("PostDate") %>'></asp:Label>
this code show date and time together
add this
DataFormatString="{0:dd/MM/yyyy}"
<asp:Label ID="lblDate" runat="server" Text='<%# Eval("PostDate") %>' DataFormatString="{0:dd/MM/yyyy}" ></asp:Label>
change the Eval with format as below
Text='<%# Eval("PostDate", "{0:dd/MM/yyyy}") %>'

format decimal value in gridview

I have a bound field in my Gridview which is getting its value from a database table.
I have got the data but don't know how to format it inside the gridview.
For example I get total data from below like "123456", but I want to display as "123,456"
<asp:BoundField DataField="totaldata" HeaderText="Total Data"
ReadOnly="True" SortExpression="totaldata" />
How can I do this? Do I need to convert the bound field into a template field ? But what do i do after that.
please help.
I have used DataFormatString="{0:n0}" and it solved the above problem.
how do i do for this:
<asp:TemplateField HeaderText="Failed Files"
SortExpression="NumFailed">
<ItemTemplate>
<asp:Image ID="Image2" runat="server" ImageUrl="~/NewFolder1/warning_16x16.gif" />
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "GetFilesFailed.aspx?id="+Eval("MachineID") %>' Text='<%# Bind("NumFailedFiles") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
the hyperlink has the number which need to be formatted...
Use DataFormat property :
<asp:BoundField DataField="totaldata" HeaderText="Total Data"
ReadOnly="True" SortExpression="totaldata" DataFormatString="{0:n3}" />
EDIT : For the second part of your question use Eval method's second parameter to format your data :
<%# Eval("NumFailedFiles", "{0:n3}") %>
Then your template will be like that :
<asp:TemplateField HeaderText="Failed Files"
SortExpression="NumFailed">
<ItemTemplate>
<asp:Image ID="Image2" runat="server"
ImageUrl="~/NewFolder1/warning_16x16.gif" />
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# "GetFilesFailed.aspx?id="+Eval("MachineID") %>'
Text='<%# Eval("NumFailedFiles", "{0:n3}") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
A couple of ways in how you can do that
Option 1
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "GetFilesFailed.aspx?id="+Eval("MachineID") %>' Text='<%# Bind("NumFailedFiles","{0:n0}") %>'></asp:HyperLink>
Option 2
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "GetFilesFailed.aspx? id="+Eval("MachineID") %>' Text='<%# Bind("NumFailedFiles").ToString("N") %>'></asp:HyperLink>
Option 3
Create a method in the code behind page that returns the formatted number.
protected string GetFormatedNumber(object number)
{
if ( number != null )
{
return number.ToString("N");
}
return "0";
}
And call the method in your aspx page as below:
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "GetFilesFailed.aspx? id="+Eval("MachineID") %>' Text='<%#GetFormatedNumber(Eval("NumFailedFiles")) %>'></asp:HyperLink>
I think you need to take a look at this MSDN article on How to Format Data in the DataGridView
if you want to format your data on gridview use "{0:n3}"
<asp:Label ID="label" runat="server" Visible="true" Text='<%#DataBinder.Eval(Container.DataItem, "data","{0:n3}") %>'></asp:Label>

Categories