Deleting Gridview Row

Posted on 2009-02-12
Last Modified: 2012-05-06
Hello all,

I would like to know how to delete a gridview row on button click. I' m not using the auto generated delete button because of a requirement.

Thanks for the help.
Question by:AvinashKN
    LVL 12

    Accepted Solution

    Hi, I assume you want to delete the underlying record in a gridview row from the database.
    Let me know if its other way round.

    Ok , so you have a linkbutton in grid, assign its commandargument to the dataitemindex i.e. it'll store
    the row. syntax would be: CommandArgument=<%# Container.DataItemIndex %> in the button markup.

    Also, I assume that the primary key or identifier is kept in datakey in gridview.

    so on click of a linkbutton, you fetch its command argument
    command argument gives you the rowid where button is clicked
    using rowid, you fetch that row's datakey of the particular value you want to identify it in database
    then delete it from database by passing that key in your deletefunction of database.

    Let me know if its confusing you or you are stuck at any particular point.
    Happy Coding!
    LVL 12

    Expert Comment

    Oops forgot to add that 'You have to re-bind the grid after you delete the record in order to reflect the changes in web form'

    Author Comment

    Thanks for the quick reply.

    I have created a method to remove the row. What I'm finding difficult is to get the value of the row.

    For example...

    I'm using the following code to change the color of a particular account number..

    if (e.Row.RowType == DataControlRowType.DataRow)
                string strAccountNumber = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Account No"));
                if (strAccountNumber == "7000512518001")

                    e.Row.BackColor = System.Drawing.Color.Red;

    I would like to know how to select the row based on the account number like I'm doing there.
    LVL 12

    Expert Comment

    Hi, like I said, in the row, you have to set your button's command argument to container.dataitemindex.   Set your "Account No" as datakey value for your gridview, you can even set it from the front end property box.  

    Then access current row's datakey value to get your account no as I have already mentioned in the example.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    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…

    733 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

    19 Experts available now in Live!

    Get 1:1 Help Now