0

我正在绑定一个下拉列表,我在第一列中有名字,在第二列中有姓氏作为 DDL`

protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{ 
    DataTable myTable = new DataTable();

    DataColumn productIDColumn = new DataColumn("FirstName");

    DataColumn productNameColumn = new DataColumn("LastName");

    myTable.Columns.Add(productNameColumn);
    DataSet ds = new DataSet();

    ds = get();
    if (e.Row.RowType == DataControlRowType.DataRow)

   {

      var  fname= (e.Row.Cells[0].Text);

    var expression = "FirstName "+fname;

    DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");

    DataRow[] rows = ds.Tables[0].Select(expression);



    ddl.DataSource = myTable;

    ddl.DataTextField = "LastName";

    ddl.DataValueField = "LastName";

    ddl.DataBind();

 }

但是 ddl 是空的,无法填充 ddl 对此有任何帮助吗?

4

1 回答 1

0

从你的代码来看,myTable没有数据。

您正在创建myTable,但没有填充它:您正在将信息获取到变量ds中,并将其过滤到rows中,但您设置ddl.DataSourcemyTable,而不是rows

使用rows,你可能会没事,只要rows.

ddl.DataSource = rows;

ddl.DataTextField = "LastName"; 

ddl.DataValueField = "LastName"; 

ddl.DataBind(); 
于 2012-10-24T17:11:21.137 回答