【问题标题】:Bind Nested Gridview with Radiobuttonlist使用 Radiobuttonlist 绑定嵌套的 Gridview
【发布时间】:2015-08-28 19:11:06
【问题描述】:

我有一个嵌套的Gridiew。父级显示customers,子级显示customer 的每个部分。

有关信息:
grdViewCustomers 显示customers
grdviewordersofcustomer 显示每个customer 的部分。

ChildGridview 我有一个RadioButtonList,所以你可以选择它是什么零件类型。

我在.cs 中有ChildGridview 和使用grdviewcustomers_onrowdatabound 的父数据绑定。

我可以从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>

【问题讨论】:

    标签: c# asp.net gridview nested radiobuttonlist


    【解决方案1】:

    您应该将每个内容的代码移动到 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(Editable).Selected = [True]

    【讨论】:

    • 遗憾的是,这对我需要绑定 RadioButtonList 的事实没有帮助。
    • 编辑 OP 以包含 ASPX
    • 将绑定添加到我的答案中。在最后一个建议中,我假设您要将其绑定到您进入可编辑字符串的值。希望对您有所帮助。
    • 非常感谢您。它非常适合将其拉入。现在将其保存到数据库不起作用,但我会研究一下。再次感谢您的宝贵时间!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-10
    • 1970-01-01
    • 2013-02-18
    • 2015-12-06
    • 1970-01-01
    • 2021-08-25
    • 2010-09-22
    相关资源
    最近更新 更多