【问题标题】:Dropdown List issue in ASP.net gridview edit modeASP.net gridview 编辑模式下的下拉列表问题
【发布时间】:2014-10-04 17:19:32
【问题描述】:

我有一个适合我的 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=''”代码的情况下运行它,它会根据需要显示下拉列表,只是没有选择的值连接到当前数据中的数据表。

我查看了网络上的几个示例,可能只是缺少一些小东西。我就是想不通。

谢谢!

【问题讨论】:

  • 你没有显示gridview

标签: asp.net vb.net gridview


【解决方案1】:

你需要在你的 sqldatasource1 中选择 wrkCode 否则它没有什么可以绑定的。因此,只需在该 sql 语句的 select 子句中包含 wrkCode 即可。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-26
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 1970-01-01
    • 2011-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多