tags:

views:

29

answers:

2

work on C# asp.net vs05. i take a combo on gridview template field.

<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource3">
            <Columns>
                <asp:BoundField DataField="StudentID" HeaderText="StudentID" ReadOnly="True" SortExpression="StudentID" />
                <asp:TemplateField HeaderText="DivisionName" SortExpression="DivisionName">
                    <ItemTemplate>
                        <asp:Label ID="lblDivisionName" runat="server" Text='<%# Bind("DivisionName") %>'
                            Width="116px"></asp:Label><br />
                        <asp:DropDownList ID="DropDownList1" runat="server">
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="StudentName" SortExpression="StudentName">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("StudentName") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("StudentName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:ButtonField ButtonType="Button" Text="Update" />
            </Columns>
        </asp:GridView>

Now i want to fill this combo by the bellow code ?

 DropDownList1.DisplayMember = "CommercialRegionName";
            foreach (object oItem in collection)
            {
                DropDownList1.Items.Add(oItem);
            }

how to get the combo control id from the grid

A: 

A good article

DropDownList inside a GridView (or DataGrid)

rahul
+1  A: 

You can use the following code: Set OnRowDataBound of the gridview in aspx as OnRowDataBound="GridView3_RowDataBound"

Then put the following code in aspx.cs page.

protected  void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList DropDownList1 =(DropDownList) e.Row.FindControl("DropDownList1");
            DropDownList1.DisplayMember = "CommercialRegionName";
            foreach (object oItem in collection)
            {
                DropDownList1.Items.Add(oItem);
            }

        }
    }
Himadri