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>
Related
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}") %>' />
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}") %>'
I have a gridview to which I dynamically bind data to.
It features SELECT and EDIT commands.
When it is in EDIT mode all the fields turn into textboxes that can be edited.
What I would like to do is add an Ajax Calendar Extender to one of the textboxes.
How can I create a TemplateField just for the one field?
Is it even possible? I know how to do it if I bound all fields and assign the data via a SqlDataSource.
I have tried adding the templatefield to my gridview but the data is just not showing up:
<asp:GridView ID="gvCheckResults" runat="server" OnRowDataBound="gvCheckResults_RowDataBound"
OnRowEditing="gvCheckResults_RowEditing" OnRowUpdating="gvCheckResults_RowUpdating" OnRowCancelingEdit="gvCheckResults_RowCancelingEdit"
OnRowDeleting="gvCheckResults_RowDeleting">
<Columns>
<asp:TemplateField HeaderText="DateOfTest" SortExpression="DateOfTest">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("DateOfTest") %>'></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="test" runat="server"
TargetControlID="TextBox1"
CssClass="calendar"
Format="dd/MM/yyyy">
</ajaxToolkit:CalendarExtender>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("DateOfTest") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
How the data gets bound:
protected void TestDataBind()
{
string Name = txtCheckName.Text;
if (string.IsNullOrEmpty(Name))
Name = null;
gvCheckResults.DataSource = dataContext.GetRecords(Name);
gvCheckResults.DataBind();
}
I am databinding a gridview and one of the columns is defined as shown below. What I would like to do is to colour the text depending on whether the text says "Yes" or "No". If the text is "Yes" I'd like to set it to red else set it to green. Can this be done and if so should it be done via css or can I add some code to the line?
<asp:TemplateField HeaderText="Validated" ItemStyle-HorizontalAlign="Center" SortExpression="Product">
<ItemTemplate>
<asp:Label ID="lblValidated" runat="server" Text='<%# Bind("Validation") %>' />
</ItemTemplate>
</asp:TemplateField>
The below should do what you want.
ASP.NET
<asp:TemplateField HeaderText="Validated" ItemStyle-HorizontalAlign="Center" SortExpression="Product">
<ItemTemplate>
<asp:Label ID="lblValidated" runat="server" Text='<%# Bind("Validation") %>' CssClass='<%# SetColor(DataBinder.Eval(Container.DataItem, "Validation")) %>' />
</ItemTemplate>
C#
public string SetColor(string Text)
{
return Text.ToUpper == "YES" ? "GreenClass" : "RedClass"
}
CSS
.GreenClass{color:green;}
.RedClass{color:red;}
i think this maybe an easy one...
I have a Gridview, bound to a dataSource with this query:
select userid, name, phone from users
I need to change the content of the Name cell to a link, like so:
User's Name
so OnRowDataBound i'm doing this:
e.Row.Cells[1].Text = "" + e.Row.Cells[1].Text + "";
It all works fine this way. My problem is that i don't want to display the UserId column in the Gridview, but when i attribute it (asp attribute or server-side) Visible="false" to the BoundField UserId, i can't capture it's value to build the Name Cell.
Thanx for your help
Make your gridView to use template items instead of simple grivdiew column.
<asp:TemplateField HeaderText="FirstName" SortExpression="FirstName">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("FirstName")%>'>
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("FirstName") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
You can find a nice intro Here
Agree with Jani, this should be done with a template field. If you really need to do it the other way, see Can data be kept in a dynamically bound GridView's invisible fields?.
you can set the visibility on the onrowdatabound event