DetailsView fields

By default, the DetailsView control displays data in each field in a separate HTML table row. Each row corresponds to a field in the data source. We can bind the DetailsView control to the data source either by declaratively or programmatically.

By default, all the fields in the data source are displayed automatically because the AutoGenerateRows property value is set to true.

If we want more control over the display of fields in the detailsView control, first, we need to set the AutoGenerateRows property value to false. Next, we need to declare <Fields> element in the DetailsView control.

Using <Fields> element with the DetailsView control, we can define individual fields to be displayed by the control. The <Fields> element is similar to the element of the GridView control.

Within the <Fields> element we can declare the following different type of fields corresponding to the type of fields to be displayed by the DetailsView control.

The DetailsView control supports the following types of fields.

BoundField  		- 	Displays the value of a data item as text 
CheckBoxField  		- 	Displays the value of a data item as check box
CommandField 		- 	Displays links for editing, deleting and selecting 
ButtonField 		- 	Displays the value of a data item as a button  
HyperLinkField 		- 	Displays the value of a data item as link
ImageField			-	Displays the value of a data item as image 
TemplateField		-	Enables customization of the appearance of a data item


The below code demonstrates how to add BoundField element to the Fields section of the DetailsView control.

  <asp:DetailsView ID="DetailsView1" runat="server"   
   AllowPaging="true" DataKeyNames="Code" 
   DataSourceID="MyDataSource"    AutoGenerateRows="false" 
   AutoGenerateInsertButton="true" 
   AutoGenerateEditButton="true">
   <Fields>
   
   <asp:BoundField   DataField="Name" HeaderText="Name" />
   <asp:BoundField  DataField="description" HeaderText="description" />
   
   </Fields>
   
   </asp:DetailsView>
<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" 
InsertCommand="Insert SAMPLE(Code,Name,description) VALUES(@Code,@Name,@description)"
runat="server"/>


output of DetailsView with fields