简体   繁体   中英

Unable to populate drop down list inside gridview

I am binding a dropdownlist i have firstname in the 1st column and lastname as a DDL in the second column`

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

 }

but the ddl is empty unable to fill the ddl any help on this??

Judging from your code, myTable has no data.

You are creating myTable , but you aren't filling it: you are fetching info into the variable ds , and filtering it into rows , but you set ddl.DataSource to myTable , not to rows .

Use rows and you will probably be fine, as long as there is data in rows .

ddl.DataSource = rows;

ddl.DataTextField = "LastName"; 

ddl.DataValueField = "LastName"; 

ddl.DataBind(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM