Click here to Skip to main content
Email Password   helpLost your password?

Download MultipleDeletionDatagrid.zip - 22.7 KB

Screenshot - datagrid2.jpg

Screenshot - editGrid.jpg

Introduction

I found it very common assignment to make a datagrid with multiple deletion option like HOTMAIL ..and that inspires me to write this code.Here is the code for editing and deleting multiple data with datagrid.

Background

(Optional) Is there any background to this article that may be useful such as an introduction to the basic ideas presented?

Using the code

To use this code just unzip the file as I have not attached the whole project file so you need to add the pages in your project. and some modification as you always need.

Blocks of code should be set as style "Formatted" like this:

//

// Any source code blocks look like this

// 

 
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Configuration;

namespace DelhiApp.Testing
{
    /// <summary>

    /// Summary description for WebForm1.

    /// </summary>

    public class WebForm1 : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Button Button2;
        protected System.Web.UI.WebControls.DataGrid DataGrid2;
        protected System.Web.UI.WebControls.Panel Panel3;
    
        private void Page_Load(object sender, System.EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                BindData(DataGrid2);
                Button2.Attributes.Add("onclick","return confirm('Are you sure you wish to delete these records?');");
            }
        }

        private void BindData(DataGrid DataGrid1)
        {
            
            OleDbConnection con=new OleDbConnection(ConfigurationSettings.AppSettings["connectionString"]);
            OleDbCommand cmd=new OleDbCommand();
            cmd.CommandText="select * from emp";
            cmd.Connection=con;
            cmd.Connection.Open();
            OleDbDataReader dr=cmd.ExecuteReader();
            DataGrid1.DataSource=dr;
            DataGrid1.DataBind();
            cmd.Connection.Close();
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //

            // CODEGEN: This call is required by the ASP.NET Web Form Designer.

            //

            InitializeComponent();
            base.OnInit(e);
        }
        
        /// <summary>

        /// Required method for Designer support - do not modify

        /// the contents of this method with the code editor.

        /// </summary>

        private void InitializeComponent()
        {    
            this.Button2.Click += new System.EventHandler(this.Button2_Click);
            this.DataGrid2.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.Cancel_Grid);
            this.DataGrid2.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.Edit_Grid);
            this.DataGrid2.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.Update_Grid);
            this.DataGrid2.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.Delete_Grid);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void Button2_Click(object sender, System.EventArgs e)
        {
            foreach(DataGridItem objItem in DataGrid2.Items)
            {
                if (objItem.ItemType != ListItemType.Header && objItem.ItemType != ListItemType.Footer && objItem.ItemType != ListItemType.Pager)
                {    
                    
                    if(((CheckBox)objItem.Cells[0].FindControl("cbSelected")).Checked == true)
                    {
                        OleDbConnection con=new OleDbConnection(ConfigurationSettings.AppSettings["connectionString"]);
                        OleDbCommand cmd=new OleDbCommand();
                        cmd.CommandText="DELETE from emp where empId=@empId";
                        cmd.Parameters.Add("@empId",OleDbType.Numeric).Value=DataGrid2.DataKeys[objItem.ItemIndex];
                        cmd.Connection=con;
                        cmd.Connection.Open();
                        cmd.ExecuteNonQuery();
                        cmd.Connection.Close();
                        
                    }
                }
            }
            BindData(DataGrid2);
        }

        private void Delete_Grid(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            OleDbConnection con=new OleDbConnection(ConfigurationSettings.AppSettings["connectionString"]);
            OleDbCommand cmd=new OleDbCommand();
            cmd.CommandText="DELETE from emp where empId=@empId";

            
            cmd.Parameters.Add("@empId",OleDbType.Numeric).Value=DataGrid2.DataKeys[e.Item.ItemIndex];

            cmd.Connection=con;
            cmd.Connection.Open();
            cmd.ExecuteNonQuery();
            cmd.Connection.Close();

            DataGrid2.EditItemIndex=-1;
            BindData(DataGrid2);
        }

        private void Edit_Grid(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            DataGrid2.EditItemIndex = e.Item.ItemIndex;
            
            // Always bind the data so the datagrid can be displayed.

            BindData(DataGrid2);
        }

        private void Update_Grid(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            OleDbConnection con=new OleDbConnection(ConfigurationSettings.AppSettings["connectionString"]);
            OleDbCommand cmd=new OleDbCommand();
            cmd.CommandText="UPDATE emp SET empName=@empName, salary=@salary, joiningDate=@joiningDate where empId=@empId";

            cmd.Parameters.Add("@empName",OleDbType.Char).Value=((TextBox)e.Item.Cells[1].Controls[0]).Text;
            cmd.Parameters.Add("@salary",OleDbType.Numeric).Value=((TextBox)e.Item.Cells[2].Controls[0]).Text;
            cmd.Parameters.Add("@joiningDate",OleDbType.Date).Value=((TextBox)e.Item.Cells[3].Controls[0]).Text;
            cmd.Parameters.Add("@empId",OleDbType.Numeric).Value=DataGrid2.DataKeys[e.Item.ItemIndex];

            cmd.Connection=con;
            cmd.Connection.Open();
            cmd.ExecuteNonQuery();
            cmd.Connection.Close();

            DataGrid2.EditItemIndex=-1;
            BindData(DataGrid2);
        }

        private void Cancel_Grid(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            DataGrid2.EditItemIndex = -1;
            BindData(DataGrid2);
        }
    }
}
 
"Edit_and_Delete_Data/MultipleDeletionDatagrid.zip">Download MultipleDeletionDatagrid.zip - 22.7 KB

Remember to set the Language of your code snippet using the Language dropdown.

Use the "var" button to to wrap Variable or class names in <code> tags like this.

Points of Interest

Did you learn anything interesting/fun/annoying while writing the code? Did you do anything particularly clever or wild or zany?

History

Keep a running update of any changes or improvements you've made here.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Questionstoring webform data in pdf file in asp.net
Rambabukasiboina
5:22 2 Aug '07  
How can i store the user data in pdf formatted file in my local system with asp.net vb code.please send me the answer it's very important to me. is that possible in asp.net.

rambabu

AnswerRe: storing webform data in pdf file in asp.net
rahul_bit
20:07 2 Aug '07  
yes, u can use 3rd party component or u can develop ur own code to do this. better download the code and modify it, it is available on various sites.

Rahul
GeneralTry with ASP.NET 2.0 GridView
Jaiprakash M Bankolli
21:52 12 Apr '07  
Hi,

Nice article would suggest you to try with ASP.NET 2.0 GridView control and various other controls that comes with 2.0.

there are lot more new facility with it.


Regards,
Jaiprakash M Bankolli
jaiprakash.bankolli@gmail.com
http://jaiprakash.blog.com/

GeneralNot able to run the appliaction
honeyss
2:38 26 Mar '07  
Hi,

First of all let me thank u for posting wonderfull concept.
Everything is working fine except cmd.Parameters.Add("@empId",OleDbType.Numeric).Value=DataGrid2.DataKeys[e.Item.ItemIndex];
In this line for every method i am getting error as Index out of range. Can you tell me the solution to eradiacte that;P;P
GeneralRe: Not able to run the appliaction
rahul_bit
20:00 26 Mar '07  
hi,
u r getting this error becoz u havnt set the value for datagrid properties called "DataKeyField" go properties box for datagrid and enter the primary key field name of the table in DataKeyField or u can also modify the code as ...
cmd.Parameters.Add("@empId",OleDbType.Numeric).Value=((TextBox)e.Item.Cells[0].Controls[0]).Text;
here u have to put the Cell index of the column containing primary key for the table... like Cell[0]...Cell[1]...hope u got the point.Cool



Rahul.
GeneralRe:Thanks
honeyss
19:24 27 Mar '07  
Thanks Rahul..............

Sorry i didnt even kept my mind there. With this i got b4 only.
cmd.Parameters.Add("@empId",OleDbType.Numeric).Value=((TextBox)e.Item.Cells[0].Controls[0]).Text;

Now i tried by keeping the datakeyfield.......and its working.......

Thanks a lot


Last Updated 19 Apr 2007 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010