GridVew Updation
-
How to update a gridview if we have used sqldatasource and updatecommand is storedprocedure,How We can get the edited fields of gridview as a parameters for our stored procedure used here??????
PIYUSH
Use the SqlDataSource for this or code it your self. I asume you will not use the SqlDataSource. Steps: 1) fetch the GridViewRowUpdate event 2) compare old and new value 3) create sql statements See sample(you should call your Storedproc with SqlCommand class) Protected Sub GridViewDetectorInstellingen_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridViewDetectorInstellingen.RowUpdating Dim newvals As IOrderedDictionary = e.NewValues Dim oldvals As IOrderedDictionary = e.OldValues 'note TDBP1 is column name in Grid Dim begin As String = newvals.Item("TDBP1").ToString() Dim einde As String = newvals.Item("TDBP2").ToString() Dim oudbegin As String = oldvals.Item("TDBP1").ToString() Dim oudeinde As String = oldvals.Item("TDBP2").ToString() Dim keys As IOrderedDictionary = e.Keys 'Keys are the key columns from the main key of the source table Dim dim1 As String = keys.Item("Dimension1") Dim dim2 As String = keys.Item("Dimension2") Dim kid As String = keys.Item("KID") Dim sql As String Dim pos As Int32 If Not IsNothing(begin) Then If begin <> oudbegin Then pos = begin.IndexOf(":") If pos > -1 Then begin = begin.Remove(begin.IndexOf(":"), 1) End If sql = "update Elementen0 set [Value] = " & begin & " where Dimension1 = " & dim1 & " and Dimension2 = " & dim2 & " and OID = (select ID from Objecten where KID = " & kid & " and Naam = 'TDBP1')" globalstuff.ExecuteNonQuery(sql) End If End If If Not IsNothing(einde) Then If einde <> oudeinde Then pos = einde.IndexOf(":") If pos > -1 Then einde = einde.Remove(einde.IndexOf(":"), 1) End If sql = "update Elementen0 set [Value] = " & einde & " where Dimension1 = " & dim1 & " and Dimension2 = " & dim2 & " and OID = (select ID from Objecten where KID = " & kid & " and Naam = 'TDBP2')" globalstuff.ExecuteNonQuery(sql) End If End If 'no update through SqlDataSource permitted e.Cancel = True 'update is done through code behind so stop editmode Me.GridViewDetectorInstellingen.EditIndex = -1 Me.GridViewDetectorInstellingen.DataBind() End Sub Mdv