Results 1 to 3 of 3

Thread: populate dropdownlist in datagrid

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    populate dropdownlist in datagrid

    Really struggline with how to populate a dropdownlist in a datagrid when i click the edit link:

    VB Code:
    1. Sub dgrdResults_PageIndexChanged (s as Object, e as DataGridPageChangedEventArgs)
    2.     dgrdResults.CurrentPageIndex = e.NewPageIndex
    3.     BindDataGrid
    4. End Sub
    5.  
    6. Sub dgrdResults_CancelCommand(s as Object, e as DataGridCommandEventArgs)
    7.     dgrdResults.EditItemIndex = -1
    8.     BindDataGrid
    9. End Sub
    10.  
    11. Sub dgrdResults_UpdateCommand(s as Object, e as DataGridCommandEventArgs)
    12.     Dim TempList as DropDownList
    13.     Dim TempValue as String
    14.    
    15.     TempList = e.Item.FindControl("drpUnitRating")
    16.     TempValue = TempList.SelectedItem.Value
    17.    
    18.     dgrdResults.EditItemIndex = -1
    19.     BindDataGrid
    20. End Sub
    21.  
    22. Function BindRating()
    23.     'Dim colArrayList as  New ArrayList
    24.     'colArrayList.Add("5")
    25.     'colArrayList.Add("4")
    26.     'colArrayList.Add("3")
    27.     'colArrayList.Add("2")
    28.     'colArrayList.Add("1")
    29.     'drpUnitRating.DataSource = colArrayList
    30.     'drpUnitRating.DataBind()
    31. End Function
    32.  
    33. Sub dgrdResults_EditCommand(s as Object, e as DataGridCommandEventArgs)
    34.     dgrdResults.EditItemIndex = e.Item.ItemIndex
    35.     BindDataGrid
    36. End Sub
    37.  
    38. ......
    39.  
    40. <asp:DataGrid
    41.                 ID="dgrdResults"
    42.                 AllowPaging="true"
    43.                 AutoGenerateColumns="false"
    44.                 PagerStyle-Mode="NumericPages" PagerStyle-PageButtonCount="5" PagerStyle-Position="Bottom"
    45.                 PageSize="25"
    46.                 OnPageIndexChanged="dgrdResults_PageIndexChanged"
    47.                 OnEditCommand="dgrdResults_EditCommand"
    48.                 OnUpdateCommand="dgrdResults_UpdateCommand"
    49.                 OnCancelCommand="dgrdResults_CancelCommand"
    50.                 CellPadding="0"
    51.                 BorderWidth="0"
    52.                 GridLines="None" CssClass="../fmcss.css"
    53.                 runat="server" >
    54.                   <columns>
    55.                   <asp:TemplateColumn>
    56.                     <itemTemplate>
    57.                       <%# Container.DataItem("PRD_BRAND") %>&nbsp;<%# Container.DataItem("PRD_MODEL") %>&nbsp;<%# Container.DataItem("PRD_NAME") %>&nbsp;<%# Container.DataItem("PRD_CATEGORY_CD") %>
    58.                     </itemTemplate>
    59.                   </asp:TemplateColumn>
    60.                   <asp:TemplateColumn>
    61.                     <itemTemplate>
    62.                       <%# Container.DataItem("UNIT_RATING") %>
    63.                     </itemTemplate>
    64.                     <edititemtemplate>
    65.                         <asp:DropDownList ID="drpUnitRating" DataTextField='<%# Container.DataItem("UNIT_RATING") %>' DataValueField='<%# Container.DataItem("UNIT_RATING") %>' DataSource='<%# BindRating %>' runat="server"></asp:DropDownList>
    66.                     </edititemtemplate>
    67.                   </asp:TemplateColumn>
    68.                   <asp:EditCommandColumn EditText="E" UpdateText="U" CancelText="C" />
    69.                   </columns>
    70.                 </asp:DataGrid>

    I can do it either by connecting to a database or by just typing in say 5 values 1-5. I will be doing it both ways once I figure out how to do this. Right now I am just trying to just do it without the connecting to a database.

    Thanks in advance to any replies.

  2. #2
    New Member
    Join Date
    Jul 2004
    Location
    Lansing, MI
    Posts
    11

    Re: populate dropdownlist in datagrid


  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    Re: populate dropdownlist in datagrid

    Kevin,

    Nice link.

    Anyone know how to translate this to vb.net?
    VB Code:
    1. private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    2. {
    3.    // see if we are working with the row being edited
    4.    if (e.Item.ItemType == ListItemType.EditItem)
    5.    {
    6.       // populate the division DDL
    7.       DropDownList ddlDivision = (DropDownList) e.Item.FindControl("ddlDivision");
    8.      
    9.       // connect to Access DB
    10.       OleDbConnection myConnection = new OleDbConnection(ConfigurationSettings.AppSettings["connString"]);
    11.       myConnection.Open();
    12.  
    13.       const string SQL = @"SELECT DivisionID, Name FROM Divisions ORDER BY Name";
    14.       OleDbCommand myCommand = new OleDbCommand(SQL, myConnection);
    15.  
    16.       ddlDivision.DataSource = myCommand.ExecuteReader();
    17.       ddlDivision.DataBind();
    18.  
    19.       myConnection.Close();
    20.  
    21.       // now, select the appropriate division
    22.       int currentDivisionID = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "DivisionID"));
    23.       ListItem li = ddlDivision.Items.FindByValue(currentDivisionID.ToString());
    24.       if (li != null) li.Selected = true;
    25.  
    26.       // finally, populate the department DDL based on the selected division
    27.       int currentDepartmentID = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "DepartmentID"));
    28.       if (li != null) li.Selected = true;
    29.  
    30.       DropDownList ddlDepartment = (DropDownList) e.Item.FindControl("ddlDepartment");
    31.  
    32.       PopulateDepartmentBasedOnDivision(ddlDepartment, ddlDivision, currentDepartmentID);
    33.    }
    34. }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width