【问题标题】:System.ArgumentOutOfRangeException in a gridview网格视图中的 System.ArgumentOutOfRangeException
【发布时间】:2015-02-19 15:58:40
【问题描述】:

单击按钮时我试图选择一行,但出现错误。

这个网站曾经运行良好。

 protected void Btn1_Click(object sender, EventArgs e)
    {    
        GridViewRow gvRow = (GridViewRow)(sender as Control).Parent.Parent;
        int index = gvRow.RowIndex;

         string rute  = gridview_busqueda.Rows[index].Cells[0].Text; <-- 
          string rute1 = gridview_busqueda.Rows[index].Cells[1].Text;
          string rute2 = gridview_busqueda.Rows[index].Cells[2].Text;
          string rute3 = gridview_busqueda.Rows[index].Cells[3].Text;
          string rute4 = gridview_busqueda.Rows[index].Cells[4].Text;
      }

<asp:GridView ID="gridview_busqueda" Width="100%" 
      name="gridview_busqueda" EmptyDataText="No hay valores para Mostrar." 
      runat="server" CssClass="table table-hover table-bordered" AutoGenerateColumns="False" HeaderStyle-BackColor = "#e6e6e6"
      AllowPaging ="true" OnPageIndexChanging = "OnPaging">
    <Columns>
       <asp:BoundField DataField="Nombre" HeaderText="Nombre"   />
        <asp:BoundField DataField="Rut" HeaderText="Rut" />
        <asp:BoundField DataField="Gerencia" HeaderText="Gerencia"   />
        <asp:BoundField DataField="Cargo" HeaderText="Cargo"   />
        <asp:BoundField DataField="Vigencia_desde" HeaderText="Vigencia_desde"   />
        <asp:BoundField DataField="Vigencia_hasta" HeaderText="Vigencia_hasta"   />
        <asp:BoundField DataField="Fecha_creacion" HeaderText="Fecha Creacion"   />

    <%-- Adding button to gridview --%>
  <asp:TemplateField HeaderText="" >
      <ItemTemplate>
        <asp:Button ID="Btn1" runat="server" 
             Text="Ver Mas" CommandArgument="Button1"
             OnClick="Btn1_Click" />
      </ItemTemplate>
   </asp:TemplateField>         
   </Columns>
 </asp:GridView>

错误:

'gridview_busqueda.Rows[index]' threw an exception of type 'System.ArgumentOutOfRangeException'
System.Web.UI.WebControls.GridViewRow {System.ArgumentOutOfRangeException}

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

【问题讨论】:

  • 为什么不只是gvRow.Cells[0]...
  • gridview 是空的..奇怪的情况,由于在网页中显示了 20 行。

标签: c# asp.net gridview


【解决方案1】:

我认为问题出在 [index] 上。当您想选择特定行的值时,您可以执行以下操作。

protected void Btn1_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    GridViewRow gvRow = (GridViewRow)btn.NamingContainer;
    string rute = gvRow.Cells[0].Text;
    string rute1 = gvRow.Cells[1].Text;
    string rute2 = gvRow.Cells[2].Text;
    string rute3 = gvRow.Cells[3].Text;
    string rute4 = gvRow.Cells[4].Text;
}

希望它对你有用。

【讨论】:

    猜你喜欢
    • 2020-09-28
    • 2018-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-13
    • 2013-07-18
    • 2020-09-08
    相关资源
    最近更新 更多