关闭

如何实现在DataGrid中实现DropDownList控件的联动

1124人阅读 评论(0) 收藏 举报

在C#中,DATAGRID控件的是数据绑定类控件的一个好东东,这其中的奥秘就在于它支持多种样式,而且还能和c#中其他控件实现联动,如下例子:

前台代码:

<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 117px; POSITION: absolute; TOP: 76px" runat="server" AutoGenerateColumns="False" Width="246px">
     <Columns>
      <asp:TemplateColumn>
       <HeaderTemplate>
        订单号
       </HeaderTemplate>
       <ItemTemplate>
        <asp:Label id="lblOrderID" runat="server">
         <%# 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>
       <HeaderTemplate>
        商品号
       </HeaderTemplate>
       <ItemTemplate>
        <asp:Label id="lblproductID" runat="server">
         <%# 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>
    </asp:DataGrid> 

 

后台代码:

//声明全局变量

protected System.Web.UI.WebControls.DataGrid DataGrid1;
  public string orderID="";
  protected string productID="";
        OleDbConnection myconn=new OleDbConnection();
  OleDbCommand cmd=new OleDbCommand();
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
    myconn=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c://inetpub//wwwroot//Palm//StationTable.MDB");
     
   if (!IsPostBack)
   {
    DataGridDataBind();
      }
  }
//将数据库中查找到的结果绑定到DATAGRID控件上

 public void DataGridDataBind()
  {
   OleDbDataAdapter da=new OleDbDataAdapter("select orders.orderid,productid from orders,orderdetails where orders.orderid=orderdetails.orderid order by orders.orderid",myconn);
      DataSet ds=new DataSet();
   da.Fill(ds,"testTable");
   DataGrid1.DataSource=ds.Tables["testTable"];
   DataGrid1.DataBind();
  }
  public OleDbDataReader BindTheOrderID()
  {
   cmd=new OleDbCommand("select orderid from orders order by orderid",myconn);
   myconn.Open();
   return cmd.ExecuteReader(CommandBehavior.CloseConnection);
  }
  public OleDbDataReader BindTheProductID(string OrderID)
  {
   string strsql="";
   if(OrderID!="" && OrderID!=null)
   {
    strsql="select productid from orderdetails where orderid='"+OrderID+"'";
   }
   else
   {
    strsql="select distinct productid from orderdetails";
   }
   cmd=new OleDbCommand(strsql,myconn);
   myconn.Open();
   return cmd.ExecuteReader(CommandBehavior.CloseConnection);
  }

  private void DataGrid1_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();
   DataGrid1.EditItemIndex=e.Item.ItemIndex;
   DataGridDataBind();
  }
  public void DropDownListChange(object sender,System.EventArgs e)
  {
   DropDownList OrderDrp=(DropDownList)sender;
   orderID=OrderDrp.SelectedItem.Text;
   DataGridDataBind();
  }

  private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   DataGrid1.EditItemIndex=-1;
   DataGridDataBind();
  }

  private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
  {
   if(e.Item.ItemType==ListItemType.EditItem)
   {
    DropDownList drpOrderId=(DropDownList)e.Item.FindControl("drpOrder");
    drpOrderId.SelectedIndex=drpOrderId.Items.IndexOf(drpOrderId.Items.FindByText(orderID));
    DropDownList drpProductID=(DropDownList)e.Item.FindControl("drpProduct");
    drpProductID.SelectedIndex=drpProductID.Items.IndexOf(drpProductID.Items.FindByText(productID));
   }
  }

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:131575次
    • 积分:597
    • 等级:
    • 排名:千里之外
    • 原创:52篇
    • 转载:4篇
    • 译文:1篇
    • 评论:48条
    最新评论