-1

可能重复:
在网格视图中填充下拉列表

我有一个下拉列表,我在 gridview 中填充它。在第一列中我有 Firstname,在第二列中我有一个 DDL,我想加载与 firstname 关联的 lastname 我该如何实现?

Protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            con.Open();
            var ddl = (DropDownList)e.Row.FindControl("DropDownList1");
            //int CountryId = Convert.ToInt32(e.Row.Cells[0].Text);
            SqlCommand cmd = new SqlCommand("select FirstName,LastName from Profile_Master" , con);
            SqlDataAdapter da = new SqlDataAdapter(cmd); 
            DataSet ds = new DataSet();
            da.Fill(ds);
            con.Close();
            ddl.DataSource = ds;
            ddl.DataTextField = "LastName";
            ddl.DataValueField = "FirstName";
            ddl.DataBind();


        }

    }
4

1 回答 1

0
  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {


            if (e.Row.RowType == DataControlRowType.DataRow)
            {

 SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=dbname;Integrated Security=True");

                SqlCommand cmd = new SqlCommand("select lastname from Profile_Master where firstname='" + e.Row.Cells[0].Text + "'", con);
                SqlDataAdapter adp = new SqlDataAdapter(cmd);
                System.Data.DataSet DS=new System.Data.DataSet();
                adp.Fill(DS);



                Control Ctr = e.Row.FindControl("ddl");

                DropDownList dd =  (DropDownList)  Ctr;
                dd.DataSource = DS;
                dd.DataTextField = "LastName";
                dd.DataValueField = "LastName";
                dd.DataBind();

}

}
于 2012-10-24T08:46:13.540 回答