This article shows how to format a GridView. What is GridView? GridView is an extremely flexible grid control for showing data in a basic grid consisting of rows and columns. It has selection, paging, and editing features, and it is extensible through templates. The great advantage of Gridview over Datagrid is its support for code-free scenarios. In GridView, you can do many things without writing any code, such as paging and selection.

Formatting Fields

To format the grid view, you have to ensure that dates, currency, and other number values are in good format. Grid View has the property "DataFormatString" to apply formatting. You can change colors, fonts, borders, and alignment of the grid. Each BoundField column provides a DataFormatString property that you can use to configure the numbers and dates using a format string.

Format strings are generally made up of placeholders and format indicators, which are wrapped inside curly brackets like this.

{0:C}

Here 0 shows the value that will be formatted and the letter indicates a predetermined format style. In this case, C means currency format, which formats a number as a dollar.

<asp:BoundField 
    DataField="Price" 
    HeaderText="Price" 
    DataFormatString="{0:C}" 
    HtmlEncode="false" 
/>

Here we are going to discuss about few format strings, if you want to know in detail then you can search on MSDN.

Numeric Format Strings

Time and Date Format Strings

<asp:BoundField 
    DataField="DOB" 
    HeaderText="DOB" 
    DataFormatString="{0:MM/dd/yy}" 
    HtmlEncode="false" 
/>

Styles

you can set eight GridView styles.

Example

<asp:GridView 
    ID="GridView1" 
    runat="server" 
    AllowPaging="True" 
    ShowFooter="True">

    <RowStyle 
        BackColor="Gray" 
        Font-Italic="True" />
        
    <EmptyDataRowStyle 
        BackColor="Yellow" />
        
    <PagerStyle 
        BackColor="#FFC0C0" 
        Font-Italic="True" 
        Font-Underline="True" />
        
    <SelectedRowStyle 
        BackColor="#00C000" 
        Font-Bold="True" 
        Font-Italic="True" />
        
    <EditRowStyle 
        BackColor="#0000C0" />
        
    <AlternatingRowStyle 
        BackColor="Red" 
        BorderColor="Green" 
        BorderStyle="Dashed" 
        BorderWidth="1px" 
        Font-Bold="True" />
        
    <FooterStyle 
        BackColor="#00C0C0" />
        
    <HeaderStyle 
        BackColor="#00C000" 
        Font-Bold="True" 
        Font-Italic="True" 
        Font-Underline="True" />
        
</asp:GridView>