在我的情况下,Gridview上的索引超出了范围

时间:2015-06-17 08:35:28

标签: c# asp.net gridview

我总是收到这个错误:

  

mscorlib.dll中发生了'System.ArgumentOutOfRangeException'类型的异常但未在用户代码中处理

     

其他信息:指数超出范围。必须是非负面的   并且小于集合的大小。

在我的代码的这一部分:

GridViewRow gvrow = GridView1.Rows[index]

我不知道发生了什么......

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    int index = Convert.ToInt32(e.CommandArgument);
    if (e.CommandName.Equals("detail"))
    {
        string id = GridView1.DataKeys[index].Value.ToString();
        IEnumerable<DataRow> query = from i in dt.AsEnumerable()
                                     where i.Field<String>("id").Equals(id)
                                     select i;
        DataTable detailTable = query.CopyToDataTable<DataRow>();
        DetailsView1.DataSource = detailTable;
        DetailsView1.DataBind();
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append(@"<script type='text/javascript'>");
        sb.Append("$('#detailModal').modal('show');");
        sb.Append(@"</script>");
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "DetailModalScript", sb.ToString(), false);
    }
    else if (e.CommandName.Equals("editRecord"))
    {
        GridViewRow gvrow = GridView1.Rows[index];
        lblPTAID.Text = HttpUtility.HtmlDecode(gvrow.Cells[3].Text).ToString();
        txtPO.Text = HttpUtility.HtmlDecode(gvrow.Cells[3].Text).ToString();
        txtPODate.Text = HttpUtility.HtmlDecode(gvrow.Cells[3].Text).ToString();
        txtAmount.Text = HttpUtility.HtmlDecode(gvrow.Cells[3].Text).ToString();
        TextLearDebit.Text = HttpUtility.HtmlDecode(gvrow.Cells[3].Text).ToString();
        TextLearDebitAmount.Text = HttpUtility.HtmlDecode(gvrow.Cells[3].Text).ToString();
        TextVenInvoiceDate.Text = HttpUtility.HtmlDecode(gvrow.Cells[3].Text).ToString();
        TextVenInvo.Text = HttpUtility.HtmlDecode(gvrow.Cells[3].Text).ToString();
        TextVenInvoiceAmount.Text = HttpUtility.HtmlDecode(gvrow.Cells[3].Text).ToString();
        TextVenInvoiceProcessed.Text = HttpUtility.HtmlDecode(gvrow.Cells[3].Text).ToString();
        TextPTAvsVendor.Text = HttpUtility.HtmlDecode(gvrow.Cells[3].Text).ToString();
        TextVariance.Text = HttpUtility.HtmlDecode(gvrow.Cells[3].Text).ToString();
        TextRootCause.Text = HttpUtility.HtmlDecode(gvrow.Cells[3].Text).ToString();
        lblResult.Visible = false;
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append(@"<script type='text/javascript'>");
        sb.Append("$('#editModal').modal('show');");
        sb.Append(@"</script>");
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "EditModalScript", sb.ToString(), false);

    }
    else if (e.CommandName.Equals("deleteRecord"))
    {

        string code = GridView1.DataKeys[index].Value.ToString();
        id.Value = code;
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append(@"<script type='text/javascript'>");
        sb.Append("$('#deleteModal').modal('show');");
        sb.Append(@"</script>");
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "DeleteModalScript", sb.ToString(), false);
    }

}

// Handles Update Button Click Event

