Search code examples
c#asp.netgridviewnestedradiobuttonlist

Bind Nested Gridview with Radiobuttonlist


I have a nested Gridiew. The parent shows customers and the child shows each part for that customer.

For information :
grdViewCustomers shows the customers.
grdviewordersofcustomer shows the parts for each customer.

In the ChildGridview I have a RadioButtonList so you can select what part type it is.

I have the ChildGridview and Parent databind using grdviewcustomers_onrowdatabound in .cs.

I can get value from RadioButtonList. However I can not pull data from SQL and set to appropriate value on page load. Could anyone assist? I feel close but no gold yet.

protected void grdViewCustomers_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string customerID = grdViewCustomers.DataKeys[e.Row.RowIndex].Value.ToString();
        GridView grdViewOrdersOfCustomer = (GridView)e.Row.FindControl("grdViewOrdersOfCustomer");
        grdViewOrdersOfCustomer.DataSource = SelectData("select * from [dbo].[View_Budget_Setup_2_Items] WHERE CUSID='" + customerID + "' order by id_item");
        grdViewOrdersOfCustomer.DataBind();

        foreach (GridViewRow rowChild in grdViewOrdersOfCustomer.Rows)
        {
            string ChildID = grdViewOrdersOfCustomer.DataKeys[rowChild.RowIndex].Value.ToString();
            string Editable = dbUtilities.SalesEditableID(ChildID);

            if (Editable == "Bud")
            {
              // Set values here

            }
        }


        grdViewOrdersOfCustomer.DataBind();
        //testing
    }
}

ASPX side

<div>
    <asp:GridView ID="grdViewCustomers" runat="server" AutoGenerateColumns="false" DataKeyNames="ID"
         CssClass="Grid" EmptyDataText="No Budgets To Approve" OnRowDataBound="grdViewCustomers_OnRowDataBound">
        <Columns>
            <asp:TemplateField ItemStyle-Width="20px">
                <ItemTemplate>
                    <a href="JavaScript:divexpandcollapse('div<%# Eval("ID") %>');">
                        <img alt="Details" id='imgdiv<%# Eval("ID") %>' src="images/plus.png" />
                    </a>
                    <div id='div<%# Eval("ID") %>' style="display: none;">
                        <asp:GridView ID="grdViewOrdersOfCustomer" runat="server" AutoGenerateColumns="false"
                            DataKeyNames="ID" CssClass="ChildGrid" OnRowDataBound="grdViewOrdersOfCustomer_RowDataBound">
                            <Columns>
                                <asp:BoundField ItemStyle-Width="150px" DataField="ID" HeaderText="ID" Visible="False" />
                                <asp:BoundField ItemStyle-Width="150px" DataField="ID_CUST_SOLDTO" HeaderText="Customer Name" />
                                <asp:BoundField ItemStyle-Width="100px" DataField="id_item" HeaderText="Item" />
                                <asp:BoundField ItemStyle-Width="75px" DataField="PN5YB" HeaderText="-5 Back" />
                                <asp:BoundField ItemStyle-Width="75px" DataField="PN4YB" HeaderText="-4 Back" />
                                <asp:BoundField ItemStyle-Width="75px" DataField="PN3YB" HeaderText="-3 Back" />
                                <asp:BoundField ItemStyle-Width="75px" DataField="PN2YB" HeaderText="-2 Back" />
                                <asp:BoundField ItemStyle-Width="75px" DataField="PN1YB" HeaderText="-1 Back" />
                                <asp:BoundField ItemStyle-Width="75px" DataField="PN0YB" HeaderText="This Year" />
                                <asp:BoundField ItemStyle-Width="75px" DataField="Area" HeaderText="Area" />
                                <asp:TemplateField HeaderText="Edit">
                                    <ItemTemplate>
                                        <asp:RadioButtonList ID="RadioCusListC" runat="server" RepeatDirection="Horizontal">
                                            <asp:ListItem Text="Bud" />
                                            <asp:ListItem Text="B/O" />
                                            <asp:ListItem Text="B/G" />
                                            <asp:ListItem Text="DNB" />
                                        </asp:RadioButtonList>
                                    </ItemTemplate>
                                </asp:TemplateField> 
                            </Columns>
                        </asp:GridView>
                    </div>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="ID_CUST_SOLDTO" SortExpression="ID_CUST_SOLDTO" ItemStyle-Width="90px" />
                <asp:BoundField DataField="PN5YB" SortExpression="PN5YB" DataFormatString="{0:C0}" ItemStyle-Width="90px" ItemStyle-HorizontalAlign="Right" />
                <asp:BoundField DataField="PN4YB" SortExpression="PN4YB" DataFormatString="{0:C0}" ItemStyle-Width="90px" ItemStyle-HorizontalAlign="Right" />
                <asp:BoundField DataField="PN3YB" SortExpression="PN3YB" DataFormatString="{0:C0}" ItemStyle-Width="90px" ItemStyle-HorizontalAlign="Right" />
                <asp:BoundField DataField="PN2YB" SortExpression="PN2YB" DataFormatString="{0:C0}" ItemStyle-Width="90px" ItemStyle-HorizontalAlign="Right" />
                <asp:BoundField DataField="PN1YB" SortExpression="PN1YB" DataFormatString="{0:C0}" ItemStyle-Width="90px" ItemStyle-HorizontalAlign="Right" />
                <asp:BoundField DataField="PN0YB" SortExpression="PN0YB" DataFormatString="{0:C0}" ItemStyle-Width="90px" ItemStyle-HorizontalAlign="Right" />
                <asp:BoundField DataField="area" SortExpression="Area" ItemStyle-Width="90px" ItemStyle-HorizontalAlign="Right" />
            <asp:TemplateField HeaderText="Edit">
                <ItemTemplate>
                    <asp:RadioButtonList ID="RadioCusList"  runat="server" RepeatDirection="Horizontal">
                        <asp:ListItem Text="Bud" />
                        <asp:ListItem Text="B/O" />
                        <asp:ListItem Text="B/G" />
                        <asp:ListItem Text="DNB" />
                        <asp:ListItem Text="Err" />
                </asp:RadioButtonList>
                </ItemTemplate>
                </asp:TemplateField>
        </Columns>
    </asp:GridView>

   </div>

