GridView editing without using data source controls?

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
cyzfitz
11/13/2007 4:43:59 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

9 Replies
1952 Views

Similar Articles

[PageSpeed] 39
Get it on Google Play
Get it on Apple App Store

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
mcmcomasp
11/13/2007 5:15:33 PM

Thanks for your reply, however I am wanting to do this without adding a datasource control to the page.

0
cyzfitz
11/13/2007 5:19:26 PM

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
mcmcomasp
11/13/2007 5:31:19 PM

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
cyzfitz
11/13/2007 5:39:45 PM
0
mcmcomasp
11/13/2007 5:40:56 PM

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
cyzfitz
11/13/2007 5:51:13 PM

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
mcmcomasp
11/13/2007 5:56:49 PM

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
11/13/2007 7:37:53 PM

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
n0k
11/3/2008 10:24:26 PM
Reply:

Similar Artilces:

Trying to use the FAQ " Sorting and paging in the GridView control when not using data source controls "Sorting and paging in the GridView control when not using..."
and I am not getting the results I had hoped for.  I keep getting a "Compiler Error Message: CS0123: No overload for 'AdhocJobGridView_PageIndexChanged' matches delegate 'System.EventHandler'".   Here is my event handling... protected void AdhocJobGridView_PageIndexChanged(object sender, GridViewPageEventArgs e){ try { AdhocJobGridView.DataSource = wsBaseInfoArray; AdhocJobGridView.PageIndex = e.NewPageIndex; AdhocJobGridView.DataBind(); }catch (Exception ex) {throw ex; }   Here is my aspx... <asp:GridView ID="AdhocJobGridView...

Using Data Controls Without Using ObjectDataSource Control
Hi, I've been looking on the Internet for a tutorial that would show me how to use the data conrols (FormView, GridView etc) without using an ObjectDataSource (or any other data source control).  The reason is that I want to use a formview and gridview from within another formview control to create a single form  that would allow me to insert, update and delete an order and its order details at the same time. I thought that this could be achieved by having a DataSet containing two DataTable objects in the code-behind with the Order DataTable containing one row to repres...

Add new controls to a gridview row; when editing that row using the edit row feature in the gridview control.
Hi all  I have a questions that looks hard / complex to me, and i know you can help me in this. Q {  I have row in a Gridview and i have an edit option to it. I press the edit button, to edit the values in that row and update data all the way back to the database. Now when i press the edit button, i want a drop down list to be displayed that contain values for a particular field in my grid view. (for now only plain text boxes appear.) } I hope you understand my question, all i need to do is replace the text box controls with a dropdown list when i am editing a row in ...

using javascript getting value from textbox when edit button is clicked and both controls are present in gridview control
Hi All using javascript I want to get value  from textbox when edit button is clicked and both controls that is texbox and button are present in gridview control .Alok Hi aloksinha83, Please see this post: http://forums.asp.net/t/1069245.aspx Thanks,Qin Dian TangMicrosoft Online Community SupportPlease remember to mark the replies as answers if they help and unmark them if they provide no help....

Data presentation control in another Data presentation control
 Hello,I want to make a menu with datalist or gridview control.Basically,the menu will be like this,Categories     Sub Categories.For example, for COMPUTING category there will be NOTEBOOKS DESKTOPS etc.There will be a image for CATEGORIES and linkbuttons with Subcategories.If I put a datalist inside a datalist and a link button inside the second datalist visually it satisfies my need.However,CommandArguments come from database to linkbutton.Andwhen one of these command buttons is cliked it must go to proper page.I dont know how to wrie an event_handler ,specifically, for...

need code to update gridview without using sql data source control
    hi,this is satish,working as a software developer.I have faced the problem to update gridview in .NET 2.0 with teplate columns including one dropdownlist instea of textbox without using sql datasource.please send me the code with events. please...

Edit data with GridView Control ? or another control ? help
I use GrivView in ASP.Net 2.0 to show data from DataTable. And i want to edit some fields in some rows. How can I do it? If i touch EDIT button - all columns, which have ReadOnly=false, switch into EDIT mode. But I want, that in first raw I can edit 2,3 column and in second row only 3 column. How can I do it ? ...

Using Data Control in another data control
Hey, How to use a SqlDataSource control in a GridView and so on...I mean i wanna show data which comes from UserTables in a gridview whoses data comes from ContentTable so they are two separated tables. Thanks and Regards...I am not a perfect programmer, but i have perfect programmers' habits [ i think so :)] I mean how to use nested gridview controls. Please aren't there any poeple who can help me ?I am not a perfect programmer, but i have perfect programmers' habits [ i think so :)] Try the links below, they might be of some help to you.http://msdn2.microso...

how can I use the same data source to control gridview
 I have two gridviews. One is used to bind the data that user search by searching form.And another one is used to show the selected data when user select in previous gridview.When I use gridview, i bind the selected data by using the data source, ds.and after result show back to the user, my page will disable any searching area, and show only the button to add more items into the selected list.My scope is... if user need to add more items into the selected list.I will enable searching area to the user for searching it again, My question is.. how can i bind the selected data that us...

using controlS to control a gridview
This question is probably posted but i did not bother to look at 10,000+ posts so i am sorry. I've seen the tutorial where he builds a dataset and uses a gridview and controls the gridview with the drop down list box. What i am trying to do is to control the gridview with 2 drop down list boxes. First select the Country you life in and then the State. I've looked at the grid view properties but couldn't find where i can add another parameter. Is it possible to control a grid view (or any form of displaying the information) with more than 1 controller? Thank You.  ...

How to add the data present in the controls to a table control without any database involvement.
Hi All,i am four controls where i will write the text and once i click the add button those should get added to the table .i want 6 columns fixed in the table and rows as many as required.i tried doing that taking a table server control and able to load the table.but the problem is whenver i am adding a new row the old one is gfetting overwritten by this and at the end i am getting only one row.  System.Web.UI.WebControls.Table t1 = new System.Web.UI.WebControls.Table();  i have declared the below one global TableRow tr; TableCell tc1; TableCell tc2; TableCell tc...

Bind Data to an Image Control not in a Data Presentation Control
I've been searching for two days but I don't really know what to search for.I'm making a site for a photographer.  The Default.aspx page displays Albums by showing a title, image and description from the database table Albums. Click an album to view the contents and it passes the QueryString which is AlbumName.  The ViewAlbum.aspx page uses the QueryString to display an UpdatePanel holding a Large Image and a Repeater full of dynamically filled ImageButtons for thumbs.  Both are from the database table Photos and are retrieved using an ObjectDataSource. Click the ...

Data presentation without a data control
The Gridview and Formview controls provide good functionality in some circumstances but are clumsey in others. As a newbie to .NET I was wondering if its appropriate to do databinding with textboxes and other data controls outside of one of the new data controls (formview, gridview, etc). Or is it a new standard to always use one of the new data controls. If the answer is yes, would you still use an SQLDatasource, or would you set up the connection in code? And, how would you deal with the syntax? In the Formview we have: <asp:TextBox ID="fnameTextBox" runat="server" ...

Using GridView and Data Source Control with a DAL and BL
In ASP 1.1 you have a Business Layer and a Data Access Layer. The presentation layer, or the aspx form in my example has a data grid. The grid gets it's datasource (a dataset) from the business layer. Then when there are updates, inserts, and deletes it calls the business layer and uses the logic inside of it. That code calls the DAL to make the actual database changes or to initially retrieve the dataset. So the BL creates the commandtext (or perhaps the parameters to pass to a stored procedure) and the DAL actually creates the command and connection objects and runs the update. So...

Web resources about - GridView editing without using data source controls? - asp.net.presentation-controls

Control - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

‘Enough Is Enough’: Obama Demands More Gun Control After Colorado Shooting
President Obama again turned to gun control as a potential solution to the horrific act

Obama pushes for gun control after Colorado shooting
"This is not normal," president says after gunman fatally shoots three people, including police officer, at Planned Parenthood facility

DC Police Chief, “If you’re in a position to try and take the gunman down…it’s the best option for saving ...
... in a position to try and take the gunman down…it’s the best option for saving lives before police can get there,” Yet DC Supports Gun Control ...

The Legend of Zelda: Twilight Princess Wii U will likely support Wiimote and Nunchuk controls
Well this is interesting. Fans have been speculating as to the control schemes that will be supported in The Legend of Zelda: Twilight Princess ...

Mind-Control Bacteria Stop You From Eating Too Much
Gut microbes release proteins that stop the brain from thinking you're still hungry.

Control Your HTPC With Scavenged IR Parts
If you’ve built yourself a home theater PC, one of your highest priorities is probably coming up with a convenient control solution. The easiest ...

ICYMI: Even the AP couldn’t validate any of Hillary’s gun control nonsense
... – not 200 as she claimed . Asked to explain the discrepancy, Clinton’s campaign pointed to 2013 statistics from the Centers for Disease Control ...

Over-The-Counter Birth Control Empowers Women And Pharmacists In Oregon And California
Over-the-counter birth control is coming to Oregon and California. There’s an enactment of new laws on the horizon that will allow pharmacists ...

Colorado embodies nation's divide over gun control
Los Angeles Times Colorado embodies nation's divide over gun control Los Angeles Times People are escorted away from a deadly shooting Nov. ...

Resources last updated: 11/29/2015 7:07:37 PM