gridview删除和allowpaging问题

时间:2009-02-10 21:14:58

标签: asp.net vb.net

我已经搜索过,现在对我的问题的原因感到茫然。

这是我的问题的简短版本。

我有一个gridview,一个用于打开/关闭gridview分页的复选框和一个带有select和delete查询的sql数据源。如果打开了分页,我可以在gridview中单击删除行,它会运行并且页面刷新没有问题。如果我为gridview关闭分页,当我对sql server中的表进行直接查询但是页面无法重新加载时,删除查询似乎会运行。我收到以下错误:

  

指定的参数不在   有效值范围。参数名称:   值描述:未处理   期间发生了异常   执行当前的Web请求。   请查看堆栈跟踪了解更多信息   有关错误的信息和位置   它起源于代码。

     

异常详细信息:   System.ArgumentOutOfRangeException:   指定的论点不合时宜   有效值范围。参数名称:   值

     

来源错误:

     

生成了未处理的异常   在执行当前   网络请求。有关的信息   异常的起源和位置   可以使用例外来识别   堆栈跟踪下面。

     

堆栈追踪:

     

[ArgumentOutOfRangeException:   指定的论点不合时宜   有效值范围。参数名称:   值]
  System.Web.UI.WebControls.GridView.set_SelectedIndex(的Int32   价值)+280
  System.Web.UI.WebControls.GridView.HandleDeleteCallback(的Int32   affectedRows,exception ex)+352
  System.Web.UI.DataSourceView.Delete(IDictionary的   钥匙,IDictionary oldValues,   DataSourceViewOperationCallback   回调)+119
  System.Web.UI.WebControls.GridView.HandleDelete(GridViewRow   row,Int32 rowIndex)+938
  System.Web.UI.WebControls.GridView.HandleEvent(EventArgs的   e,Boolean causeValidation,String   validationGroup)+1152
  System.Web.UI.WebControls.GridView.RaisePostBackEvent(字符串   eventArgument)+190
  System.Web.UI.WebControls.GridView.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(字符串   eventArgument)+7
  System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler   sourceControl,String eventArgument)   +11 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection   postData)+172
  System.Web.UI.Page.ProcessRequestMain(布尔   includeStagesBeforeAsyncPoint,Boolean   includeStagesAfterAsyncPoint)+4920

以下是页面代码:

