Gridview rowupdating get datakey
Instead of just using WHERE [Product ID] = @original_Product ID, with optimistic concurrency the WHERE clause would have read: This would prevent an UPDATE from happening if, between the time the user clicked a Grid View row's Edit button and having clicked the Update button, another user had modified that particular row.
Like deleting data from a Grid View bound to an Object Data Source, creating an editable Grid View bound to an Object Data Source requires very few changes in the creation of the ASP. Most of the work is required in adding a method to the data access layer class to handle the update.
(Recall that we created the Product DAL class back in the The Update Product Method (Visual Basic) Public Class Product DAL ... Close() End Using End Sub End Class The Update Product Method (C#) public class Product DAL As you can see, the Update method, Update Products(), takes in the original_Product ID along with each of the editable columns in the Grid View: product Name, unit Price, and units In Stock.
Public Shared Sub Update Product(By Val original_Product ID As Integer, _ By Val product Name As String, By Val unit Price As Decimal, _ By Val units In Stock As Integer) 'Updates the Products table Dim sql As String = "UPDATE Products SET Product Name = " & _ " @Product Name, Unit Price = @Unit Price, Units In Stock = " & _ "@Units In Stock WHERE Product ID = @Product ID" Using my Connection As _ New Sql Connection( _ Configuration Manager. Connection String) Dim my Command As New Sql Command(sql, my Connection) my Command. Add( _ New Sql Parameter("@Product Name", product Name)) my Command. Add(New Sql Parameter("@Unit Price", _ unit Price)) my Command. Add(New Sql Parameter("@Units In Stock", _ units In Stock)) my Command. Add(New Sql Parameter("@Product ID", _ original_Product ID)) my Connection. If you had configured the Object Data Source to Compare All Values you'd need an Update method with a signature like: ' Visual Basic .
Additionally, the standard Text Box as an editing interface might not be acceptable; we'll also look at an example that replaces the Text Box with a databound Drop Down List.
Figure 40 Figure 41 In Figure 41 notice how the Product ID column is not editable. NET page's declarative syntax below shows, you can mark a Grid View column as non-editable by settings its Read Only property to True.
In addition to deleting a Grid View's underlying data, another common need is to allow end users to edit the data displayed in a Grid View. NET 1.x's Data Grid control, editing the data is certainly possible, but requires creating three event handlers and writing a dozen or so lines of code. NET 2.0, it is possible to create an editable Grid View without writing a single line of code!
All of the necessary functionality is encapsulated within the Grid View.
Once you have configured a Sql Data Source with an UPDATE statement, creating an editable Grid View is a breeze.
Just add a new Grid View to the page and, from its Smart Tag, click the Enable Editing checkbox (see Figure 39).
Search for gridview rowupdating get datakey:
This will add a Command Field to the Grid View with an Edit button.