代码之家  ›  专栏  ›  技术社区  ›  Gopal

组合框选定值显示在gridview中

  •  0
  • Gopal  · 技术社区  · 16 年前

    在我使用Comboxbox的网页中,如果我从combobox中选择值,所选值应显示在gridview中。。。。。。

    代码

    cmd2 = new OdbcCommand("Select * from tb_car where vehicleno = '" + cmbvnoview.SelectedValue + "' ", con);
            ada2 = new OdbcDataAdapter(cmd2);
            ds1 = new DataSet();
            ada2.Fill(ds1);
            vhviewgrid.DataSource = ds1;
            vhviewgrid.DataBind();
    

    需要C代码帮助。。。

    4 回复  |  直到 13 年前
        1
  •  1
  •   Azhar    16 年前

    把你的代码写进去 OnSelectedIndexChanged

    AutoPostBack="true"
    

    在combox标记中。。

    <asp:DropDownList ID="cmbvnoview" runat="server" AutoPostBack="true"
                                OnSelectedIndexChanged="cmbvnoview_SelectedIndexChanged">     </asp:DropDownList>
    
    protected void cmbvnoview_SelectedIndexChanged(object sender, EventArgs e)
    {
      cmd2 = new OdbcCommand("Select * from tb_car where vehicleno = '" + cmbvnoview.SelectedValue + "' ", con);
      ada2 = new OdbcDataAdapter(cmd2);
      ds1 = new DataSet();
      ada2.Fill(ds1);
      vhviewgrid.DataSource = ds1;
      vhviewgrid.DataBind();
    }
    
        2
  •  3
  •   sshow Darron Smith    16 年前

    cmbvnoview.SelectedIndexChanged 事件。

        3
  •  1
  •   Neil Knight    16 年前

    使用组合框 SelectedIndexChanged 事件。

        4
  •  1
  •   Jeyavel    16 年前

    <asp:DropDownList ID="DropDownList1"    runat="server" AutoPostBack="True" 
               onselectedindexchanged="DropDownList1_SelectedIndexChanged">
           </asp:DropDownList>
           <br />
           <asp:GridView ID="GridView1" runat="server">
               <SelectedRowStyle BackColor="#99CCFF" />
           </asp:GridView>
    

    代码:

    protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
            SqlConnection con = new SqlConnection("Data Source=JEL-PC\\SQLSERVER2008;Initial Catalog=Jel;user id=sa;password=jel_2004;");
            SqlDataAdapter sda = new SqlDataAdapter("select * from employee", con);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            ViewState["ds"] = ds;
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
    
            DropDownList1.DataSource = ds.Tables[0];
            DropDownList1.DataTextField = "ename";
    
            DropDownList1.DataValueField = "eid";
            DropDownList1.DataBind();
            }
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string x = DropDownList1.SelectedValue;
            int index=0;
            DataSet ds=new DataSet();
    
            ds=(DataSet)ViewState["ds"];
    
            for(int i=0;i<ds.Tables[0].Rows.Count;i++)
            {
                if(ds.Tables[0].Rows[i][0].ToString()==x)
                {
                    index=i;
                    Response.Write(ds.Tables[0].Rows[i][0].ToString()+" i="+i);
                }
            }
    
            GridView1.SelectedIndex = index;
    
    
        }
    
    推荐文章