<asp:SqlDataSource ID="dsSQL_ClaimsDetail" runat="server" ConnectionString="<%$ ConnectionStrings:activeDB_Connection %>"
        DeleteCommand="DELETE FROM [tblReturnClaimDetail] WHERE [ID] = @ID" ProviderName="<%$ ConnectionStrings:activeDB_Connection.ProviderName %>"
        SelectCommand="SELECT ID, HeaderID, CustomerSKUData, NikeInvoiceOrSalesOrderNumber, StyleandColorCode, StyleCode, ColorCode, StyleName, WholesaleDolrs, OriginalWholesaleDolrsPerUnit, Units, AccountDiscDolrsPerUnit, TotalNetPriceDolrs, HandlingandProcessing, Negotiated, NegotiatedReason, NegotiatedOptionType, NegotiatedOptionValue, FinalUnitPrice, FinalDolrs, FinalStandardDolrs, UpdateTime, UpdateUser, BatchUser, Approved, Denied, Pending, ClaimDisposition, FinalDecisionDate, ClaimRecordNumber, ASMName, Category, Gender, Other1, Other2, Other3, GBURegion, Division, BudgetManagerName, ClaimsBudgetYear, ClaimsBudgetQuarter, ApprovedDate, ReconciledBy, ReconciledDate, ReconcileOrigionalLineID FROM tblReturnClaimDetail WHERE (HeaderID = @claimID)">
        <DeleteParameters>
            <asp:Parameter Name="ID" />
        </DeleteParameters>
    <SelectParameters>
            <asp:ControlParameter ControlID="lbl_ClaimNumber" Name="claimID" PropertyName="Text" />
        </SelectParameters>
    </asp:SqlDataSource>
    <asp:CheckBox ID="chkbox_detailPagerToggle" runat="server" Text="View all styles at once" Visible="True" AutoPostBack="True" Checked="True" />&nbsp;</div>
    <asp:GridView ID="grid_Details" runat="server" AutoGenerateColumns="False"
        DataKeyNames="ID" DataSourceID="dsSQL_ClaimsDetail">
        <Columns>
            <asp:CommandField SelectText="Edit" ShowSelectButton="True">
                <HeaderStyle BackColor="Transparent" />
            </asp:CommandField>
            <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
                SortExpression="ID" />
            <asp:BoundField DataField="NikeInvoiceOrSalesOrderNumber" HeaderText="Nike Invoice /&lt;br /&gt;Sales Order #"
                HtmlEncode="False" SortExpression="NikeInvoiceOrSalesOrderNumber" />
            <asp:BoundField DataField="StyleCode" HeaderText="Style Code" SortExpression="StyleCode" />
            <asp:BoundField DataField="ColorCode" HeaderText="Color Code" SortExpression="ColorCode" />
            <asp:BoundField DataField="StyleName" HeaderText="Style Name" SortExpression="StyleName" />
            <asp:BoundField DataField="GBURegion" HeaderText="Channel" SortExpression="GBURegion" />
            <asp:BoundField DataField="Category" HeaderText="Category" SortExpression="Category" />
            <asp:BoundField DataField="Gender" HeaderText="Gender" SortExpression="Gender" />
            <asp:BoundField DataField="OriginalWholesaleDolrsPerUnit" DataFormatString="{0:c2}"
                HeaderText="WholeSale&lt;br /&gt;$/Unit" HtmlEncode="False" SortExpression="OriginalWholesaleDolrsPerUnit" />
            <asp:BoundField DataField="Units" HeaderText="Units" SortExpression="Units" />
            <asp:BoundField DataField="AccountDiscDolrsPerUnit" DataFormatString="{0:c2}" HeaderText="Discount&lt;br /&gt;$/Unit"
                HtmlEncode="False" SortExpression="AccountDiscDolrsPerUnit" />
            <asp:BoundField DataField="HandlingandProcessing" DataFormatString="{0:c2}" HeaderText="Handling &amp;&lt;br /&gt;Processing"
                HtmlEncode="False" SortExpression="HandlingandProcessing" />
            <asp:CheckBoxField DataField="Negotiated" HeaderText="Negotiated" SortExpression="Negotiated" />
            <asp:BoundField DataField="NegotiatedReason" HeaderText="Negotiated Reason" SortExpression="NegotiatedReason" />
            <asp:BoundField DataField="NegotiatedOptionType" HeaderText="Negotiated Type" SortExpression="NegotiatedOptionType" />
            <asp:BoundField DataField="NegotiatedOptionValue" HeaderText="Negotiated Value" SortExpression="NegotiatedOptionValue" />
            <asp:BoundField DataField="FinalDolrs" DataFormatString="{0:c2}" HeaderText="Final $ for Product"
                HtmlEncode="False" SortExpression="FinalDolrs">
                <ItemStyle Font-Bold="True" />
            </asp:BoundField>
            <asp:CommandField ShowDeleteButton="True">
                <HeaderStyle BackColor="Transparent" />
            </asp:CommandField>
        </Columns>
    </asp:GridView>

有问题的3个对象背后的唯一代码是复选框:

Protected Sub chkbox_detailPagerToggle_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkbox_detailPagerToggle.CheckedChanged
    If Me.chkbox_detailPagerToggle.Checked = True Then
        Me.grid_Details.AllowPaging = False
    Else
        Me.grid_Details.AllowPaging = True
    End If
End Sub

让我朝着正确的方向前进的任何想法都会很精彩,因为我不知道下一步要去解决这个问题。

1 个答案:

答案 0 :(得分:3)

看起来GridView对要删除的行数感到困惑,并尝试将所选索引设置为无效值。

是否仅在删除最后一行时才会出现?

执行删除时是否有触发器触发?这可能导致@@ RowCount搞砸了。尝试在删除呼叫之前添加“SET NOCOUNT OFF”和“SET NOCOUNT ON”。

我在猜测,但也许在分页时,SetSelectedIndex代码的工作方式略有不同。