T 附加代码(ASPX)

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:GridView ID="GridView1" runat="server" Width="550px"
                AutoGenerateColumns="false" AlternatingRowStyle-BackColor="#C2D69B"
                HeaderStyle-BackColor="green" AllowPaging="true"
                PageSize="10" OnRowCommand="GridView1_RowCommand">
                <Columns>
                    <asp:ButtonField CommandName="detail"
                        ControlStyle-CssClass=""
                        ButtonType="Button" Text="Detail" HeaderText="Detailed View">
                        <ControlStyle CssClass="btn btn-info"></ControlStyle>
                    </asp:ButtonField>

                    <asp:ButtonField CommandName="editRecord"
                        ControlStyle-CssClass=""
                        ButtonType="Button" Text="Edit" HeaderText="Edit Record">
                        <ControlStyle CssClass="btn btn-info"></ControlStyle>
                    </asp:ButtonField>
                    <asp:ButtonField CommandName="deleteRecord"
                        ControlStyle-CssClass=""
                        ButtonType="Button" Text="Delete" HeaderText="Delete Record">
                        <ControlStyle CssClass="btn btn-info"></ControlStyle>
                    </asp:ButtonField>
                    <asp:BoundField DataField="PTAID" HeaderText="PTAID" SortExpression="PTAID" />
                    <asp:BoundField DataField="RequestID" HeaderText="RequestID" SortExpression="RequestID" />

                </Columns>
                <AlternatingRowStyle BackColor="#C2D69B" />
            </asp:GridView>

        </ContentTemplate>
        <Triggers>
        </Triggers>
    </asp:UpdatePanel>


    <!-- Detail Modal Starts here-->
    <div id="detailModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <h3 id="myModalLabel">Detailed View</h3>
        </div>
        <div class="modal-body">
            <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                <ContentTemplate>
                    <asp:DetailsView ID="DetailsView1" runat="server" CssClass="table table-bordered table-hover" BackColor="White" ForeColor="Black" FieldHeaderStyle-Wrap="false" FieldHeaderStyle-Font-Bold="true" FieldHeaderStyle-BackColor="LavenderBlush" FieldHeaderStyle-ForeColor="Black" BorderStyle="Groove" AutoGenerateRows="False">
                        <Fields>
                            <asp:BoundField DataField="PTAID" HeaderText="PTAID" />
                            <asp:BoundField DataField="RequestID" HeaderText="RequestID" />
                            <asp:BoundField DataField="RequestDate" HeaderText="RequestDate" />
                            <asp:BoundField DataField="ProvName" HeaderText="ProvName" />
                            <asp:BoundField DataField="Amount" HeaderText="Amount" />
                            <asp:BoundField DataField="INorOUT" HeaderText="INorOUT" />
                            <asp:BoundField DataField="Supplier" HeaderText="Supplier" />
                            <asp:BoundField DataField="Customer" HeaderText="Customer" />
                            <asp:BoundField DataField="Program" HeaderText="Program" />
                            <asp:BoundField DataField="IssueDesc" HeaderText="IssueDesc" />
                            <asp:BoundField DataField="Chargeable" HeaderText="Chargeable" />
                            <asp:BoundField DataField="Company" HeaderText="Company" />
                            <asp:BoundField DataField="PTAID" HeaderText="PTAID" />
                            <asp:BoundField DataField="A_PO" HeaderText="PO" />
                            <asp:BoundField DataField="A_PODate" HeaderText="PODate" />
                            <asp:BoundField DataField="A_Amount" HeaderText="Amount" />
                            <asp:BoundField DataField="A_LearDebit" HeaderText="LearDebit" />
                            <asp:BoundField DataField="A_LearDebitAmount" HeaderText="LearDebitAmount" />
                            <asp:BoundField DataField="B_VenInvoiceDate" HeaderText="VenInvoiceDate" />
                            <asp:BoundField DataField="B_VenInvo" HeaderText="VenInvo" />
                            <asp:BoundField DataField="B_VenInvoiceAmount" HeaderText="VenInvoiceAmount" />
                            <asp:BoundField DataField="B_VenInvoiceProcessed" HeaderText="VenInvoiceProcessed" />
                            <asp:BoundField DataField="C_PTAvsVendor" HeaderText="PTAvsVendor" />
                            <asp:BoundField DataField="C_Variance" HeaderText="Variance" />
                            <asp:BoundField DataField="C_RootCause" HeaderText="RootCause" />
                        </Fields>
                    </asp:DetailsView>
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="GridView1" EventName="RowCommand" />
                </Triggers>
            </asp:UpdatePanel>
            <div class="modal-footer">
                <button class="btn btn-info" data-dismiss="modal" aria-hidden="true">Close</button>
            </div>
        </div>
    </div>
    <!-- Detail Modal Ends here -->


    <!-- Edit Modal Starts here -->
    <div id="editModal" class="modal hide fade"
        tabindex="-1" role="dialog" aria-labelledby="editModalLabel"
        aria-hidden="true">
        <div class="modal-header">
            <button type="button" class="close"
                data-dismiss="modal" aria-hidden="true">
                ×</button>
            <h3 id="editModalLabel">Edit Record</h3>
        </div>
        <asp:UpdatePanel ID="upEdit" runat="server">
            <ContentTemplate>
                <div class="modal-body">
                    <table class="table">
                        <tr>
                            <td>PTA ID :
  <asp:Label ID="lblPTAID" runat="server"></asp:Label>
                            </td>
                        </tr>
                        <tr>
                            <td>PO : 
  <asp:TextBox ID="txtPO" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>PODate :
  <asp:TextBox ID="txtPODate" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>Amount :
  <asp:TextBox ID="txtAmount" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>LearDebit :
  <asp:TextBox ID="TextLearDebit" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>LearDebitAmount :
  <asp:TextBox ID="TextLearDebitAmount" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>VenInvoiceDate :
  <asp:TextBox ID="TextVenInvoiceDate" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>VenInvo :
  <asp:TextBox ID="TextVenInvo" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>VenInvoiceAmount :
  <asp:TextBox ID="TextVenInvoiceAmount" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>VenInvoiceProcessed :
  <asp:TextBox ID="TextVenInvoiceProcessed" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>PTAvsVendor :
  <asp:TextBox ID="TextPTAvsVendor" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>Variance :
  <asp:TextBox ID="TextVariance" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>RootCause :
  <asp:TextBox ID="TextRootCause" runat="server"></asp:TextBox>
                            </td>
                        </tr>

                    </table>
                </div>
                <div class="modal-footer">
                    <asp:Label ID="lblResult" Visible="false" runat="server"></asp:Label>
                    <asp:Button ID="btnSave" runat="server" Text="Update" CssClass="btn btn-info" OnClick="btnSave_Click" />
                    <button class="btn btn-info" data-dismiss="modal" aria-hidden="true">Close</button>
                </div>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="GridView1" EventName="RowCommand" />
                <asp:AsyncPostBackTrigger ControlID="btnSave" EventName="Click" />
            </Triggers>
        </asp:UpdatePanel>
    </div>
    <!-- Edit Modal Ends here -->

    <!-- Delete Record Modal Starts here-->
    <div id="deleteModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="delModalLabel" aria-hidden="true">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <h3 id="delModalLabel">Delete Record</h3>
        </div>
        <asp:UpdatePanel ID="upDel" runat="server">
            <ContentTemplate>
                <div class="modal-body">
                    Are you sure you want to delete the record?
              <asp:HiddenField ID="id" runat="server" />
                </div>
                <div class="modal-footer">
                    <asp:Button ID="btnDelete" runat="server" Text="Delete" CssClass="btn btn-info" OnClick="btnDelete_Click" />
                    <button class="btn btn-info" data-dismiss="modal" aria-hidden="true">Cancel</button>
                </div>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="btnDelete" EventName="Click" />
            </Triggers>
        </asp:UpdatePanel>

0 个答案:

没有答案