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
GetDataSet
BindDataGrid1
BindDataGrid2
DataGrid1_ItemDataBound
Edit_Grid1
Cancel_Grid1
Update_Grid1
Delete_Grid1
deletegrid1
DataGrid1_ItemCreated
grid2_ItemDataBound
grid2_EditCommand
grid2_CancelCommand
grid2_UpdateCommand
grid2_DeleteCommand
DeleteGrid2
property
Item_Command
}
3.数据库脚本{
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
GetDataSet
BindDataGrid1
BindDataGrid2
DataGrid1_ItemDataBound
Edit_Grid1
Cancel_Grid1
Update_Grid1
Delete_Grid1
deletegrid1
DataGrid1_ItemCreated
grid2_ItemDataBound
grid2_EditCommand
grid2_CancelCommand
grid2_UpdateCommand
grid2_DeleteCommand
DeleteGrid2
property
Item_Command
}
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Student]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Student]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Class]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Class]
GO
CREATE TABLE [dbo].[Student] (
[StudentID] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[StudentName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[ClassID] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[Age] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Class] (
[ClassID] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[ClassName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[Description] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
4.源代码下载/Files/singlepine/DataGridNesting.rar
drop table [dbo].[Student]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Class]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Class]
GO
CREATE TABLE [dbo].[Student] (
[StudentID] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[StudentName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[ClassID] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[Age] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Class] (
[ClassID] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[ClassName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[Description] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO