ASP.net gridview编辑模式中的下拉列表问题

时间:2014-08-11 19:18:20

标签: asp.net vb.net gridview

我有一个asp.net gridview,它给了我适合。我正在将数据拉入网格而没有任何问题。但是,当我点击编辑时,我收到此错误:

 DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'wrkCode'.

我的目的是允许用户使用下拉列表编辑群组的工作时间表。这是我的代码:

<asp:GridView ID="grdShowGroups" runat="server" datakeynames="grpID" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="5" GridLines="Vertical" CellSpacing="5" Width="700px" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="grdShowGroups_SelectedIndexChanged">
    <AlternatingRowStyle BackColor="#DCDCDC" />
        <Columns>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" HeaderText="Edit" />
            <asp:BoundField DataField="grpID" HeaderText="grpID" SortExpression="grpID" InsertVisible="False" ReadOnly="True" Visible="false" />
            <asp:BoundField DataField="grpStartTime" HeaderText="Start Time" SortExpression="grpStartTime" />
            <asp:BoundField DataField="grpEndTime" HeaderText="End Time" SortExpression="grpEndTime" />
            <asp:TemplateField HeaderText="Work Schedule" SortExpression="wrkSchedule">
                <EditItemTemplate>
                    <asp:DropDownList ID="drpWrkSchedule" runat="server" DataSourceID="SqlDataSource2" DataTextField="wrkDescription" DataValueField="wrkCode" SelectedValue='<%#Bind("wrkCode")%>' AppendDataBoundItems="true" AutoPostBack="true"></asp:DropDownList>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblWrkSchedule" runat="server" Text='<%# Bind("wrkSchedule") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="grpDescription" HeaderText="Description" SortExpression="grpDescription" />
            <asp:CommandField ShowSelectButton="True" />
        </Columns>
        <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
        <HeaderStyle BackColor="#000000" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
        <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#0000A9" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#000065" />

</asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:EmployeeMGTConnectionString %>" SelectCommand="SELECT [grpID], [grpStartTime], [grpEndTime], [wrkSchedule], [grpDescription] FROM [empGroups]" DeleteCommand="DELETE FROM [empGroups] WHERE [grpID] = @grpID" InsertCommand="INSERT INTO [empGroups] ([grpStartTime], [grpEndTime], [wrkSchedule], [grpDescription]) VALUES (@grpStartTime, @grpEndTime, @wrkSchedule, @grpDescription)" UpdateCommand="UPDATE [empGroups] SET [grpStartTime] = @grpStartTime, [grpEndTime] = @grpEndTime, [wrkSchedule] = @wrkSchedule, [grpDescription] = @grpDescription WHERE [grpID] = @grpID">
        <DeleteParameters>
            <asp:Parameter Name="grpID" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter DbType="DateTime" Name="grpStartTime" />
            <asp:Parameter DbType="DateTime" Name="grpEndTime" />
            <asp:Parameter Name="wrkSchedule" Type="String" />
            <asp:Parameter Name="grpDescription" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter DbType="DateTime" Name="grpStartTime" />
            <asp:Parameter DbType="DateTime" Name="grpEndTime" />
            <asp:Parameter Name="wrkSchedule" Type="String" />
            <asp:Parameter Name="grpDescription" Type="String" />
            <asp:Parameter Name="grpID" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
 <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:EmployeeMGTConnectionString %>" SelectCommand="SELECT [wrkCode], [wrkDescription], [wrkID] FROM [wrkSchedule]"></asp:SqlDataSource>

如果我使用此代码运行下拉列表:

<asp:DropDownList ID="drpWrkSchedule" runat="server" DataSourceID="SqlDataSource2" DataTextField="wrkDescription" DataValueField="wrkCode" SelectedValue='<%#Bind("wrkCode")%>'>

我收到错误。如果我在没有“SelectedValue ='&lt;%#Bind(”wrkCode“)%&gt;'”代码的情况下运行它,它会根据我的需要显示下拉列表,只是没有连接到当前数据中的数据的选定值在表中。

我看过来自网络上的几个例子,可能是因为我错过了一些小事。我只是想不出来。

谢谢!

1 个答案:

答案 0 :(得分:1)

您需要在sqldatasource1中选择wrkCode,否则无需绑定。因此,只需在该sql语句的select子句中包含wrkCode,就可以了。