locked
Populating an EditTemplate DropDownList in a DataGrid RRS feed

  • Question

  • User-395912426 posted

    Here is the code from a datagrid edit command in which I am attempting to populate a DropDownList in the EditTemplate from the database. The result is only one item (the first) from the database, instead of all items.

     

    public void Grid_EditCommand(object source, DataGridCommandEventArgs e)
            {
                Grid.EditItemIndex = e.Item.ItemIndex;
                
                Grid.DataSource = LoadfromDB();
                Grid.DataBind();
    
    
                String strConn;
                // reading the connection string from web.config file
                strConn = ConfigurationSettings.AppSettings["DbDSN"];
    
                //Connection object creation
                SqlConnection con = new SqlConnection(strConn);
                con.Open();
    
                // Set the SQL command to run
    
                SqlCommand myCommand = new SqlCommand("SELECT s.levelID, ls.levelName FROM s INNER JOIN ls ON s.levelID = ls.levelID", con );
                SqlDataReader dr = myCommand.ExecuteReader();
                if (dr.HasRows)
                {
                    DropDownList ddLevelTemp = (DropDownList)Grid.Items[Grid.EditItemIndex].FindControl("ddProgLevel");
                    if (ddLevelTemp != null)
                    {
                        ddLevelTemp.DataTextField = "levelName";
                        ddLevelTemp.DataValueField = "levelID";
                        ddLevelTemp.DataSource = dr;
                        ddLevelTemp.DataBind();
                    }
                    
                }
                con.Close();
                dr.Close();
            }
    


     

    Wednesday, April 7, 2010 11:14 AM

Answers

  • User-395912426 posted

    I figured it out. Embarassed My select statement was returning only one value, so naturally the DropDown contained only one value. Oops.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 7, 2010 9:45 PM

All replies

  • User-146758770 posted

    change grid.items to grid.rows on line 23

    Wednesday, April 7, 2010 11:55 AM
  • User-395912426 posted

    That didn't work. In fact Rows is not valid there. That part seems to be working just find, that is where i search the datagrid to find the dropdownlist which I am attempting to populate. It finds it, just doesn't fill the list.

    Thanks.

    Wednesday, April 7, 2010 9:02 PM
  • User-395912426 posted

    I figured it out. Embarassed My select statement was returning only one value, so naturally the DropDown contained only one value. Oops.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 7, 2010 9:45 PM