tags:

views:

47

answers:

1

Hi,

     I have a scenario where I will be showing a Customer data in a GridView. I have a Dictionary collection like this IDictionary<Customer, IList<CustomerOrder>>. 

First Question Can I bind this dictionary to the ASP.Net GridView?

Now next question, I want to show following kind of UI i.e. Show multiple CustomerOrders in one row of Customer.

FName LastName OrderNumber MarkOrderCompleted(RadioButton) ABC DEF 123 Radio Buttons Yes No 345 Radio Buttons Yes No 678 Radio Buttons Yes No GHI JKL 213 Radio Buttons Yes No 546 Radio Buttons Yes No 768 Radio Buttons Yes No

FName and LastName would be populated from Customer object inside the Dictionary and OrderNumber and MarkOrderCompleted would be populated from CustomerOrders. Below all thre is a submit button, which saves the radion buton status values to the database against the Order. Immediate help would be really appreciated. Thanks in advance...

+1  A: 

Do you have to have both Customer and CustomerOrder in the same Dictionary? I would have used a nested grid with two different collections. The nested grid uses the "DataKeyNames" attribute of the first grid as parameter. Example:

The Grid:

   <asp:GridView ID="CustomerGrid" runat="server" EnableModelValidation="True" 
        onrowdatabound="CustomerGrid_RowDataBound" DataKeyNames="CustomerId">
        <Columns>
            <asp:TemplateField HeaderText="Orders">
                <ItemTemplate>
                    <asp:GridView ID="OrderGrid" runat="server">
                    </asp:GridView>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

Code behind:

    List<Customer> CustomerList = new List<Customer>();
    List<Orders> Orders = new List<Orders>();

    protected void Page_Load(object sender, EventArgs e)
    {
        // Add some customers
        CustomerList.Add(new Customer() { CustomerId = 1, CompanyName = "TestCompany", FirstName = "John", LastName = "West" });
        CustomerList.Add(new Customer() { CustomerId = 2, CompanyName = "AnotherCompany", FirstName = "Alan", LastName = "East" });

        // Add some orders
        Orders.Add(new Orders() { CustomerId = 1, OrderId = 1, OrderDate = DateTime.Now, Sum = 300 });
        Orders.Add(new Orders() { CustomerId = 1, OrderId = 2, OrderDate = DateTime.Now, Sum = 600 });
        Orders.Add(new Orders() { CustomerId = 2, OrderId = 3, OrderDate = DateTime.Now, Sum = 2000 });
        Orders.Add(new Orders() { CustomerId = 2, OrderId = 4, OrderDate = DateTime.Now, Sum = 100 });

        // Bind customerlist to grid
        CustomerGrid.DataSource = CustomerList;
        CustomerGrid.DataBind();
    }

    protected void CustomerGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // Get customer id for row
            int customerId = (int)CustomerGrid.DataKeys[e.Row.RowIndex].Value;

            // Get the nested grid control
            GridView OrderGrid = e.Row.FindControl("OrderGrid") as GridView;

            // Get orders for customer
            OrderGrid.DataSource = Orders.Where(o => o.CustomerId == customerId);
            OrderGrid.DataBind();
        }
    }
Svendberg
Thanks This was what i needed....
Harshal
One thing i some one one can help. What if i have more than one columns in the innerGridView and the same has to be matched to the HeaderText of the parent GridView. How can this be achieved?
Harshal
I have some problems understanding what you want to achieve. Could you give a practical example?
Svendberg
i got the solution that is for header column separate style needs to be written.
Harshal