在编辑行时无法在gridview中绑定多个下拉列表

时间:2017-01-05 07:48:11

标签: c# asp.net gridview datagridview rowdatabound

我在gridview中有两列return position;Customer Type。当网格处于正常模式时,我使用标签显示值。在编辑行时,这两列成为下拉列表。我使用File Frequency绑定下拉列表。但是,只有OnRowDataBound="RowDataBound"方法中的第一个下拉列表(在方法中首先编写)才能在编辑模式下获得绑定。

的.aspx

RowDataBound

的.cs

<asp:GridView ID="gvManageCustomers" DataKeyNames="Ship_To" runat="server" AutoGenerateColumns="False"
    OnRowEditing="EditCustomer" OnRowDataBound="RowDataBound" OnRowUpdating="UpdateCustomer"
    OnRowCancelingEdit="CancelEdit" CssClass="table table-bordered table-condensed">
    <Columns>
        <asp:TemplateField HeaderText="Customer Type">
            <ItemTemplate>
                <asp:Label ID="lblCustType" runat="server" Text='<%# Eval("Customer_Type")%>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:Label ID="lblCustType" runat="server" Text='<%# Eval("Customer_Type")%>' Visible="false">
                </asp:Label>
                <asp:DropDownList ID="ddlgvCustomerType" runat="server">
                </asp:DropDownList>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="File Frequency">
            <ItemTemplate>
                <asp:Label ID="lblFileFreq" runat="server" Text='<%# Eval("FileFrequency")%>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:Label ID="lblFileFreq" runat="server" Text='<%# Eval("FileFrequency")%>' Visible="false"></asp:Label>
                <asp:DropDownList ID="ddlgvFileFreq" runat="server">
                </asp:DropDownList>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowEditButton="True" />
    </Columns>
</asp:GridView>

2 个答案:

答案 0 :(得分:0)

问题在于这一行,它提出了一个例外。这就是为什么只有最顶层的绑定才有效。

FATAL EXCEPTION: main
                                               Process: com.example.cosmic.zumi_test, PID: 534
                                               java.lang.NullPointerException: Attempt to invoke interface method 'int android.database.Cursor.getColumnIndex(java.lang.String)' on a null object reference
                                                   at com.example.cosmic.zumi_test.Contact_FRAGMENT3.getPlayers(Contact_FRAGMENT3.java:243)
                                                   at com.example.cosmic.zumi_test.Contact_FRAGMENT3.access$100(Contact_FRAGMENT3.java:57)
                                                   at com.example.cosmic.zumi_test.Contact_FRAGMENT3$1.onDataChange(Contact_FRAGMENT3.java:157)
                                                   at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:45)
                                                   at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
                                                   at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
                                                   at android.os.Handler.handleCallback(Handler.java:815)
                                                   at android.os.Handler.dispatchMessage(Handler.java:104)
                                                   at android.os.Looper.loop(Looper.java:194)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5643)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

但看起来你希望DropDownList拥有正确的ddlgvFileFreq.Items.FindByValue((e.Row.FindControl("lblFileFreq") as Label).Text).Selected = true; 。这就是你如何做到的。

SelectedValue

答案 1 :(得分:-1)

 <asp:TemplateField HeaderText="Customer Type">
                                    <ItemTemplate>
                                        <asp:Label ID="lblCustType" runat="server" Text='<%# Eval("Customer_Type")%>'></asp:Label>
                                        <asp:Label ID="lblCustTypeID" runat="server" Visible="false" Text='<%# (Eval("Customer_TypeID")) %>'  />
                                    </ItemTemplate>
                                    <EditItemTemplate>                                     
                                        <asp:DropDownList ID="ddlgvCustomerType" runat="server" DataSource="<%# FetchCustomerType() %>" DataValueField="Customer_TypeID" DataTextField="Customer_Type"  >
                                        </asp:DropDownList>
                                    </EditItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="File Frequency">
                                    <ItemTemplate>
                                        <asp:Label ID="lblFileFreq" runat="server" Text='<%# Eval("FileFrequency")%>'></asp:Label>
                                        <asp:Label ID="lblFileFreqID" runat="server" Visible="false" Text='<%# (Eval("FileFrequency_ID")) %>'  />
                                    </ItemTemplate>
                                    <EditItemTemplate>                                       
                                        <asp:DropDownList ID="ddlgvFileFreq" runat="server" DataSource="<%# FetchFileFrequency() %>" DataValueField="FileFrequency_ID" DataTextField="FileFrequency" >
                                        </asp:DropDownList>
                                    </EditItemTemplate>
                                </asp:TemplateField>

在onRowEditing事件中

((DropDownList)gvManageCustomers.Rows[e.NewEditIndex].Cells[0].FindControl("ddlgvCustomerType")).SelectedValue = ((Label)gvManageCustomers.Rows[e.NewEditIndex].Cells[0].FindControl("lblCustTypeID")).Text;
                            ((DropDownList)gvManageCustomers.Rows[e.NewEditIndex].Cells[0].FindControl("ddlgvFileFreq")).SelectedValue = ((Label)gvManageCustomers.Rows[e.NewEditIndex].Cells[0].FindControl("lblFileFreqID")).Text;