using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace WEBSWAPP.Website.CodeSamples.ASPNET1.NestedDataGrids
{
public partial class NestedGrid : System.Web.UI.UserControl
{
public int CompanyPrimaryKey
{
get
{
int ret = 0;
if (ViewState["CompanyPrimaryKey"] != null) ret = (int)ViewState["CompanyPrimaryKey"];
return ret;
}
set
{
ViewState["CompanyPrimaryKey"] = value;
DataGrid_Bind();
}
}
private void DataGrid_Bind()
{
if (Data_View() != null)
{
if (Data_View().Count > 0)
{
ChildGrid.DataSource = Data_View();
ChildGrid.DataBind();
}
}
}
private DataView Data_View()
{
DataView dv = null;
DataSet ds = (DataSet)Session["DataGrid_ParentChild"];
if (ds != null)
{
DataView dvParent = new DataView(ds.Tables["Company"], "ID=" + CompanyPrimaryKey, null, System.Data.DataViewRowState.CurrentRows);
if (dvParent.Count > 0) dv = dvParent[0].CreateChildView("ParentChild");
}
return dv;
}
protected void DataGrid_Command(object sender, DataGridCommandEventArgs e)
{
}
protected void EditCommand(object sender, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
ChildGrid.EditItemIndex = e.Item.ItemIndex;
DataGrid_Bind();
}
protected void CancelCommand(Object sender, DataGridCommandEventArgs e)
{
ChildGrid.EditItemIndex = -1;
DataGrid_Bind();
}
protected void UpdateCommand(Object sender, DataGridCommandEventArgs e)
{
DataRow dr = Data_View().Table.Rows.Find(ChildGrid.DataKeys[e.Item.ItemIndex]);
if (dr != null) //item found
{
dr["Name"] = ((TextBox)e.Item.Cells[1].Controls[0]).Text;
}
ChildGrid.EditItemIndex = -1;
DataGrid_Bind();
}
}
}