Event Description
OnRowDataBound event function grid1_OnRowDataBound() is called: This event is called when the data is bound to the row. Dropdown lists are attached to the data row. If it’s the data control row then the selected option is set equal to the value in the database.
Here the value for the DataKeyNameCollection is used. The second value, the one we set on the DataKeyNames property of the Gridview.
For footer row the dropdown list attached.
OnRowUpdating event function grid1_OnRowUpdating() is called: This event allows the capture of the update event and data update. Here the row index of the updating row is captured. Then the controls in that row and the values in them is retrieved and pass to update method in the class file.
Note: The Key value for the database is extracted from the DataKeysCollection.
OnRowEditing event function grid1_OnRowEditing() is called: This event is fired as soon as user clicked the edit button, then the edit index for that row is set that is equal to the row index of the updating row. This value is returned by the event arguments.
OnRowCancelingEdit event function grid1_OnRowCancelingEdit() is called: This event is fired when the user cancels the row update. Here the selection of the row for editing is removed.
OnRowCommand event function grid1_OnRowCommand() is called: This event is fired when the user clicks the ‘Add Record’ button, the command fired is ‘Add_Rec’ as it is defiend in Grid, see the footer row. The name is captured from the event arguments. Ihe control in the footer row are located and their valus is extracted and sent for insert function.
OnRowDeleting event function grid1_OnRowDeleting() is called: This function is called when the record is deleted, the key for the record is fetched from the datakey collection and passed to the delete function.
OnSorting event function grid1_OnSorting() is called: This event is fired when the sort button is clicked by the user. The sort expression is retrieved from the event arguments and put in the session so that they are retrieved in the fill_grid() method and used for sorting.
OnPageIndexChanging event function grid1_OnPageIndexChanging() is called: This function is called when user clicks different page number than current page number. The grid’s page index is set to this new page number. While loading grid automatically displayes the requested page.
Whenever user clicks the different page number the whole set of data is retrieved from the database and loaded into the grid. Suppose the complete requested data is 1000 records and the grid page size is 10 records, then only 10 records will be displayed to the user while rest of the records are of no use as because if user clicks the different page number again, whole 1000 records shall be fetched again. This process is not very efficient. However, it is the default working of the paging operation. There are many ways to improve this process which may require another big article which will overshadow the purpose of this article so simple default operation is used here.
Page_Load() this is the first significant event for the user after which he is able to see the page ready for his action. Here we are calling the fill_grid() method.
Fill_Grid(): A small trick is implemented in this function, common operation of getting the DataTable from the database is implemented. Besides that we are checking the sort order of the grid if it is present in the Session directory. If it is not then no sorting order is used, however if the sort order is present then it is used. If new sorting order is selected then that is saved in the session, (check the
grid1_OnSorting() function) and is retrieved and used while filling the grid every time. This way the sort order is preserved for the session, and will be the selected sort order even after insert/update/delete operations.
Also in case there is no data returned then grid will not be displayed in this scenario we can use the lable to tell the user about it. However, it is the easiest way out but some logic can be added to allow user to enter data if appropriate, there are many ways to do it those may be business need dpendent and specific to each requirements and are not covered in this article.
Methods used in the class file:
I am using the functions to be static so that the instantiation of the class is not required, we can call the function by the the
class_name.function_name() convention.
FetchProducts(): It simply retrives the data from the dabase into the datatable and retunrs this datatable to caller.
FetchCategory(): It simply returns the whole category file data into the datatable and returns this datatable to the caller.
Update(): It updates the record, it gets all the field values from the calling function as parameters. These values are inserted into the command with the help of SqlParameters. ExecuteNonQuery() is used and the number of affacted rows are returned to the calling function.
InsertRow(): It adds a new record in database. It gets all the field values from the calling function as parameters.These values are inserted into the command with the help of SqlParameters. ExecuteNonQuery() is used and the number of affacted rows are returned to the calling function.
RowDelete(): It deletes the desired row from the database. The key value is received through the parameters. With the help of SqlParameters and the ExecuteNonQuery() the record is deleted and affacted rows are returned to the calling function.
No comments:
Post a Comment