Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Deleting Gridview Row

Posted on 2009-02-12
Medium Priority
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
  • 3
LVL 12

Accepted Solution

techExtreme earned 2000 total points
ID: 23625079
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

ID: 23625093
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

ID: 23625156
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

ID: 23625304
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month12 days, 7 hours left to enroll

578 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