Friday, February 23, 2007

Delete the data through DataGrid

This is the program to delete data in the Datagrid.

[CODE]
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.SqlClient;
namespace DeleteGrid
{
///


/// Summary description for WebForm1.
///

public class WebForm1 : System.Web.UI.Page
{
protected DataTable table = new DataTable();
protected System.Web.UI.WebControls.DataGrid DataGrid1;
SqlConnection con = new SqlConnection(@"Server=Servername;DataBase=Databasename;uid=username;pwd=username");
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(!IsPostBack)
{
FillDG();
BindDG();
}
}

public void FillDG()
{
SqlDataAdapter da = new SqlDataAdapter("Select * from Emp order by iEid",con);
da.Fill(table);
}

public void BindDG()
{
DataGrid1.DataSource=table;
DataGrid1.DataBind();
}
#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);
}
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler
(this.DataGrid1_DeleteCommand);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string s11;
s11 =(e.Item.Cells[1].Text);//Getting The DateGird Cell Conetent
SqlCommand Dcmd =new SqlCommand("Delete from Emp WHERE iEid=" + Convert.ToInt32(s11),con);
con.Open();
Dcmd.ExecuteNonQuery();
FillDG();
BindDG();
}
}
}
[/CODE]

OutPut
Initial DataGrid

AfterDeleting a One Row




No comments:

Creative Commons License
Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.