将嵌套Gridview与Radiobuttonlist绑定

问题描述 投票:0回答:1

我有一个嵌套的

Gridiew
。家长显示
customers
,孩子显示该
customer
的每个部分。

信息:

grdViewCustomers
显示
customers

grdviewordersofcustomer
显示每个
customer
的部件。

ChildGridview
中,我有一个
RadioButtonList
,因此您可以选择它是什么零件类型。

我有

ChildGridview
和父级数据绑定,使用
grdviewcustomers_onrowdatabound
中的
.cs

我可以从

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个回答
0
投票

您应该将每个内容的代码移至 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("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>

请记住,如果您获取的值与查询中的值不同,您将会收到错误。

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

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

© www.soinside.com 2019 - 2024. All rights reserved.