Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads
 
Add your own
alternative version

Edit and Delete multiple Data in DataGrid in ASP.NET

, 18 Apr 2007
Edit and Delete multiple Data in DataGrid in ASP.NET
MultipleDeletionDatagrid.zip
MultipleDeletionDatagrid
user_db.mdb
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);
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

rahul_bit
Web Developer TATA Communications
India India
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 19 Apr 2007
Article Copyright 2007 by rahul_bit
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid