asp.net c# tutorials and examples code for web developers

Thursday, December 4, 2008

How to use BoundFiled in GridView

GridView BoundFiled
GridView is an asp.net server control that can display the values of a data source in a table. AutoGenerateColumns property allow us to automatically generate columns for gridview control. asp.net developers can manually specify which column fields appear in gridview by setting AutoGenerateColumns property value to False. if developers want to manually control the gridview columns then they need to define their own column field collection and column field types.

asp.net developers can use the following column field types in a gridview those are BoundFiled, ButtonField, CheckBoxField, CommandField, HyperLinkField, ImageField and TemplateField.

BoundField is the default column type of the gridview server control. BoundField display the value of a field as text in gridview. gridview control display a BoundField object as a column. developers can specify the field to display in a BoundField object by setting its DataField property.

BoundField's value can be HTML encoded before display in web browser by setting its HtmlEncode property value to True. we also can apply a custom formatting string to the field's value by setting BoundField object's DataFormatString property. when a field's value is null, developers can display a custom caption by setting a text for NullDisplayText property value. ConvertEmptyStringToNull property allow us to automatically convert empty string field values to null values.

we can control a BoundField object's visibility by setting its Visible property. we can disable editing of a BoundField in edit mode by setting its ReadOnly property value to True. we can hide a BoundField object in gridview's insert mode by setting its InsertVisible property value to False. we can customize a BoundField's header section by HeaderText property. HeaderImageUrl property allow us to display an image in BoundField header section.

the following asp.net c# example source code demonstrate us how can we use BoundField in gridview server control.
BoundFieldExample.aspx
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>asp.net BoundField example: how to use asp:BoundFiled</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Navy">BoundField Example</h2>
        <asp:SqlDataSource
             ID="SqlDataSource1"
             runat="server"
             ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
             SelectCommand="SELECT TOP 5 ProductID, ProductName, UnitPrice FROM Products"
             >
        </asp:SqlDataSource>
        <asp:GridView
             ID="GridView1" 
             runat="server"
             DataSourceID="SqlDataSource1"
             AutoGenerateColumns="false"
             BackColor="Cornsilk"
             ForeColor="DarkOrchid"
             BorderColor="DarkSlateBlue"
             >
            <Columns>
                <asp:BoundField HeaderText="Product ID" DataField="ProductID" />
                <asp:BoundField HeaderText="Product Name" DataField="ProductName" />
                <asp:BoundField HeaderText="Unit Price" DataField="UnitPrice" />
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>
Related asp.net examples