Adding boundfields to a GridView control
To establish control over displaying columns in a GridView control, we have to set the AutoGenerateColumns property to 'false' and define a BoundField object for each column to be displayed in the GridView.Using BoundFields, we can customize the header text by setting the HeaderText property. To format the data displayed in the GridView column we use DataFormatString property of BoundField.
When we use a BoundField for a column, the value of a data item is displayed as text. We need to use one <BoundField> only, for each column. The <asp:BoundField> tag has three important properties.
Property | Description |
---|---|
DataField | Specifies the name of the field that the boundfield displays |
HeaderText | Displays text in the column header |
DataFormatString | Specifies the format string to format a data item |
Other properties we can use with <BoundField> column are:
Property | Description |
---|---|
FooterStyle | Enables footer column formatting |
FooterText | Enables text display in the column footer |
HeaderStyle | Enables header column formatting |
HeaderText | Enables text display in the column header |
ItemStyle | Enables to format a data item |
You can find a lively discussion on How to create custom bound fields in GridView - here
Steps to create a <BoundField> column are:
The first step is to declare a GridView control and set the AutoGenerateColumns property to 'false'. Next, create a <asp:BoundField> element within the <Columns> element. Define a <asp:BoundField> element for each column we want to display as text. Specify the name of the field we want to display in the DataField property.
<asp:GridView ID="GridView1" DataSourceId="MyDataSource" DataKeyNames="AccountCode" ShowFooter="true" AutoGenerateColumns="false" AutoGenerateEditButton="true" AutoGenerateDeleteButton="true" runat="server"> <Columns> <asp:BoundField DataField="AccountCode" HeaderText="Account Code"> <ItemStyle Font-Size="Large" /> </asp:BoundField> <asp:BoundField DataField="AccountName" HeaderText="Account Name" FooterText="Enter Footer Text"> <FooterStyle CssClass="FooterStyle" /> </asp:BoundField > <asp:BoundField DataField="AccountDescription" HeaderText="Description" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="MyDataSource" ConnectionString="<%$Connectionstrings:ERPConnectionString%>"
GridView Articles
- GridView AutoGenerateColumns property
- Bind GridView control with SqlDataSource control
- DataBinding in a GridView control
- Binding a GridView programmatically with dataset
- Binding a GridView programmatically with Generic List Collection
- Using fields with the GridView control
- Adding button fields to a GridView control
- Create Templatefields in a GridView control
- Access a value using SelectedIndexChanged() event when a row in a GridView is selected
- How to restrict an item from being selected using GridView SelectedIndexChanging Event
- How to add a new row in the GridView control by using the GridView footer template
- Gridview datakeynames
- How to select a particular row in a GridView control using SelectedDataKey method
- Formatting GridView control with CSS
- GridView rowDataBound event
Become an Expert
Learn More
Q1. What is Azure Platform?
GridView CommandField example
GridView and DetailsView Master/Detail page using SqlDataSource control
POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
Query entity data model using linq to entities
Difference between arraylist and list collection
How to create a Web service using Visual Studio.net
FormView DataBound Event
Calling base class constructor in C#
Convert a sequence to a generic list using ToList()method
ERP Software Development
Project ideas for students
Accounting Software
Creating an ASP.Net MVC 3 application
Using assemblies in .net applications
How to implement form validation using ASP.Net 2.0 Validation Controls
Constructors in Visual Basic.Net
GridView CommandField example
GridView and DetailsView Master/Detail page using SqlDataSource control
POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
Query entity data model using linq to entities
Difference between arraylist and list collection
How to create a Web service using Visual Studio.net
FormView DataBound Event
Calling base class constructor in C#
Convert a sequence to a generic list using ToList()method
ERP Software Development
Project ideas for students
Accounting Software
Creating an ASP.Net MVC 3 application
Using assemblies in .net applications
How to implement form validation using ASP.Net 2.0 Validation Controls
Constructors in Visual Basic.Net