控件设置:
<asp:DataGrid id="dgDoubleDrp" runat="server" Width="680px" Height="256px" AutoGenerateColumns="False"
BorderColor="White" BorderStyle="Ridge" CellSpacing="1" BorderWidth="2px" BackColor="White"
CellPadding="3" GridLines="None">
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#9471DE"></SelectedItemStyle>
<ItemStyle ForeColor="Black" BackColor="#DEDFDE"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#E7E7FF" BackColor="#4A3C8C"></HeaderStyle>
<FooterStyle ForeColor="Black" BackColor="#C6C3C6"></FooterStyle>
<Columns>
<asp:TemplateColumn HeaderText="订单号">
<ItemTemplate>
<asp:Label ID=lblOrderID Runat=server text='<%#DataBinder.Eval(Container,"DataItem.OrderID")%>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID=drpOrder Runat=server AutoPostBack=True OnSelectedIndexChanged="DropDownListChange" DataSource="<%#BindTheOrderID()%>" DataTextField="OrderID">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="商品号">
<ItemTemplate>
<asp:Label ID=lblproductID Runat=server text='<%#DataBinder.Eval(Container,"DataItem.ProductID")%>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="drpProduct" Runat=server DataSource="<%#BindTheProductID(orderID)%>" DataTextField="ProductID">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="编辑"></asp:EditCommandColumn>
</Columns>
<PagerStyle HorizontalAlign="Right" ForeColor="Black" BackColor="#C6C3C6"></PagerStyle>
</asp:DataGrid>
后台代码:
//记录当前订单号
public string orderID="";
//记录当前商品号
protected string productID="";
//创建链接对象
SqlConnection conn=new SqlConnection();
//创建命令对象
SqlCommand cmd=new SqlCommand();
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["DataBaseCon"].ToString());
//页面初试化时进行数据绑定
if(!IsPostBack)
DataGridDataBind();
}
private void DataGridDataBind()
{
//创建数据适配器对象
SqlDataAdapter da=new SqlDataAdapter("select top 10 Orders.OrderID,ProductID from Orders,[Order Details] where Orders.OrderID=[Order Details].OrderID order by Orders.OrderID",conn);
//创建DataSet对象
DataSet ds=new DataSet();
try
{
//填充数据集
da.Fill(ds,"testTable");
dgDoubleDrp.DataSource=ds.Tables["testTable"];
dgDoubleDrp.DataBind();
}
catch(Exception error)
{
Response.Write(error.ToString());
}
}
//订单号列表绑定方法
public SqlDataReader BindTheOrderID()
{
//定义命令对象
cmd=new SqlCommand("select OrderID from Orders order by OrderID",conn);
//打开数据库联结
conn.Open();
//返回DataReader对象
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
//商品号订单列表绑定方法
public SqlDataReader BindTheProductID(string OrderID)
{
string strSql="";
if(OrderID!=""&&OrderID!=null)
{
strSql="select ProductID from [Order Details] where OrderID='"+OrderID+"'";
}
else
{
strSql="select distinct ProductID from [Order Details]";
}
//定义命令对象
cmd=new SqlCommand(strSql,conn);
//打开数据库联结
conn.Open();
//返回DataReader对象
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
private void dgDoubleDrp_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//取得当前行的订单号和商品号
orderID=((Label)e.Item.FindControl("lblOrderID")).Text.Trim();
productID=((Label)e.Item.FindControl("lblproductID")).Text.Trim();
//设置DataGrid控件的当前编辑项的索引
dgDoubleDrp.EditItemIndex=e.Item.ItemIndex;
//重新进行数据绑定
DataGridDataBind();
}
public void DropDownListChange(object sender,System.EventArgs e)
{
//建立drpOrder下拉列表控件的引用对象
DropDownList OrderDrp=(DropDownList)sender;
//取得选择的订单号
orderID=OrderDrp.SelectedItem.Text;
//重新进行数据绑定
DataGridDataBind();
}
private void dgDoubleDrp_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//取消编辑
dgDoubleDrp.EditItemIndex=-1;
DataGridDataBind();
}
private void dgDoubleDrp_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
//判断是否是编辑项
if(e.Item.ItemType==ListItemType.EditItem)
{
//找到编辑项中的drpOrder下拉列表控件
DropDownList drpOrderID=(DropDownList)e.Item.FindControl("drpOrder");
//设置在上个操作中drpOrder下拉列表中选中的项
drpOrderID.SelectedIndex=drpOrderID.Items.IndexOf(drpOrderID.Items.FindByText(orderID));
//找到编辑项中的drpProduct下拉列表控件
DropDownList drpProductID=(DropDownList)e.Item.FindControl("drpProduct");
//设置在上个操作中drpProduct下拉列表中选中的项
drpProductID.SelectedIndex=drpProductID.Items.IndexOf(drpProductID.Items.FindByText(productID));
}
}