Click here to Skip to main content
Click here to Skip to main content
Go to top

Add column for delete in GridView with confirmation box

, 25 Jun 2007
Rate this:
Please Sign up or sign in to vote.
In this article, we will add a column for delete in GridView, with an image button and confirmation.

Introduction

In this article, we will see how to add and delete columns in a GridView, with an image button and a confirmation box.

Background

This article is an extension of my previous article: Event handling in Gridview User Control. I am extending the same project for this article.

Using the Code

  1. In the article Event handling in Gridview User Control, we created an event handler for RowDataBound. In this article, we are adding one more event handler for RowCommand. Use the same procedure to define the event.
  2. protected void Simple_DGV_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        OnRowCommand(e);
    }
    
    protected void OnRowCommand(GridViewCommandEventArgs e)
    {
        if (GridRowCommand != null)
            GridRowCommand(this, e);}
  3. Define the delegates for the event.
  4. public delegate void RowCommand(object sender, GridViewCommandEventArgs e);
    public event RowCommand GridRowCommand;
  5. Add an event handler for RowDataBound in the parent page.
  6. In the parent page, add a blank boundfield column in the GridView.
  7. BoundField b2 = new BoundField();
    Inc_GridView1.Columns.Add(b2);//blank column to add delete image
  8. Now in the RowDataBound event, add an image button in the blank column.
  9. void Inc_GridView1_GridRowDataBound(object sender, GridViewRowEventArgs e)
    {
    	if (e.Row.RowType == DataControlRowType.DataRow)
    	{
    		int x = Inc_GridView1.Columns.Count - 1;
    		// it will give blank column index
    
    		ImageButton img1 = new ImageButton();
    		img1.ID = "img1";
    		img1.ToolTip = "Delete";
    		img1.ImageUrl = "icons-del.gif";
    		e.Row.Cells[x].Controls.Add(img1);
    
    		ImageButton img2 = new ImageButton();
    		img2 = (ImageButton)e.Row.Cells[x].Controls[0];
    		img2.CommandName = "Delete"; 
    		img2.CommandArgument = 
    		     DataBinder.Eval(e.Row.DataItem, "c").ToString();
    		// argument to pass for delete command
    
    		// it will give confirmation on button click
    		img2.OnClientClick = 
    		     "return confirm('Do you want to delete this Record?');";
    	}
    }
  10. Now add code for handling the delete functionality:
  11. void Inc_GridView1_GridRowCommand(object sender, GridViewCommandEventArgs e)
    {
    	if (e.CommandName == "Delete")
    	{
    		int param = Convert.ToInt32(e.CommandArgument);
    		//function to delete row
    	}
    }

That's all.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Pallavi Bhoite
Web Developer
India India
Working as software developer from last 3 year.
I am handling Windows,Web Application with C# (Visual studio 2005) and SQL server 2005. I am MCTS - Web Application 2.0 and MCPD - Web developer.

Comments and Discussions

 
GeneralBugs etc. Pinmemberploxis10-Jul-07 2:45 
1. GridViews are not re-bound each time the form posts back so you should be using the OnRowCreated event and not the OnRowDataBound event. If you added a button to the form that causes a postback and click the button the the delete buttons will not reappear.
 
2. There is a much easier way to do this by:
- Enable Deleting on the gridview
- Edit the gridview columns, select the delete column and click the "Convert this field into a TemplateField" button.
- Edit the gridview's templates
- Select the template that was created from the delete button, Select the delete button from within the template and set the button's OnClientClick to "return confirm('Are you sure you would like to delete item?');"
 
3. You have the following line :
int x = Inc_GridView1.Columns.Count - 1; // it will give blank column index
This line does not "give blank column index", it gives the index of the last column. I expect that you wanted to add a new column and create delete buttons in that new column, what this code actually does is add delete buttons to the last column which may or may not already have items in it. If the last column does already have text or buttons in it your code will simply add the delete buttons to whatever is in the last column

GeneralRe: Bugs etc. PinmemberAaron Dutton22-Aug-07 7:17 
GeneralThanks Pinmemberinetfly12326-Jun-07 5:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 26 Jun 2007
Article Copyright 2007 by Pallavi Bhoite
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid