So many people asked in the emails for my previous article why I have used Template Columns rather than bound fields.
I just want to make them clear this question (going to explain it at the end of the article) because, I am going to use Template Fields again in this article 🙂 TASK: In this article we are trying to display some information from Adventure Works database, Employee table, and if the employee pay rate is more than 25 we are going to change the color of the row/cell/text.
Here is the html markup for the grid The Above markup is pretty simple if you follow my previous article as I just used template field’s and Item templates. If we want to modify data or check certain conditions before displaying it to the user this is the correct place to do the changes.
The only thing special piece from the previous article is On Row Data Bound=”Grid View1_Row Data Bound”. As we need to change the row color/ text color/ cell color before displaying to the user this we need this event. protected void Grid View1_Row Data Bound(object sender, Grid View Row Event Args e) As we can see, it has two parameters.
You can not attach validation controls directly to these Text Boxes.
We need to convert these Bound Fields to Template Fields.
If the last name is "Beaver", the delete operation is canceled, and an error message is displayed.
Click on the “Advanced” button and check the “Generate INSERT, UPDATE and DELETE statements” checkbox (Figure 3). Now that you have configured the SQL Data Source control let’s bind it with a Grid View control. Open its smart tag and configure it as shown in Figure 4.
A Grid View Delete Event Args object is passed to the event-handling method, which enables you to determine the index of the current row and to indicate that the delete operation should be canceled.
To cancel the delete operation, set the event to cancel the delete operation.
One note will be helpful for you, while describing Columns in Grid View if you are using a boundfield then create objects of the control using cells[index] in the rowupdateing event of Grid View, but if you are using controls itself like Label or textboxes etc then use Fincontrol("stringid").
The idea is to only allow a command to be performed on specific people. I'd prefer a declarative solution to a procedural one. For me converting the commandfield to a templatefield control is not an option, as the commandfield comes with built-in functionality that I would otherwise have to create myself, for example the fact that it changes to "Update Cancel" when Edit is clicked, and that when Edit is clicked, all the cells in the row which are labels become textboxes, etc.