Download MultipleDeletionDatagrid.zip - 22.7 KB
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:
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
{
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)
{
InitializeComponent();
base.OnInit(e);
}
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;
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.