Listbox inside gridview

vbhargav80
vbhargav80 used Ask the Experts™
on
Dear Experts,

I am using linq to SQL to bind my result to a gridview. I have a customers table and each customer can have multiple orders. When I query the customers table using linq, it should automatically return the list of orders for each customers as they have a foreign key relationship.

The problem is how do i bind my orders list to a listbox control inside a dridview column.

Can you please help? Followng is my code
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField HeaderText="ID" DataField="ID" />
        <asp:BoundField HeaderText="Name" DataField="Name" />
        <asp:TemplateField HeaderText="Orders">
            <ItemTemplate>
                <asp:ListBox ID="lstOrders" runat="server" DataSource="WHAT SHOULD GO HERE">
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>       
</asp:GridView>
 
In codebehind this is how i bind
 
DBContext context = new DBContext ();
            var customers = from c in context.Customers
                          select new {c.Name, c.ID, c.Orders};
 
            gvCustomers.DataSource = customers ;
            this.DataBind();

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Developer User Interface
Commented:
U could handle the rowdatabound event of the gridview.

Write this code...

ListBox list = e.Row.FindControl("listboxname") as ListBox;

list.DataSource= ds;
......

Author

Commented:
Thanks so much!

Works Perfectly!!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial