Configuring the GridView and DetailsView Controls

Configuring the GridView and DetailsView Controls

Configuring the GridView and DetailsView controls in ASP.NET is relatively simple. Here are some common configurations:

Displaying data with the GridView control:

<asp:GridView ID=”GridView1″ runat=”server” AutoGenerateColumns=”False” DataSourceID=”SqlDataSource1″>

   <Columns>

      <asp:BoundField DataField=”ProductID” HeaderText=”Product ID” />

      <asp:BoundField DataField=”ProductName” HeaderText=”Product Name” />

      <asp:BoundField DataField=”UnitPrice” HeaderText=”Unit Price” DataFormatString=”{0:C}” />

   </Columns>

</asp:GridView>

This code defines a GridView control with an ID of “GridView1” that retrieves data from the SqlDataSource control with the ID of “SqlDataSource1”. The AutoGenerateColumns property is set to “False” to specify that the columns are defined manually. The Columns collection specifies the columns to display, which are bound to the “ProductID”, “ProductName”, and “UnitPrice” fields of the SqlDataSource control. The DataFormatString property of the “UnitPrice” field is set to “{0:C}” to format the unit price as a currency value.

Displaying data with the DetailsView control:

<asp:DetailsView ID=”DetailsView1″ runat=”server” AutoGenerateRows=”False” DataSourceID=”SqlDataSource1″>

   <Fields>

      <asp:BoundField DataField=”ProductID” HeaderText=”Product ID” />

      <asp:BoundField DataField=”ProductName” HeaderText=”Product Name” />

      <asp:BoundField DataField=”UnitPrice” HeaderText=”Unit Price” DataFormatString=”{0:C}” />

   </Fields>

</asp:DetailsView>

This code defines a DetailsView control with an ID of “DetailsView1” that retrieves data from the SqlDataSource control with the ID of “SqlDataSource1”. The AutoGenerateRows property is set to “False” to specify that the fields are defined manually. The Fields collection specifies the fields to display, which are bound to the “ProductID”, “ProductName”, and “UnitPrice” fields of the SqlDataSource control. The DataFormatString property of the “UnitPrice” field is set to “{0:C}” to format the unit price as a currency value.

Sorting and paging data with the GridView control:

<asp:GridView ID=”GridView1″ runat=”server” AutoGenerateColumns=”False” DataSourceID=”SqlDataSource1″ AllowSorting=”True” AllowPaging=”True”>

   <Columns>

      <asp:BoundField DataField=”ProductID” HeaderText=”Product ID” SortExpression=”ProductID” />

      <asp:BoundField DataField=”ProductName” HeaderText=”Product Name” SortExpression=”ProductName” />

      <asp:BoundField DataField=”UnitPrice” HeaderText=”Unit Price” DataFormatString=”{0:C}” SortExpression=”UnitPrice” />

   </Columns>

</asp:GridView>

This code defines a GridView control with an ID of “GridView1” that retrieves data from the SqlDataSource control with the ID of “SqlDataSource1”. The AllowSorting property is set to “True” to enable sorting of columns. The AllowPaging property is set to “True” to enable paging of data. The BoundField controls for each column include a SortExpression property that specifies the field to sort by.

These are just a few examples of how to configure the GridView and DetailsView controls in ASP.NET. There are many other properties and options available, so be sure to check the documentation for more information.

Apply for ASP.NET Certification Now!!

https://www.vskills.in/certification/certified-aspnet-programmer

Back to Tutorial

Get industry recognized certification – Contact us

Menu