我在点击按钮时尝试选择一行,但是出现错误。
此网站以前工作正常。
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}
类型&#39; System.ArgumentOutOfRangeException&#39;的例外情况发生在mscorlib.dll中但未在用户代码中处理
答案 0 :(得分:0)
我认为问题在于[索引]。如果要选择特定行的值,可以执行以下操作。
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;
}
希望它适合你。