c# tutorials and examples code for beginners

How to use BoundFiled in GridView

GridView BoundFiled
GridView is an 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. 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. 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 c# example source code demonstrate us how can we use BoundField in gridview server control.
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<script runat="server">


<html xmlns="">
<head runat="server">
    <title> BoundField example: how to use asp:BoundFiled</title>
    <form id="form1" runat="server">
        <h2 style="color:Navy">BoundField Example</h2>
             ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
             SelectCommand="SELECT TOP 5 ProductID, ProductName, UnitPrice FROM Products"
                <asp:BoundField HeaderText="Product ID" DataField="ProductID" />
                <asp:BoundField HeaderText="Product Name" DataField="ProductName" />
                <asp:BoundField HeaderText="Unit Price" DataField="UnitPrice" />
Related example