在gridview中设置日期格式

时间:2015-08-14 13:50:30

标签: c# asp.net gridview

我想在abbreviated中显示月份的gridview名称,例如11/Aug/2015

<asp:GridView ID="Gridsrev" runat="server" CssClass="table table-bordered  text-nowrap" AutoGenerateColumns="False"    OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
     <Columns>
        <asp:CommandField ButtonType="Button" ControlStyle-CssClass="btn btn-primary" HeaderText="Select" SelectText="Preview" ShowSelectButton="True"></asp:CommandField>
        <asp:BoundField DataField="S.R.NO" HeaderText="S.R.NO" SortExpression="S.R.NO"></asp:BoundField>
        <asp:BoundField DataField="Agrement ID" HeaderText="Agrement ID" SortExpression="Agrement ID"></asp:BoundField>
        <asp:BoundField DataField="CUST_ID" HeaderText="CUST ID" SortExpression="CUST_ID"></asp:BoundField>
        <asp:BoundField DataField="Customer" HeaderText="Customer" SortExpression="Customer"></asp:BoundField>
        <asp:BoundField DataField="Service Type" HeaderText="Service Type" SortExpression="Service Type"></asp:BoundField>
        <asp:BoundField DataField="Service Date" HeaderText="Service Date" SortExpression="Service Date" DataFormatString="{0:dd/MMM/yyyy}"></asp:BoundField>
        <asp:BoundField DataField="Next Service" HeaderText="Next Service" SortExpression="Next Service" DataFormatString="{0:dd/MMM/yyyy}"></asp:BoundField>
    </Columns>

3 个答案:

答案 0 :(得分:1)

如果我不想要你需要为你的 BoundField 设置 htmlEncode = false DataFormatString 它将禁用html的编码并使用您的DataFormatString

答案 1 :(得分:1)

只需添加htmlEncode并将其设置为true即可。试试这个:

    <asp:BoundField DataField="Service Date" HeaderText="Service Date" SortExpression="Service Date"  HtmlEncodeFormatString="true" DataFormatString="{0:dd/MMM/yyyy}"></asp:BoundField>
    <asp:BoundField DataField="Next Service" HeaderText="Next Service" SortExpression="Next Service"  HtmlEncodeFormatString="true" DataFormatString="{0:dd/MMM/yyyy}"></asp:BoundField>

答案 2 :(得分:0)

我刚试过在我的ASP.Net GridView中使用你的DataFormatString,它将日期格式改为你想要的:

<asp:BoundField DataField="StartDate" DataFormatString="{0:dd/MMM/yyyy}"  HeaderStyle-Width="100px" />

这产生了以这种格式的日期列:

11.Jan.2013

(我的语言设置有句号作为日期分隔符。)

这可能是您数据的问题?