Updating GridView with Datasource
Hi, the last days we need edit a row into GridView only with DataSourse, so in many places any body has a entire solution to this, then, I decided post the code to solution this.
( Note: In VFP this is easy, only a cursor, a grid and one append and replace or updatetable, easy.)
Partial
Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dv As New Data.DataView
Dim sql As New SqlDataSource(ConfigurationManager.ConnectionStrings.Item("demoConnectionString").ToString(), "select * from empleados")
dv = sql.Select(UI.DataSourceSelectArguments.Empty)
Me.GridView1.DataSource = dv
Me.GridView1.DataBind()
End If
End Sub
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
Dim gv As GridView = DirectCast(sender, GridView)
For i As Integer = 0 To GridView1.Columns.Count - 1
Dim cell As DataControlFieldCell = TryCast(gv.Rows(e.RowIndex).Cells(i), DataControlFieldCell)
gv.Columns(i).ExtractValuesFromCell(e.NewValues, cell, DataControlRowState.Edit,
True)
Next
End Sub
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
Dim gv As GridView = DirectCast(sender, GridView)
gv.EditIndex = e.NewEditIndex
gv.DataBind()
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.GridView1.EditIndex = 1
End Sub
End
Class
Best Regards
Franklin Garzón
MVP Visual FoxPro
MCITP SQLServer