Using CommandField in a GridView control
Using a <CommandField> element, we can display one or more command buttons in a column of the GridView control. When we use the <CommandField> element tag, command buttons are included in each row of a GridView control. As shown in the picture, the <CommandField> column displays Delete, Edit, Update and Cancel buttons.Using the below code, the <CommandField> column displays Delete, Edit, Update and Cancel buttons.
Note: Update and Cancel buttons are displayed only when a user clicks on the Edit button to edit a row. Although a single <CommandField> can display more than one button, it is recommended to create separate command fields for Select, Edit and Delete buttons. The ButtonType property specifies the type of button to be displayed in the GridView control. The valid options are Button, Link or Image.
In the code given below, we declare a GridView control and set the AutoGenerateColumns property to 'false' and then add <CommandField> element within the <Columns> element.
<asp:CommandField> element to display buttons in each row of the GridView control. Observe that the ButtonType property of CommandField is set as a hyperlink.
<asp:GridView ID="GridView1" DataSourceId="MyDataSource" DataKeyNames="Code" AutoGenerateColumns="false" runat="server"> <Columns> <asp:CommandField ButtonType="Link" ShowEditButton="true" ShowDeleteButton="true" ShowCancelButton="true" /> <asp:BoundField DataField="Code" HeaderText="Code" /> <asp:BoundField DataField="Name" HeaderText="Name" /> <asp:BoundField DataField="Description" HeaderText="Description" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="MyDataSource" ConnectionString="<%$Connectionstrings:ERPConnectionString%>" SelectCommand="SELECT * FROM Sample" UpdateCommand="Update SAMPLE SET Name=@Name,Description=@Description Where Code=@Code" DeleteCommand="Delete SAMPLE Where Code=@Code" runat="server"/>
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 Boundfields to a 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
Details-View
GridView and DetailsView Master/Detail page using SqlDataSource control
POCO
POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
Entity Framework
Query entity data model using linq to entities
Array List
Difference between arraylist and list collection
Web Services
How to create a Web service using Visual Studio.net
Form-View
FormView DataBound Event
Object Oriented Programming
Calling base class constructor in C#
Linq
Convert a sequence to a generic list using ToList()method
ERP
ERP Software Development
Project Ideas
Project ideas for students
AccountingSoftware
Accounting Software
MVC
Creating an ASP.Net MVC 3 application
.Net
Using assemblies in .net applications
ASP .Net
How to implement form validation using ASP.Net 2.0 Validation Controls
VB .Net
Constructors in Visual Basic.Net
People also viewed:
Create your own ERP Software using ASP Core>>