1.html代码
<
HTML
>
< HEAD >
< title > DataGridNesting </ title >
< meta content ="Microsoft Visual Studio .NET 7.1" name ="GENERATOR" >
< meta content ="C#" name ="CODE_LANGUAGE" >
< meta content ="JavaScript" name ="vs_defaultClientScript" >
< meta content ="http://schemas.microsoft.com/intellisense/ie5" name ="vs_targetSchema" >
</ HEAD >
< body MS_POSITIONING ="GridLayout" >
< form id ="Form1" method ="post" runat ="server" >
< asp:datagrid id ="DataGrid1" runat ="server" DataKeyField ="ClassID" OnEditCommand ="Edit_Grid1"
OnCancelCommand ="Cancel_Grid1" OnUpdateCommand ="Update_Grid1" OnDeleteCommand ="Delete_Grid1"
CellPadding ="4" BackColor ="White" BorderWidth ="1px" BorderStyle ="None" BorderColor ="#CC9966"
Width ="660px" AutoGenerateColumns ="False" OnItemCommand ="Item_Command" >
< FooterStyle ForeColor ="#330099" BackColor ="#FFFFCC" ></ FooterStyle >
< SelectedItemStyle Font-Bold ="True" ForeColor ="#663399" BackColor ="#FFCC66" ></ SelectedItemStyle >
< ItemStyle ForeColor ="#330099" BackColor ="White" ></ ItemStyle >
< HeaderStyle Font-Bold ="True" ForeColor ="#FFFFCC" BackColor ="#990000" ></ HeaderStyle >
< Columns >
< asp:TemplateColumn HeaderText ="序号" >
< HeaderStyle Width ="10px" ></ HeaderStyle >
< ItemTemplate >
<% # Container.ItemIndex+1 %>
</ ItemTemplate >
</ asp:TemplateColumn >
< asp:TemplateColumn HeaderText ="序号" >
< HeaderStyle Width ="10px" ></ HeaderStyle >
< ItemTemplate >
< asp:ImageButton id ="ImageButton1" runat ="server" ImageUrl ="add.gif" ></ asp:ImageButton >
</ ItemTemplate >
</ asp:TemplateColumn >
< asp:BoundColumn Visible ="False" DataField ="ClassID" HeaderText ="ClassID" >
< HeaderStyle Width ="0px" ></ HeaderStyle >
</ asp:BoundColumn >
< asp:TemplateColumn HeaderText ="班级" >
< HeaderStyle Width ="300pt" ></ HeaderStyle >
< ItemTemplate >
<% # DataBinder.Eval(Container.DataItem,"ClassName") %>
< asp:DataGrid id ="DataGrid2" runat ="server" DataKeyField ="StudentID" OnEditCommand ="grid2_EditCommand"
OnCancelCommand ="grid2_CancelCommand" OnUpdateCommand ="grid2_UpdateCommand" OnDeleteCommand ="grid2_DeleteCommand"
CellPadding ="4" BackColor ="White" BorderWidth ="1px" BorderStyle ="None" BorderColor ="#3366CC"
Width ="200px" AutoGenerateColumns ="False" >
< FooterStyle ForeColor ="#003399" BackColor ="#99CCCC" ></ FooterStyle >
< SelectedItemStyle Font-Bold ="True" ForeColor ="#CCFF99" BackColor ="#009999" ></ SelectedItemStyle >
< ItemStyle ForeColor ="#003399" BackColor ="White" ></ ItemStyle >
< HeaderStyle Font-Bold ="True" ForeColor ="#CCCCFF" BackColor ="#003399" ></ HeaderStyle >
< Columns >
< asp:TemplateColumn HeaderText ="序号" >
< HeaderStyle Width ="10px" ></ HeaderStyle >
< ItemTemplate >
<% # Container.ItemIndex+1 %>
</ ItemTemplate >
</ asp:TemplateColumn >
< asp:BoundColumn Visible ="False" DataField ="StudentID" HeaderText ="StudentID" >
< HeaderStyle Width ="0px" ></ HeaderStyle >
</ asp:BoundColumn >
< asp:TemplateColumn HeaderText ="姓名" >
< HeaderStyle Width ="30px" ></ HeaderStyle >
< ItemTemplate >
<% # DataBinder.Eval(Container.DataItem,"StudentName") %>
</ ItemTemplate >
< EditItemTemplate >
< asp:TextBox id ="txtStudentName" Runat ="server" Text ='<%# DataBinder.Eval(Container.DataItem,"StudentName") % > '>
</ asp:TextBox >
</ EditItemTemplate >
</ asp:TemplateColumn >
< asp:TemplateColumn HeaderText ="年龄" >
< HeaderStyle Width ="30px" ></ HeaderStyle >
< ItemTemplate >
<% # DataBinder.Eval(Container.DataItem,"Age") %>
</ ItemTemplate >
< EditItemTemplate >
< asp:TextBox id ="txtAge" Runat ="server" Text ='<%# DataBinder.Eval(Container.DataItem,"Age") % > '>
</ asp:TextBox >
</ EditItemTemplate >
</ asp:TemplateColumn >
< asp:TemplateColumn HeaderText ="班级" >
< HeaderStyle Width ="30px" ></ HeaderStyle >
< ItemTemplate >
<% # DataBinder.Eval(Container.DataItem,"ClassName") %>
</ ItemTemplate >
< EditItemTemplate >
< asp:DropDownList id ="drpClass" Runat ="server" >
< asp:ListItem Value ="1" > 一班 </ asp:ListItem >
< asp:ListItem Value ="2" > 二班 </ asp:ListItem >
< asp:ListItem Value ="3" > 三班 </ asp:ListItem >
</ asp:DropDownList >
</ EditItemTemplate >
</ asp:TemplateColumn >
< asp:EditCommandColumn ButtonType ="LinkButton" UpdateText ="Update" HeaderText ="Edit" CancelText ="Cancel"
EditText ="Edit" >
< HeaderStyle Width ="20px" ></ HeaderStyle >
</ asp:EditCommandColumn >
< asp:ButtonColumn Text ="Delete" HeaderText ="Delete" CommandName ="Delete" >
< HeaderStyle Width ="20px" ></ HeaderStyle >
</ asp:ButtonColumn >
</ Columns >
< PagerStyle HorizontalAlign ="Left" ForeColor ="#003399" BackColor ="#99CCCC" Mode ="NumericPages" ></ PagerStyle >
</ asp:DataGrid >
</ ItemTemplate >
< EditItemTemplate >
< asp:TextBox id =txtClassName Runat ="server" Text ='<%# DataBinder.Eval(Container.DataItem,"ClassName") % > '>
</ asp:TextBox >
</ EditItemTemplate >
</ asp:TemplateColumn >
< asp:TemplateColumn HeaderText ="备注" >
< HeaderStyle Width ="300px" ></ HeaderStyle >
< ItemTemplate >
<% # DataBinder.Eval(Container.DataItem,"Description") %>
</ ItemTemplate >
< EditItemTemplate >
< asp:TextBox id ="txtDescription" Runat ="server" Text ='<%# DataBinder.Eval(Container.DataItem,"Description") % > '>
</ asp:TextBox >
</ EditItemTemplate >
</ asp:TemplateColumn >
< asp:EditCommandColumn ButtonType ="LinkButton" UpdateText ="Update" HeaderText ="Edit" CancelText ="Cancel"
EditText ="Edit" >
< HeaderStyle Width ="20px" ></ HeaderStyle >
</ asp:EditCommandColumn >
< asp:ButtonColumn Text ="Delete" HeaderText ="Delete" CommandName ="Delete" >
< HeaderStyle Width ="20px" ></ HeaderStyle >
</ asp:ButtonColumn >
</ Columns >
< PagerStyle HorizontalAlign ="Center" ForeColor ="#330099" BackColor ="#FFFFCC" ></ PagerStyle >
</ asp:datagrid ></ form >
</ body >
</ HTML >
2.cs代码
< HEAD >
< title > DataGridNesting </ title >
< meta content ="Microsoft Visual Studio .NET 7.1" name ="GENERATOR" >
< meta content ="C#" name ="CODE_LANGUAGE" >
< meta content ="JavaScript" name ="vs_defaultClientScript" >
< meta content ="http://schemas.microsoft.com/intellisense/ie5" name ="vs_targetSchema" >
</ HEAD >
< body MS_POSITIONING ="GridLayout" >
< form id ="Form1" method ="post" runat ="server" >
< asp:datagrid id ="DataGrid1" runat ="server" DataKeyField ="ClassID" OnEditCommand ="Edit_Grid1"
OnCancelCommand ="Cancel_Grid1" OnUpdateCommand ="Update_Grid1" OnDeleteCommand ="Delete_Grid1"
CellPadding ="4" BackColor ="White" BorderWidth ="1px" BorderStyle ="None" BorderColor ="#CC9966"
Width ="660px" AutoGenerateColumns ="False" OnItemCommand ="Item_Command" >
< FooterStyle ForeColor ="#330099" BackColor ="#FFFFCC" ></ FooterStyle >
< SelectedItemStyle Font-Bold ="True" ForeColor ="#663399" BackColor ="#FFCC66" ></ SelectedItemStyle >
< ItemStyle ForeColor ="#330099" BackColor ="White" ></ ItemStyle >
< HeaderStyle Font-Bold ="True" ForeColor ="#FFFFCC" BackColor ="#990000" ></ HeaderStyle >
< Columns >
< asp:TemplateColumn HeaderText ="序号" >
< HeaderStyle Width ="10px" ></ HeaderStyle >
< ItemTemplate >
<% # Container.ItemIndex+1 %>
</ ItemTemplate >
</ asp:TemplateColumn >
< asp:TemplateColumn HeaderText ="序号" >
< HeaderStyle Width ="10px" ></ HeaderStyle >
< ItemTemplate >
< asp:ImageButton id ="ImageButton1" runat ="server" ImageUrl ="add.gif" ></ asp:ImageButton >
</ ItemTemplate >
</ asp:TemplateColumn >
< asp:BoundColumn Visible ="False" DataField ="ClassID" HeaderText ="ClassID" >
< HeaderStyle Width ="0px" ></ HeaderStyle >
</ asp:BoundColumn >
< asp:TemplateColumn HeaderText ="班级" >
< HeaderStyle Width ="300pt" ></ HeaderStyle >
< ItemTemplate >
<% # DataBinder.Eval(Container.DataItem,"ClassName") %>
< asp:DataGrid id ="DataGrid2" runat ="server" DataKeyField ="StudentID" OnEditCommand ="grid2_EditCommand"
OnCancelCommand ="grid2_CancelCommand" OnUpdateCommand ="grid2_UpdateCommand" OnDeleteCommand ="grid2_DeleteCommand"
CellPadding ="4" BackColor ="White" BorderWidth ="1px" BorderStyle ="None" BorderColor ="#3366CC"
Width ="200px" AutoGenerateColumns ="False" >
< FooterStyle ForeColor ="#003399" BackColor ="#99CCCC" ></ FooterStyle >
< SelectedItemStyle Font-Bold ="True" ForeColor ="#CCFF99" BackColor ="#009999" ></ SelectedItemStyle >
< ItemStyle ForeColor ="#003399" BackColor ="White" ></ ItemStyle >
< HeaderStyle Font-Bold ="True" ForeColor ="#CCCCFF" BackColor ="#003399" ></ HeaderStyle >
< Columns >
< asp:TemplateColumn HeaderText ="序号" >
< HeaderStyle Width ="10px" ></ HeaderStyle >
< ItemTemplate >
<% # Container.ItemIndex+1 %>
</ ItemTemplate >
</ asp:TemplateColumn >
< asp:BoundColumn Visible ="False" DataField ="StudentID" HeaderText ="StudentID" >
< HeaderStyle Width ="0px" ></ HeaderStyle >
</ asp:BoundColumn >
< asp:TemplateColumn HeaderText ="姓名" >
< HeaderStyle Width ="30px" ></ HeaderStyle >
< ItemTemplate >
<% # DataBinder.Eval(Container.DataItem,"StudentName") %>
</ ItemTemplate >
< EditItemTemplate >
< asp:TextBox id ="txtStudentName" Runat ="server" Text ='<%# DataBinder.Eval(Container.DataItem,"StudentName") % > '>
</ asp:TextBox >
</ EditItemTemplate >
</ asp:TemplateColumn >
< asp:TemplateColumn HeaderText ="年龄" >
< HeaderStyle Width ="30px" ></ HeaderStyle >
< ItemTemplate >
<% # DataBinder.Eval(Container.DataItem,"Age") %>
</ ItemTemplate >
< EditItemTemplate >
< asp:TextBox id ="txtAge" Runat ="server" Text ='<%# DataBinder.Eval(Container.DataItem,"Age") % > '>
</ asp:TextBox >
</ EditItemTemplate >
</ asp:TemplateColumn >
< asp:TemplateColumn HeaderText ="班级" >
< HeaderStyle Width ="30px" ></ HeaderStyle >
< ItemTemplate >
<% # DataBinder.Eval(Container.DataItem,"ClassName") %>
</ ItemTemplate >
< EditItemTemplate >
< asp:DropDownList id ="drpClass" Runat ="server" >
< asp:ListItem Value ="1" > 一班 </ asp:ListItem >
< asp:ListItem Value ="2" > 二班 </ asp:ListItem >
< asp:ListItem Value ="3" > 三班 </ asp:ListItem >
</ asp:DropDownList >
</ EditItemTemplate >
</ asp:TemplateColumn >
< asp:EditCommandColumn ButtonType ="LinkButton" UpdateText ="Update" HeaderText ="Edit" CancelText ="Cancel"
EditText ="Edit" >
< HeaderStyle Width ="20px" ></ HeaderStyle >
</ asp:EditCommandColumn >
< asp:ButtonColumn Text ="Delete" HeaderText ="Delete" CommandName ="Delete" >
< HeaderStyle Width ="20px" ></ HeaderStyle >
</ asp:ButtonColumn >
</ Columns >
< PagerStyle HorizontalAlign ="Left" ForeColor ="#003399" BackColor ="#99CCCC" Mode ="NumericPages" ></ PagerStyle >
</ asp:DataGrid >
</ ItemTemplate >
< EditItemTemplate >
< asp:TextBox id =txtClassName Runat ="server" Text ='<%# DataBinder.Eval(Container.DataItem,"ClassName") % > '>
</ asp:TextBox >
</ EditItemTemplate >
</ asp:TemplateColumn >
< asp:TemplateColumn HeaderText ="备注" >
< HeaderStyle Width ="300px" ></ HeaderStyle >
< ItemTemplate >
<% # DataBinder.Eval(Container.DataItem,"Description") %>
</ ItemTemplate >
< EditItemTemplate >
< asp:TextBox id ="txtDescription" Runat ="server" Text ='<%# DataBinder.Eval(Container.DataItem,"Description") % > '>
</ asp:TextBox >
</ EditItemTemplate >
</ asp:TemplateColumn >
< asp:EditCommandColumn ButtonType ="LinkButton" UpdateText ="Update" HeaderText ="Edit" CancelText ="Cancel"
EditText ="Edit" >
< HeaderStyle Width ="20px" ></ HeaderStyle >
</ asp:EditCommandColumn >
< asp:ButtonColumn Text ="Delete" HeaderText ="Delete" CommandName ="Delete" >
< HeaderStyle Width ="20px" ></ HeaderStyle >
</ asp:ButtonColumn >
</ Columns >
< PagerStyle HorizontalAlign ="Center" ForeColor ="#330099" BackColor ="#FFFFCC" ></ PagerStyle >
</ asp:datagrid ></ form >
</ body >
</ HTML >
public
class
DataGridNesting : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
BindDataGrid1();
}
}
Web Form Designer generated code#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);
this.DataGrid1.ItemCreated+=new DataGridItemEventHandler(DataGrid1_ItemCreated);
}
/**//**//**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
GetDataSet#region GetDataSet
private DataSet GetDataSet(string sql)
{
string constring=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
SqlDataAdapter sda =new SqlDataAdapter(sql,constring);
DataSet ds=new DataSet();
sda.Fill(ds);
return ds;
}
#endregion
BindDataGrid1#region BindDataGrid1
private void BindDataGrid1()
{
string sql="select * from Class";
DataSet ds=GetDataSet(sql);
this.DataGrid1.DataSource=ds;
this.DataGrid1.DataBind();
}
#endregion
BindDataGrid2#region BindDataGrid2
private void BindDataGrid2(DataGrid grid2)
{
string sql="select s.ClassID,s.StudentID,s.StudentName,s.Age,c.ClassName from Student s left join Class c on s.ClassID=c.ClassID where s.ClassID='"+DataGrid1.DataKeys[Index].ToString()+"'";
DataSet ds=GetDataSet(sql);
grid2.DataSource=ds;
grid2.DataBind();
}
#endregion
DataGrid1_ItemDataBound#region DataGrid1_ItemDataBound
private void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType==ListItemType.Header)
{
e.Item.Cells[0].ColumnSpan=2;//合并单元格
e.Item.Cells[1].Visible=false;
}
if (e.Item.ItemType==ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem)
{
System.Web.UI.WebControls.ImageButton image = (System.Web.UI.WebControls.ImageButton)e.Item.FindControl("ImageButton1");
string sql="select s.ClassID,s.StudentID,s.StudentName,s.Age,c.ClassName from Student s left join Class c on s.ClassID=c.ClassID where s.ClassID='"+this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString()+"'";
DataSet ds=GetDataSet(sql);
DataGrid grid=(DataGrid)e.Item.FindControl("DataGrid2");
grid.DataSource=ds;
grid.DataBind();
grid.Style.Add("display","none");
}
}
#endregion
Edit_Grid1#region Edit_Grid1
protected void Edit_Grid1(object sender,DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex=e.Item.ItemIndex;
BindDataGrid1();
}
#endregion
Cancel_Grid1#region Cancel_Grid1
protected void Cancel_Grid1(object sender,DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex=-1;
BindDataGrid1();
}
#endregion
Update_Grid1#region Update_Grid1
protected void Update_Grid1(object sender,DataGridCommandEventArgs e)
{
if(e.Item.ItemType==ListItemType.EditItem)
{
SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand comm=new SqlCommand("update Class set ClassName=@ClassName,Description=@Description where ClassID=@ClassID",conn);
SqlParameter parm1=new SqlParameter("@ClassName",SqlDbType.NVarChar,50);
parm1.Value=((TextBox)e.Item.FindControl("txtClassName")).Text;
SqlParameter parm2=new SqlParameter("@Description",SqlDbType.NVarChar,500);
parm2.Value=((TextBox)e.Item.FindControl("txtDescription")).Text;
SqlParameter parm3=new SqlParameter("@ClassID",SqlDbType.NVarChar,50);
parm3.Value=this.DataGrid1.DataKeys[e.Item.ItemIndex];
comm.Parameters.Add(parm1);
comm.Parameters.Add(parm2);
comm.Parameters.Add(parm3);
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
this.DataGrid1.EditItemIndex=-1;
BindDataGrid1();
}
}
#endregion
Delete_Grid1#region Delete_Grid1
protected void Delete_Grid1(object sender,DataGridCommandEventArgs e)
{
deletegrid1(this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString());
}
#endregion
deletegrid1#region deletegrid1
private void deletegrid1(string id)
{
SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand comm=new SqlCommand("delete from Class where ClassID=@ClassID",conn);
SqlParameter parm1=new SqlParameter("@ClassID",SqlDbType.NVarChar,50);
parm1.Value=id;
comm.Parameters.Add(parm1);
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
BindDataGrid1();
}
#endregion
DataGrid1_ItemCreated#region DataGrid1_ItemCreated
private void DataGrid1_ItemCreated(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem )
{
DataGrid grid2 = (DataGrid)e.Item.FindControl( "DataGrid2" );
grid2.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler( this.grid2_ItemDataBound );
}
}
#endregion
grid2_ItemDataBound#region grid2_ItemDataBound
protected void grid2_ItemDataBound(object sender, DataGridItemEventArgs e)
{
}
#endregion
grid2_EditCommand#region grid2_EditCommand
protected void grid2_EditCommand(object sender, DataGridCommandEventArgs e)
{
DataGrid grid2 = (DataGrid)sender;
grid2.EditItemIndex = e.Item.ItemIndex;
BindDataGrid2(grid2);
}
#endregion
grid2_CancelCommand#region grid2_CancelCommand
protected void grid2_CancelCommand(object sender, DataGridCommandEventArgs e)
{
DataGrid grid2 = (DataGrid)sender;
grid2.EditItemIndex =-1;
BindDataGrid2(grid2);
}
#endregion
grid2_UpdateCommand#region grid2_UpdateCommand
protected void grid2_UpdateCommand(object sender, DataGridCommandEventArgs e)
{
if(e.Item.ItemType==ListItemType.EditItem)
{
SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand comm=new SqlCommand("update Student set StudentName=@StudentName,ClassID=@ClassID,Age=@Age where StudentID=@StudentID",conn);
SqlParameter parm1=new SqlParameter("@StudentName",SqlDbType.NVarChar,50);
parm1.Value=((TextBox)e.Item.FindControl("txtStudentName")).Text;
SqlParameter parm2=new SqlParameter("@ClassID",SqlDbType.NVarChar,500);
parm2.Value=((DropDownList)e.Item.FindControl("drpClass")).SelectedItem.Value;
SqlParameter parm3=new SqlParameter("@Age",SqlDbType.NVarChar,50);
parm3.Value=((TextBox)e.Item.FindControl("txtAge")).Text;
SqlParameter parm4=new SqlParameter("@StudentID",SqlDbType.NVarChar,50);
parm4.Value=this.DataGrid1.DataKeys[e.Item.ItemIndex];
comm.Parameters.Add(parm1);
comm.Parameters.Add(parm2);
comm.Parameters.Add(parm3);
comm.Parameters.Add(parm4);
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
DataGrid grid2 = (DataGrid)sender;
grid2.EditItemIndex =-1;
BindDataGrid2(grid2);
}
}
#endregion
grid2_DeleteCommand#region grid2_DeleteCommand
protected void grid2_DeleteCommand(object sender, DataGridCommandEventArgs e)
{
DataGrid grid2 = (DataGrid)sender;
DeleteGrid2(grid2.DataKeys[e.Item.ItemIndex].ToString());
grid2.EditItemIndex =-1;
BindDataGrid2(grid2);
}
#endregion
DeleteGrid2#region DeleteGrid2
private void DeleteGrid2(string id)
{
SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand comm=new SqlCommand("delete from Student where StudentID=@StudentID",conn);
SqlParameter parm1=new SqlParameter("@StudentID",SqlDbType.NVarChar,50);
parm1.Value=id;
comm.Parameters.Add(parm1);
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
}
#endregion
property#region property
private int Index
{
get
{
if(ViewState["Index"]!=null && ViewState["Index"].ToString()!="")
{
return Convert.ToInt32(ViewState["Index"]);
}
else
{
return -1;
}
}
set
{
ViewState["Index"]=value;
}
}
#endregion
Item_Command#region Item_Command
protected void Item_Command(object sender,DataGridCommandEventArgs e)
{
Index=e.Item.ItemIndex;
System.Web.UI.WebControls.ImageButton image = (System.Web.UI.WebControls.ImageButton)e.Item.FindControl("ImageButton1");
DataGrid grid=(DataGrid)e.Item.FindControl("DataGrid2");
if(image.ImageUrl=="add.gif")
{
image.ImageUrl="sub.gif";
grid.Style.Add("display","block");
}
else
{
image.ImageUrl="add.gif";
grid.Style.Add("display","none");
}
}
#endregion
}
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
BindDataGrid1();
}
}
Web Form Designer generated code#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);
this.DataGrid1.ItemCreated+=new DataGridItemEventHandler(DataGrid1_ItemCreated);
}
/**//**//**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
GetDataSet#region GetDataSet
private DataSet GetDataSet(string sql)
{
string constring=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
SqlDataAdapter sda =new SqlDataAdapter(sql,constring);
DataSet ds=new DataSet();
sda.Fill(ds);
return ds;
}
#endregion
BindDataGrid1#region BindDataGrid1
private void BindDataGrid1()
{
string sql="select * from Class";
DataSet ds=GetDataSet(sql);
this.DataGrid1.DataSource=ds;
this.DataGrid1.DataBind();
}
#endregion
BindDataGrid2#region BindDataGrid2
private void BindDataGrid2(DataGrid grid2)
{
string sql="select s.ClassID,s.StudentID,s.StudentName,s.Age,c.ClassName from Student s left join Class c on s.ClassID=c.ClassID where s.ClassID='"+DataGrid1.DataKeys[Index].ToString()+"'";
DataSet ds=GetDataSet(sql);
grid2.DataSource=ds;
grid2.DataBind();
}
#endregion
DataGrid1_ItemDataBound#region DataGrid1_ItemDataBound
private void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType==ListItemType.Header)
{
e.Item.Cells[0].ColumnSpan=2;//合并单元格
e.Item.Cells[1].Visible=false;
}
if (e.Item.ItemType==ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem)
{
System.Web.UI.WebControls.ImageButton image = (System.Web.UI.WebControls.ImageButton)e.Item.FindControl("ImageButton1");
string sql="select s.ClassID,s.StudentID,s.StudentName,s.Age,c.ClassName from Student s left join Class c on s.ClassID=c.ClassID where s.ClassID='"+this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString()+"'";
DataSet ds=GetDataSet(sql);
DataGrid grid=(DataGrid)e.Item.FindControl("DataGrid2");
grid.DataSource=ds;
grid.DataBind();
grid.Style.Add("display","none");
}
}
#endregion
Edit_Grid1#region Edit_Grid1
protected void Edit_Grid1(object sender,DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex=e.Item.ItemIndex;
BindDataGrid1();
}
#endregion
Cancel_Grid1#region Cancel_Grid1
protected void Cancel_Grid1(object sender,DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex=-1;
BindDataGrid1();
}
#endregion
Update_Grid1#region Update_Grid1
protected void Update_Grid1(object sender,DataGridCommandEventArgs e)
{
if(e.Item.ItemType==ListItemType.EditItem)
{
SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand comm=new SqlCommand("update Class set ClassName=@ClassName,Description=@Description where ClassID=@ClassID",conn);
SqlParameter parm1=new SqlParameter("@ClassName",SqlDbType.NVarChar,50);
parm1.Value=((TextBox)e.Item.FindControl("txtClassName")).Text;
SqlParameter parm2=new SqlParameter("@Description",SqlDbType.NVarChar,500);
parm2.Value=((TextBox)e.Item.FindControl("txtDescription")).Text;
SqlParameter parm3=new SqlParameter("@ClassID",SqlDbType.NVarChar,50);
parm3.Value=this.DataGrid1.DataKeys[e.Item.ItemIndex];
comm.Parameters.Add(parm1);
comm.Parameters.Add(parm2);
comm.Parameters.Add(parm3);
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
this.DataGrid1.EditItemIndex=-1;
BindDataGrid1();
}
}
#endregion
Delete_Grid1#region Delete_Grid1
protected void Delete_Grid1(object sender,DataGridCommandEventArgs e)
{
deletegrid1(this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString());
}
#endregion
deletegrid1#region deletegrid1
private void deletegrid1(string id)
{
SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand comm=new SqlCommand("delete from Class where ClassID=@ClassID",conn);
SqlParameter parm1=new SqlParameter("@ClassID",SqlDbType.NVarChar,50);
parm1.Value=id;
comm.Parameters.Add(parm1);
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
BindDataGrid1();
}
#endregion
DataGrid1_ItemCreated#region DataGrid1_ItemCreated
private void DataGrid1_ItemCreated(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem )
{
DataGrid grid2 = (DataGrid)e.Item.FindControl( "DataGrid2" );
grid2.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler( this.grid2_ItemDataBound );
}
}
#endregion
grid2_ItemDataBound#region grid2_ItemDataBound
protected void grid2_ItemDataBound(object sender, DataGridItemEventArgs e)
{
}
#endregion
grid2_EditCommand#region grid2_EditCommand
protected void grid2_EditCommand(object sender, DataGridCommandEventArgs e)
{
DataGrid grid2 = (DataGrid)sender;
grid2.EditItemIndex = e.Item.ItemIndex;
BindDataGrid2(grid2);
}
#endregion
grid2_CancelCommand#region grid2_CancelCommand
protected void grid2_CancelCommand(object sender, DataGridCommandEventArgs e)
{
DataGrid grid2 = (DataGrid)sender;
grid2.EditItemIndex =-1;
BindDataGrid2(grid2);
}
#endregion
grid2_UpdateCommand#region grid2_UpdateCommand
protected void grid2_UpdateCommand(object sender, DataGridCommandEventArgs e)
{
if(e.Item.ItemType==ListItemType.EditItem)
{
SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand comm=new SqlCommand("update Student set StudentName=@StudentName,ClassID=@ClassID,Age=@Age where StudentID=@StudentID",conn);
SqlParameter parm1=new SqlParameter("@StudentName",SqlDbType.NVarChar,50);
parm1.Value=((TextBox)e.Item.FindControl("txtStudentName")).Text;
SqlParameter parm2=new SqlParameter("@ClassID",SqlDbType.NVarChar,500);
parm2.Value=((DropDownList)e.Item.FindControl("drpClass")).SelectedItem.Value;
SqlParameter parm3=new SqlParameter("@Age",SqlDbType.NVarChar,50);
parm3.Value=((TextBox)e.Item.FindControl("txtAge")).Text;
SqlParameter parm4=new SqlParameter("@StudentID",SqlDbType.NVarChar,50);
parm4.Value=this.DataGrid1.DataKeys[e.Item.ItemIndex];
comm.Parameters.Add(parm1);
comm.Parameters.Add(parm2);
comm.Parameters.Add(parm3);
comm.Parameters.Add(parm4);
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
DataGrid grid2 = (DataGrid)sender;
grid2.EditItemIndex =-1;
BindDataGrid2(grid2);
}
}
#endregion
grid2_DeleteCommand#region grid2_DeleteCommand
protected void grid2_DeleteCommand(object sender, DataGridCommandEventArgs e)
{
DataGrid grid2 = (DataGrid)sender;
DeleteGrid2(grid2.DataKeys[e.Item.ItemIndex].ToString());
grid2.EditItemIndex =-1;
BindDataGrid2(grid2);
}
#endregion
DeleteGrid2#region DeleteGrid2
private void DeleteGrid2(string id)
{
SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand comm=new SqlCommand("delete from Student where StudentID=@StudentID",conn);
SqlParameter parm1=new SqlParameter("@StudentID",SqlDbType.NVarChar,50);
parm1.Value=id;
comm.Parameters.Add(parm1);
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
}
#endregion
property#region property
private int Index
{
get
{
if(ViewState["Index"]!=null && ViewState["Index"].ToString()!="")
{
return Convert.ToInt32(ViewState["Index"]);
}
else
{
return -1;
}
}
set
{
ViewState["Index"]=value;
}
}
#endregion
Item_Command#region Item_Command
protected void Item_Command(object sender,DataGridCommandEventArgs e)
{
Index=e.Item.ItemIndex;
System.Web.UI.WebControls.ImageButton image = (System.Web.UI.WebControls.ImageButton)e.Item.FindControl("ImageButton1");
DataGrid grid=(DataGrid)e.Item.FindControl("DataGrid2");
if(image.ImageUrl=="add.gif")
{
image.ImageUrl="sub.gif";
grid.Style.Add("display","block");
}
else
{
image.ImageUrl="add.gif";
grid.Style.Add("display","none");
}
}
#endregion
}