Tuesday, 7 February 2012

nested grideview wtih tempory update,delete



code c#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;


public partial class Default3 : System.Web.UI.Page
{
DataClassesDataContext o = new DataClassesDataContext(ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ToString());
int i, j;
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var m = (from x in o.DEPTs select x);
ddl.DataSource = m;
ddl.DataTextField = "dname";
ddl.DataValueField = "did";
ddl.DataBind();
ddl.Items.Insert(0, "------Select------");
ddl.SelectedIndex = 0;
view();
}
}
protected void btn1_Click(object sender, EventArgs e)
{
dt.Columns.Add("DID", typeof(string));
dt.Columns.Add("DNAME", typeof(string));
DataRow dr;
dr = dt.NewRow();
dr[0] = ddl.SelectedValue;
dr[1] = ddl.SelectedItem;
dt.Rows.Add(dr);
for (i = 0; i < gv.Rows.Count; i++)
{
dr = dt.NewRow();
dr[0] = gv.Rows[i].Cells[2].Text;
dr[1] = gv.Rows[i].Cells[3].Text;
dt.Rows.Add(dr);
}
ViewState["tb"] = dt;
gv.DataSource=dt;
gv.DataBind();
}
void view()
{
var n = from x in o.EMPs select x;
gv1.DataSource = n;
gv1.DataBind();
}
protected void btn_Click(object sender, EventArgs e)
{

EMP p = new EMP();
p.EID = Convert.ToInt32(tb.Text);
p.NAME = tb1.Text;
o.EMPs.InsertOnSubmit(p);
o.SubmitChanges();
for (i = 0; i < gv.Rows.Count; i++)
{
EMP1 q = new EMP1();
q.EID = Convert.ToInt32(tb.Text);
q.DID = Convert.ToInt32(gv.Rows[i].Cells[0].Text);
o.EMP1s.InsertOnSubmit(q);
o.SubmitChanges();
}
lb.Text = "One record saved.";
view();

}
protected void im_Click(object sender, ImageClickEventArgs e)
{
ImageButton a = (ImageButton)sender;
GridViewRow gvr = (GridViewRow)a.NamingContainer;
i = Convert.ToInt32(a.CommandArgument);
var h = from u in o.EMP1s join v in o.DEPTs on u.DID equals v.DID where u.EID == i select new {v.DNAME};
((GridView)gvr.FindControl("gv2")).DataSource = h;
((GridView)gvr.FindControl("gv2")).DataBind();
}
protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DataTable dt = (DataTable)ViewState["tb"];
dt.Rows[e.RowIndex][1] = ((TextBox)gv.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
ViewState["tb"] = dt;
gv.EditIndex = -1;
gv.DataSource = dt;
gv.DataBind();
}
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = e.NewEditIndex;
DataTable dt = (DataTable)ViewState["tb"];
gv.DataSource = dt;
gv.DataBind();
}
protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DataTable dt = (DataTable)ViewState["tb"];
dt.Rows.Remove(dt.Rows[e.RowIndex]);
ViewState["tb"] = dt;
gv.DataSource = dt;
gv.DataBind();
}
protected void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gv.EditIndex = -1;
DataTable dt = (DataTable)ViewState["tb"];
gv.DataSource = dt;
gv.DataBind();
}
}

No comments:

Post a Comment