.Net GridView - ASP GridView - GridView in ASP.Net

.Net GridView (ASP GridView, GridView in ASP.Net) Displays the values of a data source in a table where each column represents a field and each row represents a record. The GridView control allows you to select, sort, and edit these items.

The GridView control is used to display the values of a data source in a table. Each column represents a field, while each row represents a record. The GridView control supports the following features:

*Binding to data source controls, such as SqlDataSource.

*Built-in sorting capabilities.

*Built-in updating and deleting capabilities.

*Built-in paging capabilities.

*Built-in row selection capabilities.

*Programmatic access to the GridView object model to dynamically set properties, handle events, and so on.

*Multiple key fields.

*Multiple data fields for the hyperlink columns.

*Customizable appearance through themes and styles.

<%@ Page language="C#" %>

<html>
  <body>
    <form runat="server">
       
      <h3>GridView Example</h3>

      <asp:gridview id="CustomersGridView"
        datasourceid="CustomersSource"
        autogeneratecolumns="true"
        emptydatatext="No data available."
        allowpaging="true"
        runat="server">               
      </asp:gridview>
           
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSource"
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server"/>
       
    </form>
  </body>
</html>

Binding to Data

The GridView control can be bound to a data source control (such as SqlDataSource, ObjectDataSource, and so on), as well as any data source that implements the System.Collections.IEnumerable interface (such as System.Data.DataView, System.Collections.ArrayList, or System.Collections.Hashtable). Use one of the following methods to bind the GridView control to the appropriate data source type:

*To bind to a data source control, set the DataSourceID property of the GridView control to the ID value of the data source control. The GridView control automatically binds to the specified data source control and can take advantage of the data source control's capabilities to perform sorting, updating, deleting, and paging functionality. This is the preferred method to bind to data.

*To bind to a data source that implements the System.Collections.IEnumerable interface, programmatically set the DataSource property of the GridView control to the data source and then call the DataBind method. When using this method, the GridView control does not provide built-in sorting, updating, deleting, and paging functionality. You need to provide this functionality by using the appropriate event.

Style property

AlternatingRowStyle: The style settings for the alternating data rows in the GridView control. When this property is set, the data rows are displayed alternating between the RowStyle settings and the AlternatingRowStyle settings.

EditRowStyle: The style settings for the row being edited in the GridView control.

EmptyDataRowStyle: The style settings for the empty data row displayed in the GridView control when the data source does not contain any records.

FooterStyle :The style settings for the footer row of the GridView control.

HeaderStyle: The style settings for the header row of the GridView control.

PagerStyle: The style settings for the pager row of the GridView control.

RowStyle: The style settings for the data rows in the GridView control. When the AlternatingRowStyle property is also set, the data rows are displayed alternating between the RowStyle settings and the AlternatingRowStyle settings.

<%@ Page language="C#" %>

<html>
  <body>
    <form runat="server">
       
      <h3>GridView Edit Example</h3>

      <!-- The GridView control automatically sets the columns     -->
      <!-- specified in the datakeynames property as read-only.    -->
      <!-- No input controls are rendered for these columns in     -->
      <!-- edit mode.                                              -->
      <asp:gridview id="CustomersGridView"
        datasourceid="CustomersSqlDataSource"
        autogeneratecolumns="true"
        autogeneratedeletebutton="true"
        autogenerateeditbutton="true"
        datakeynames="CustomerID" 
        runat="server">
      </asp:gridview>
           
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSqlDataSource" 
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)"
        deletecommand="Delete from Customers where CustomerID = @CustomerID"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </asp:sqldatasource>
           
    </form>
  </body>
</html>

Events

PageIndexChanged: Occurs when one of the pager buttons is clicked, but after the GridView control handles the paging operation. This event is commonly used when you need to perform a task after the user navigates to a different page in the control.

PageIndexChanging: Occurs when one of the pager buttons is clicked, but before the GridView control handles the paging operation. This event is often used to cancel the paging operation.

RowCancelingEdit: Occurs when a row's Cancel button is clicked, but before the GridView control exits edit mode. This event is often used to stop the canceling operation.

RowCommand: Occurs when a button is clicked in the GridView control. This event is often used to perform a task when a button is clicked in the control.

RowCreated: Occurs when a new row is created in the GridView control. This event is often used to modify the contents of a row when the row is created.

RowDataBound: Occurs when a data row is bound to data in the GridView control. This event is often used to modify the contents of a row when the row is bound to data.

RowDeleted: Occurs when a row's Delete button is clicked, but after the GridView control deletes the record from the data source. This event is often used to check the results of the delete operation.

RowDeleting: Occurs when a row's Delete button is clicked, but before the GridView control deletes the record from the data source. This event is often used to cancel the deleting operation.

RowEditing: Occurs when a row's Edit button is clicked, but before the GridView control enters edit mode. This event is often used to cancel the editing operation.

RowUpdated: Occurs when a row's Update button is clicked, but after the GridView control updates the row. This event is often used to check the results of the update operation.

RowUpdating: Occurs when a row's Update button is clicked, but before the GridView control updates the row. This event is often used to cancel the updating operation.

SelectedIndexChanged: Occurs when a row's Select button is clicked, but after the GridView control handles the select operation. This event is often used to perform a task after a row is selected in the control.

SelectedIndexChanging: Occurs when a row's Select button is clicked, but before the GridView control handles the select operation. This event is often used to cancel the selection operation.

Sorted: Occurs when the hyperlink to sort a column is clicked, but after the GridView control handles the sort operation. This event is commonly used to perform a task after the user clicks on a hyperlink to sort a column.

Sorting: Occurs when the hyperlink to sort a column is clicked, but before the GridView control handles the sort operation. This event is often used to cancel the sorting operation or to perform a custom sorting routine.

Source : MSDN (GridView Class)
Tags: ,
Hot on Web:


About author