Combobox选择的值显示在gridview中

时间:2010-08-02 07:36:36

标签: c# asp.net .net gridview combobox

使用C#& MySQL的

在我的网页上使用Comboxbox,如果从组合框中选择值,则所选值应显示在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 个答案:

答案 0 :(得分:3)

您应该在cmbvnoview.SelectedIndexChanged事件中执行此操作。

答案 1 :(得分:1)

使用ComboBox SelectedIndexChanged事件。

答案 2 :(得分:1)

OnSelectedIndexChanged 事件中编写代码并标记

AutoPostBack="true"

在combobox标签中..

<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();
}

答案 3 :(得分:1)

它将解决您的问题

<强>设计

<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;


    }