Search code examples
asp.netgridviewdataformat

data format string in grid view asp.net c#


Is there some way I can use a data format string in a bound field which appends % to my value ?

Example :

For rupee we can do this :

  <asp:BoundField DataField="PassPercent" ItemStyle-Width="7%" HeaderText="Pass Percent" DataFormatString="{0:c}"

I tried using a template field too but that didn't work :

<asp:TemplateField HeaderText="Pass Percent" ItemStyle-Width="5%" >
                        <ItemTemplate>
                            <asp:Label runat="server" DataValueField="PassPercent"  DataTextField="PassPercent" />
                            <asp:Label Text="%" runat="server" />
                        </ItemTemplate>
                 </asp:TemplateField>

Solution

  • Try this:-

    DataFormatString="{0:p}"
    

    But, please note percentages are stored as decimals in this case so you need to adjust your values accordingly. Check the formatting's here on MSDN.

    Or you can simply hard-code it:-

    DataFormatString="{0}%"