将嵌套的 Gridview 与 Radiobuttonlist 绑定

Bind Nested Gridview with Radiobuttonlist

我有一个嵌套的 Gridiew。 parent 显示 customers,child 显示 customer.

的每个部分

供参考:
grdViewCustomers 显示 customers.
grdviewordersofcustomer 显示每个 customer.

的部分

ChildGridview 我有一个 RadioButtonList 所以你可以 select 它是什么类型。

我在 .cs 中使用 grdviewcustomers_onrowdatabound 进行了 ChildGridview 和 Parent 数据绑定。

我可以从 RadioButtonList 中获取价值。但是我无法从 SQL 中提取数据并在页面加载时设置为适当的值。有人可以帮忙吗?我感觉很接近,但还没有金子。

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 端

<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>

您应该将每个内容代码移动到 grdViewOrdersOfCustomer GridView 的 onrowdatabound 事件,以便在绑定后获取每一行。

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

            }
        }
}

作为绑定单选按钮列表,您可以像这样在 aspx 代码上执行此操作:

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

请记住,如果您获得的值不是查询中的值,您将收到错误消息。

你还可以找到 grdViewOrdersOfCustomer_OnRowDataBound 上的控件并执行此操作:

RadioCusListC.Items.FindByValue(可编辑).Selected = [True]