Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# gridview中的System.ArgumentOutOfRangeException_C#_Asp.net_Gridview - Fatal编程技术网

C# gridview中的System.ArgumentOutOfRangeException

C# gridview中的System.ArgumentOutOfRangeException,c#,asp.net,gridview,C#,Asp.net,Gridview,我试图在单击按钮时选择一行,但它出错 这个网站以前工作得很好 protected void Btn1_Click(object sender, EventArgs e) { GridViewRow gvRow = (GridViewRow)(sender as Control).Parent.Parent; int index = gvRow.RowIndex; string rute = gridview_busqueda

我试图在单击按钮时选择一行,但它出错

这个网站以前工作得很好

 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>
mscorlib.dll中发生“System.ArgumentOutOfRangeException”类型的异常,但未在用户代码中处理


我认为问题在于[指数]。要选择特定行的值,可以执行以下操作

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;
}

希望它对您有用。

为什么不只是gvRow.Cells[0]…?gridview是空的。。奇怪的情况,由于在网页上显示了20行。
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;
}