ASP.Net Gridview DataFormatString

by top54u.com 18 Jul, 2008

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

 

Code for ASP.Net GridView DataFormatString

 

<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>

 

<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.

 

Output:

 

You can download the C# code to bind GridView Control with Northwind SQL Database at:

ASP.Net GridView Highlight Row onmouseover

Spotlight.....

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , , , , , ,

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

10/11/2008 3:44:00 AM




related videos.....
recent posts.....
top54u ezines.....