ASP.NET GridView Events

Introduction – In this article we will see different types of events of Asp.net GridView. We use Gridview to show data in tabular format which consist inline editing, sorting, indexing and many features.

Description – Article consists of all events with small examples, with this article you will learn how to work with GridView and their events. You can bind your Datasource to your GridView by writing – GridView1.Datasource = “Datasource” which you will bring using Ado.net Datatable or DataSet. To use SQL Related code then you will need this namespace System.Data.SqlClient

  1. OnRowDataBound

This event is termed once the information is guaranteed to the row. Dropdown lists are connected to the information row. If it’s the information management row then the chosen choice is about adequate the worth within the info.

Here the worth for the DataKeyNameCollection is employed. The second price, the one we tend to attack the DataKeyNames property of the GridView.

For footer row the dropdown list connected.

Example with Code –

OnRowDataBound using Asp.net

  1. OnRowUpdating

This event permits the capture of the update event and information update. Here the row index of the change row is captured. Then the controls therein row and therefore the values within them is retrieved and pass to update methodology in the category file.

Note: The Key price for the info is extracted from the DataKeysCollection.

Example with Code –

OnRowUpdating using Asp.net

  1. OnRowEditing

This event is dismissed as presently as user clicked the edit button, then the edit index for that row is about to adequate the row index of the change row. This price is came back by the event arguments.

Example with code –

OnRowEditing using Asp.net on OfficialCoder.com

  1. OnRowCancelingEdit-

This event is dismissed once the user cancels the row update. Here the choice of the row for redaction is removed.

Example with Code –

OnRowCancellingEdit using Asp.net on OfficialCoder

  1. OnRowCommand-

This event is dismissed once the user clicks the ‘Add Record’ button, the command dismissed is ‘Add_Rec’ because it is defined in Grid, see the footer row. The name is captured from the event arguments. The management within the footer row are placed and their values is extracted and sent for insert perform.

Example with Code –

OnRowCommand using Asp.net on OfficialCoder

  1. OnRowDeleting

This perform is termed once the record is deleted, the key for the record is fetched from the datakey assortment and passed to the delete perform.

Example with Code –

OnRowDeleting using Asp.net on OfficialCoder

  1. OnSorting-

This event is dismissed once the kind button is clicked by the user. The kind expression is retrieved from the event arguments and place within the session in order that they are retrieved in the fill_grid() methodology and used for sorting.

Example with Code –

OnSorting using Asp.net on OfficialCoder7

  1. OnPageIndexChanging

This perform is termed once user clicks completely different paging than current page number. The grid’s page index is about to the present new paging. Whereas loading grid mechanically displays the requested page.

Whenever user clicks the various paging the full set of information is retrieved from the info and loaded into the grid. Suppose the whole requested information is one thousand records and therefore the grid page size is ten records, then solely ten records are presented the user whereas remainder of the records are of no use as a result of if user clicks the various paging once more, whole one thousand records shall be fetched once more. This method is not terribly economical. However, it is the default operating of the paging operation. There are some ways to boost this method which can need another massive article which can overshadow the aim of this text therefore straightforward default operation is employed here.

Example with Code –

OnPageIndexChanging using Asp.net on OfficialCoder7

  1. Page_Load() this can be the primary important event for the user once that he is able to see the page prepared for his action. Here we tend to are job the fill_grid() methodology.

Fill_Grid(): A tiny low trick is enforced during this perform, common operation of obtaining the DataTable from the info is enforced. Besides that we tend to are checking the kind order of the grid if it is gift within the Session directory. If it is not then sorting order is employed, but if the kind order is gift then it is used. If new sorting order is chosen then that is saved within the session, (check the grid1_OnSorting() function) and is retrieved and used whereas filling the grid each time. This manner the kind order is preserved for the session, and can be the chosen type order even once insert/update/delete operations.

Also just in case there is no information came back then grid would not be displayed during this situation we will use the label to inform the user concerning it. However, it is the best resolution however some logic can be added to permit user to enter information if acceptable, there are some ways to try to it those is also business would like dependent and specific to every necessities and are not coated during this article.

Methods utilized in the category file:

I am victimization the functions to be static in order that the mental representation of the category is not needed, we will decision the perform by the the class_name.function_name() convention.

  1. FetchProducts(): It merely retrieves the information from the database into the datatable and returns this datatable to caller.
  2. FetchCategory(): It merely returns the full class file information into the datatable and returns this datatable to the caller.
  3. Update(): It updates the record, it gets all the sphere values from the job perform as parameters. These values are inserted into the command with the assistance of SqlParameters. ExecuteNonQuery() is employed and therefore the range of affected rows are came back to the job perform.
  4. InsertRow(): It adds a replacement record in info. It gets all the sphere values from the job perform as parameters.These values are inserted into the command with the assistance of SqlParameters.ExecuteNonQuery() is employed and therefore the range of affected rows are came back to the job perform.
  5. RowDelete(): It deletes the required row from the info. The key price is received through the parameters. With the assistance of SqlParameters and therefore the ExecuteNonQuery() the record is deleted and affected rows are came back to the job perform.

Leave a Reply

Your e-mail address will not be published. Required fields are marked *