update and delete a row from a datagrid

Posted on 2005-05-11
Last Modified: 2012-06-27
I need to update and delete a particular row from a datagrid in ASP.NET and the database is MS Access.
I have an update and delete button for each row. When I click on update the data should be open for edit and after editing I click on save the data should be saved.
When I click delete the row should be delted from datagrid and database
Question by:dhar116
    LVL 9

    Expert Comment

    LVL 21

    Accepted Solution

    it is very easy use button columns for Edit and Delete and use the OnEditCommand and OnDeleteCommand procedures to edit and delete the data.

    <asp:datagrid id="DataGrid1" runat="server" DataKeyField="Regionid" OnDeleteCommand="DataGrid1_Delete"
          OnEditCommand="DataGrid1_Edit" OnCancelCommand="DataGrid1_Cancel">
         <asp:ButtonColumn Text="Delete" CommandName="Delete" />
         <asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" />

    public void DataGrid1_Cancel(Object sender, DataGridCommandEventArgs e)
          DataGrid1.EditItemIndex = -1;
    public void DataGrid1_Edit(Object sender, DataGridCommandEventArgs e)
          DataGrid1.EditItemIndex = (int)e.Item.ItemIndex;                
    public void DataGrid1_Update(Object sender, DataGridCommandEventArgs e)
          string columnvalue =((TextBox)e.Item.Cells[3].Controls[0] ).Text ;
          //get the column values as above construct you update query here and update the database

    public void DataGrid1_Delete(Object sender, DataGridCommandEventArgs e)
         int orderid=(int) DataGrid1.DataKeys[(int)e.Item.ItemIndex];;
         //get the id of the row to delete as above
         //construct your delete query and delete the row from the database.
    Hope that this helps you



    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now