edcod81
edcod81

Reputation: 19

Adding a drop down in itemtemplate and populating value dynamically

I have an item template in a GridView and I want to create a dropdown for each row and bind it to a value retrieved from the database.. but I am not sure how to do this.. this is what i have so far.. but not sure where to put the code to populate the drop down per row..

 <ItemTemplate>
     <asp:DropDownList runat="server" ID="ddlMyQuantity" SelectedValue='<%# 
       (DataBinder.Eval(Container.DataItem,"Quantity")) %>'>
     </asp:DropDownList>
  </ItemTemplate>

and in code behind, not sure how to or where to put this so that it is created on every row..

 public void BindMyQuantity()
    {
        for (int i = 1; i < 15; i++)
        {
            ddlMyQuantity.Items.Add(i.ToString());
        }
    }

Also i am not sure if i can do that, but the code is not complaining.. adding SelectedValue in the asp declaration

Upvotes: 1

Views: 3605

Answers (3)

DEV-001
DEV-001

Reputation: 1

<asp:TemplateField HeaderText="Type Cargo" HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center"> 
                <ItemTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server" DataValueField="DESCRIPTION"></asp:DropDownList>

                </ItemTemplate>
            </asp:TemplateField> 

Code Behind will look like this...

Dim rowId As DropDownList

        For i As Integer = 0 To gridView1.Rows.Count - 1

            Dim gridrow As GridViewRow = gridView1.Rows(i)

            rowId = DirectCast(gridrow.FindControl("DropDownList1"), DropDownList)
            Dim dt As DataTable = Get_List()
            rowId.DataSource = dt
            rowId.DataBind()

        Next

Get_List is a Function that returns a DataTable

Upvotes: 0

TheCodeKing
TheCodeKing

Reputation: 19220

You can use OnRowDataBound to dynamically bind your dropdown:

protected void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
  if(e.Row.RowType == DataControlRowType.DataRow)
  {
     var dropdownList = (DropDownList)e.Row.FindControl("ddlMyQuantity");
     for (int i = 1; i < 15; i++)
     {
        dropdownList.Items.Add(i.ToString());
     }
     dropdownList.SelectedValue = 
           Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Quantity"));
  }
}

Add binding:

<asp:GridView ... OnRowDataBound="GridView_RowDataBound">
    ...
</asp:GridView>

Upvotes: 1

Praveen
Praveen

Reputation: 1449

As per my knowledge, the best option would be to write this code in "RowDataBound" event of the Grid. See sample code below.

   protected void GridView_RowDataBound(..)
   {
       if (e.Row.RowType.Equals(DataControlRowType.DataRow))
        {
            DropDownList ddl = e.Row.FindControl("ddlMyQuantity") as DropDownList;

            if (ddl != null)
            {
               for (int i = 1; i < 15; i++)
               {
                   ddl.Items.Add(i.ToString());
               }
            }
        }
   }

In the aspx page, provide this

   <ItemTemplate>        
        <asp:DropDownList runat="server" ID="ddlMyQuantity"></asp:DropDownList> 
   </ItemTemplate> 

Hope this Helps!!

Upvotes: 0

Related Questions