I am binding my gridview without using a datasource control and instead doing it in the code behind. However, I would like to use the inline editing features of the gridview. Could someone please point me to an example that does gridview editing/updating 'manually' in the code behind?
Thanks,
Tim
![]() |
0 |
![]() |
just create an object data source on your page and point it to a Class that acts as a BLL . in that class make public methods like:
updateTHECLASS()
insertTHECLASS()
deleteTHECLASS()and in the object data sources insert, update, and delete methods put in those public methods you made in your BLL. Also set theTypeName to the namespace and class name of your BLL Class. once this is done you can use "Auto Generate Edit Button" and "Auto Generate Delete Buttons" by binding the gridview to your new ObjectDataSource.
hth,
mcm
![]() |
0 |
![]() |
Thanks for your reply, however I am wanting to do this without adding a datasource control to the page.
![]() |
0 |
![]() |
Well, if you dont want to add a datasource to the page then you will have to put those methods, etc in the codebehind for the page. And instead of the AUTO Edit you would have to create a template field. In that template field put a button or asp hyperlink that (in codebehind) changes the gridview to Edit mode.
then in edit mode you would change the button or link in the template field to be an UPDATE button, and the click event of that update button should call the method to update in your code behind.
this will have to be repeated for deletes and inserts as well
hth,
mcm
![]() |
0 |
![]() |
Thanks again. This is exactly what I'm trying to do. And as my original question indicated, I was wondering if someone could point me to any existing examples that use gridview editing in this way. I have been unable to find anything thru other searches.
![]() |
1 |
![]() |
try this one : http://www.aspcode.net/Codebehind-databinding-howto-part-1.aspx
hth,
mcm
![]() |
0 |
![]() |
This example is basically what I am doing now, by performing the edit in a separate form. However, I was hoping to find something that does in-line editing within the gridview.
Thanks for your help. I guess this question isn't as simple/common as I thought.
![]() |
0 |
![]() |
you can do it pretty easily. its very simple to change the GridView to Edit mode like
myGridView.ChangeMode(GridViewMode.Edit) // or something like that press help on ChangeMode Method)
once in edit mode all you have to do is add a template column to your edit template and have that access your update method in codebehind. Thats it. Its not that hard if you give it a whirl.
hth,
mcm
![]() |
0 |
![]() |
Well, for anyone else interested, I think I figured it out. The biggest issue is that you cannot use e.NewValues in the RowUpdating handler. You have to find the controls you use in the EditItemTemplate of the GridView to get the new values.
Here is some sample code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then BindData() End If End Sub Private Sub BindData() GridView1.DataSource = bll.GetData() GridView1.DataBind() End Sub Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit GridView1.EditIndex = -1 BindData() End Sub Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing GridView1.EditIndex = e.NewEditIndex BindData() End Sub Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating Dim txt1 As TextBox = GridView1.Rows(e.RowIndex).Cells(0).FindControl("txt1") Dim txt2 As TextBox = GridView1.Rows(e.RowIndex).Cells(0).FindControl("txt2") bll.UpdateData(GridView1.DataKeys(e.RowIndex).Value, txt1.Text, txt2.Text) GridView1.EditIndex = -1 BindData() End Sub
![]() |
1 |
![]() |
cyzfitz:Well, for anyone else interested, I think I figured it out. The biggest issue is that you cannot use e.NewValues in the RowUpdating handler. You have to find the controls you use in the EditItemTemplate of the GridView to get the new values.
Here is some sample code:Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then BindData() End If End Sub Private Sub BindData() GridView1.DataSource = bll.GetData() GridView1.DataBind() End Sub Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit GridView1.EditIndex = -1 BindData() End Sub Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing GridView1.EditIndex = e.NewEditIndex BindData() End Sub Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating Dim txt1 As TextBox = GridView1.Rows(e.RowIndex).Cells(0).FindControl("txt1") Dim txt2 As TextBox = GridView1.Rows(e.RowIndex).Cells(0).FindControl("txt2") bll.UpdateData(GridView1.DataKeys(e.RowIndex).Value, txt1.Text, txt2.Text) GridView1.EditIndex = -1 BindData() End Sub
This was EXACTLY what I have been looking for. Thank you so much for your post cyzfitz!
![]() |
0 |
![]() |