简体   繁体   English

使用Radiobuttonlist绑定嵌套的Gridview

[英]Bind Nested Gridview with Radiobuttonlist

I have a nested Gridiew . 我有一个嵌套的Gridiew The parent shows customers and the child shows each part for that customer . 父级显示customers ,子级显示该customer每个零件。

For information : 有关信息:
grdViewCustomers shows the customers . grdViewCustomers显示customers
grdviewordersofcustomer shows the parts for each customer . grdviewordersofcustomer显示每个customer的零件。

In the ChildGridview I have a RadioButtonList so you can select what part type it is. ChildGridview我有一个RadioButtonList因此您可以选择它的零件类型。

I have the ChildGridview and Parent databind using grdviewcustomers_onrowdatabound in .cs . 我在.cs使用grdviewcustomers_onrowdataboundChildGridview和Parent databind。

I can get value from RadioButtonList . 我可以从RadioButtonList获得价值。 However I can not pull data from SQL and set to appropriate value on page load. 但是我无法从SQL中提取数据并在页面加载时设置为适当的值。 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 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>

you should move the for each content code to the onrowdatabound event of the grdViewOrdersOfCustomer GridView so you get each row AFTER the bind. 您应该将每个内容代码的移动到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 } } } 

As binding the radiobuttonlist, you could do this on the aspx code like this: 绑定单选按钮列表后,您可以在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> 

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: 您还可以在grdViewOrdersOfCustomer_OnRowDataBound上找到控件,并执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM