ASP.Net Gridview DataFormatString
In ASP.Net 2.0 GridView data control also provides the functionality to format the data item string using DataFormatString property of BoundColumn. DataFormatString property of GridView BoundColumn allows you to format the DataField value by concatenating a string with it or changing the string format using string formatting syntax e.g.: {0:N} for Number format, {0:C} for currency format, {0:X} for decimal to hexadecimal conversion etc. In this tutorial we have used the product table of Northwind SQL database to display the list of products and unit price of each product is formatted using {0:C} in the DataFormatString property of the BoundColumn. UnitsInStock column of the products table is formatted to number format using {0:N} in DataFormatString property of BoundColumn. You can learn the string format syntax from the tutorial about ASP.Net C# String Format Function
GridView Examples:
You can see the live samples and examples of GridView from the following links:
Code for ASP.Net GridView DataFormatString
CSS Code
<style type="text/css">
body {
font-family: arial;
font-size: 12px;
}
.gridHeader th {
border-bottom: solid 2px #cccccc;
}
.gridRow td {
border-bottom: dashed 1px #c0c0c0;
margin-top: 5px;
}
</style>
HTML Code
<asp:GridView ID="GridView1"
runat="server"
AutoGenerateColumns="False"
GridLines="None"
CellPadding="4"
CellSpacing="0"
HeaderStyle-HorizontalAlign="Left"
Width="400px"
RowStyle-CssClass="gridRow"
CssClass="gridHeader">
<Columns>
<asp:BoundField
DataField="unitsinstock"
HeaderText="Quantity (Default)" />
<asp:BoundField
DataField="unitsinstock"
DataFormatString="{0:N}"
HtmlEncode="false"
HeaderText="Quantity (Number Format)" />
<asp:BoundField
DataField="unitsinstock"
DataFormatString="{0:000}"
HtmlEncode="false"
HeaderText="Quantity (Number Format)" />
<asp:BoundField
DataField="unitprice"
HeaderText="Price (Default)" />
<asp:BoundField
DataField="unitprice"
DataFormatString="{0:C}"
HtmlEncode="false"
HeaderText="Price (Formatted)" />
<asp:BoundField
DataField="unitprice"
DataFormatString="${0:00.00}"
HtmlEncode="false"
HeaderText="Price (Formatted)" />
</Columns>
<RowStyle CssClass="gridRow" />
<HeaderStyle HorizontalAlign="Left" />
</asp:GridView>
From the above example code of GridView Control having DataFormatString property for BoundColumn you can see that HtmlEncode property is used with value false coz DataFormat String property supports the string format syntax only if HtmlEncode="false". There are two other types of string formatting used in the above example: ${0:00.00} it formats the DataField similar to currency format with $ sign and decimal number precision points upto 2 digits. You can also apply the same formatting for currency using {0:C2}, a digit after C in the given format syntax specifies the number of decimal point precisions.
You can get the complete C# Code for Binding the data with GridView control from ASP.Net Gridview Select Row tutorial.
Continue to next tutorial: ASP.Net Gridview DataFormatString Date Format to learn how to format the date string in a BoundField.
