In this Post I am going to explain a simple light weight GridView Edit, Delete, and Update functionality. We are going to apply this functionality on Northwind Database, Employee table.
Following are the screen shots.
First of all I am going to explain about creating a Connection String.
Connection String has mainly 3 properties.
Integrated Security will access the SQL Server with the current windows identity established on the Operating System. For more information about Integrated Security refer this. We usually place the connection string in the Web.config file under “Configuration ==> ConnectionStrings” section.
Here is my connection string.
To learn more about different type of connection strings refer this.
Then, after we are done with your connection string in the Web.config file let’s move on to the C# code.
The Following Snippet is to bind GridView.
The First Line using statement allows the programmer to specify when objects that use resources should release them. The object provided to the using statement must implement the IDisposable interface. This interface provides the Dispose method, which should release the object’s resources. Refer this for Complete Reference.
The following is how we write asp.net GridView web server control code in aspx page. I guess most of the people will be aware of the GridView events. For complete Reference refer this.
In the Code you can see I put AutoGenerateColumns=”false”. That means I have to write column names whatever I want to show to the user. If we put AutoGenerateColumns=”True”, that means u don’t have to write any custom code on your page and user can see each and every column in your dataset or datatable whatever you are binding to your gridview.
It is always good to have some control on these instead of giving up everything to it. I want to hide EmployeeID for the user. But I need it for querying the databse while updating and deleting the user stuff. So
How can we hide a column in a GridView and access it in the Code behind ?
We can do this in several ways!!! , But depends on what column we want to hide. I always follow this method.
Writing a Template Field and making it invisible. In the template field I will Place a label or some controls, So that it’s very easy to access those controls in the code behind by using FindControl method.
Suppose if we are trying to hide primary key of our table, there is actually no need to hide. GridView has a Property called DataKeyNames. We have to give our primary key column name to it. It will do the rest. we can see how we can access that in the code behind in the GridView_Rowupdating Event which I am going to explain later part of this article.
What is TemplateField, ItemTemplate, and EditItemTemplate?
We will use Template Field, whenever we want to define some custom control in the data bound controls like GridView, DataGrid or DataRepeater.
ItemTemplate is the content to display for the items in the TemplateField.
EditItemTemplate as the name itself it is the content to display for the items in the TemplateField when it is in Edit mode. Refer this for complete reference.
Take this simple Snippet.
In the above Snippet you are seeing a label in Item Template that means whenever we are showing GridView to the user Label will be visible to the user. If he clicks edit (or) in the edit mode he can be able to see the Textbox.
The above piece of code will also do the same as label in ItemTemplate and Textbox in the EditItemTemplate. By default all the boundfields will be trasferred as Textboxes in Edit Mode. To avoid this we need to keep the property ReadOnly as false, Then in the edit mode nothing will happenes. It just visible like a label.
The reason I use TemplateField instead of BoundField is that, it is very easy to grab those controls in the codebehind if we user labels or textboxes. In case of the bound field’s we need to check what column it is and need to use index of that cloumn, which is little hard and even in future if we change the column order we have to change the code too.
If you see the Last TemplateField part in the above code, I am using the link buttons for edit, update, delete and cancel. May be you may think why am I using link buttons as we are provided with some command field buttons by GridView.
1) If we use command buttons we have less control on them while doing validations, i.e. while assigning validation groups.
2) Secondly if you want to include a custom field (some button or link) in the same column it’s not possible.
If we use proper command names for the buttons like “EDIT”, “DELETE”, “UPDATE”, “CANCEL” these will also trigger the appropriate GridView Events.
Ok let’s move on to Code behind what we have to do when user clicks Edit, Delete, Update and Cancel
When we click on Edit the OnRowEditing event will be fired. You can see the C# code below
In the above code snippet as you see GridViewEditEventArgs will give the row number whatever you are editing by NewEditIndex property. So GridView will put the appropriate row into the Edit Mode by assigning row number to its EditIndex property. Then again we have to call that BindGridData () method to bind data for the textboxes.
So once after done with editing data, if we click update the following method will be triggered. It will call OnRowUpdating Event.
1) The First line is to get the Primary Key of the table using DataKeyNames Property of GridView.
2) The Second line is to access the value of the invisible column.
Once we access the required fields in the code behind, we need to update the data in the database and show the updated data to the user. So am calling the method BindGridData method which is overloaded . I am passing Update Query as a parameter to it. You can see that method in the following snippet.
If the user click cancels the following event (OnRowCancelling) will be fired. When you are setting the value of the EditIndex to -1 that means you are exiting from the editmode. So i.e. when user clicks cancel, here we are exiting from the editmode and rebinding the data to the GridView by calling BindGridData method.
If the user clicks the Delete button OnRowDeleting event will be fired. The following is the code for that event. Going into that code we are trying to get the primary key of the employee table(EmployeeID) by using GridView DataKeyNames and then pass the Query to the BindGridData method to bind the updated data to the GridView.
Happy Coding .
Keep Rocking as always J