Solution

  • you should move the for each content code to the onrowdatabound event of the grdViewOrdersOfCustomer GridView so you get each row AFTER the bind.

    protected void grdViewCustomers_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
        string customerID = grdViewCustomers.DataKeys[e.Row.RowIndex].Value.ToString();
        GridView grdViewOrdersOfCustomer = (GridView)e.Row.FindControl("grdViewOrdersOfCustomer");
        grdViewOrdersOfCustomer.DataSource = SelectData("select * from [dbo].[View_Budget_Setup_2_Items] WHERE CUSID='" + customerID + "' order by id_item");
        grdViewOrdersOfCustomer.DataBind();
      }
    }
    
    protected void grdViewOrdersOfCustomer_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
        string ChildID = (GridView)sender.DataKeys[e.row.RowIndex].Value.ToString();
        string Editable = dbUtilities.SalesEditableID(ChildID);
        if (Editable == "Bud")
        {
          // Set values here
         }
      }
    }
    

    As binding the radiobuttonlist, you could do this on the aspx code like this:

    <asp:RadioButtonList ID="RadioCusListC" runat="server" SelectedValue='<%# eval("ColumnName")%>'>
      <asp:ListItem>DNB</asp:ListItem>
      <asp:ListItem>B/G</asp:ListItem>
      <asp:ListItem>B/O</asp:ListItem>
      <asp:ListItem>BUD</asp:ListItem>
    </asp:RadioButtonList>
    

    Remember that if you get values other then those from the query, you are going to get an error.

    You cold also find the control on the grdViewOrdersOfCustomer_OnRowDataBound and do this:

    RadioCusListC.Items.FindByValue(Editable).Selected = [True]