日期:2014-05-20  浏览次数:21114 次

异常信息:指定的参数已超出有效值的范围。
异常信息提示:指定的参数已超出有效值的范围。

protected void GridView_RowCommand(object sender, GridViewCommandEventArgs e)
  {
  if (e.CommandName == "Reserve")
  {
  int rowindex = Convert.ToInt32(e.CommandArgument);
  string flightid = ((TextBox)(GridView.Rows[rowindex].Cells[1].Controls[0])).Text.ToString().Trim();


异常信息提示:指定的参数已超出有效值的范围。

这是为什么呢?已经查看过rowindex的值为“0”,而且还存在一条记录,请问这是为什么呢?

应该怎样获取Gridview中的值呢?


GridView前台代码:
<asp:GridView ID="GridView" runat="server" Visible="False" CellPadding="2"
ForeColor="#333333" AutoGenerateColumns="False" CellSpacing="2"
onrowcommand="GridView_RowCommand">
<FooterStyle BackColor="#990000" Font-Bold="true" ForeColor="White" />
<Columns>
<asp:BoundField DataField="FlightID" HeaderText="航班号" ReadOnly="true"/>
<asp:BoundField DataField="FlightTime" HeaderText="航班时间" />
<asp:BoundField DataField="FlightStart" HeaderText="起始地点"/>
<asp:BoundField DataField="FlightEnd" HeaderText="到达地点"/>
<asp:BoundField DataField="Launch" HeaderText="起飞时间"/>
<asp:BoundField DataField="Land" HeaderText="到达时间"/>
<asp:BoundField DataField="MidStation" HeaderText="中间站" />
<asp:BoundField DataField="Capacity" HeaderText="乘客量" />
<asp:BoundField DataField="Remain" HeaderText="空票数" />
<asp:BoundField DataField="Price" HeaderText="票价" />
<asp:ButtonField ButtonType="Button" CommandName="Reserve" Text="订票"/>
</Columns>
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="true" ForeColor="White"/>
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left"/>
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>
}
  }

------解决方案--------------------

<asp:ButtonField ButtonType="Button" CommandName="Reserve" Text="订票"/>
添加CommandArgument
HTML code
<asp:ButtonField ButtonType="Button" CommandName="Reserve"  CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" Text="订票"/>

------解决方案--------------------
GridViewRow row = (GridViewRow)((Control)e.CommandSource).Parent.Parent;//取得触发命令的当前行 
int index = row.RowIndex; 

CommandName="Reserve" CommandArgument='<%# Container.DataItemIndex %>'/>
------解决方案--------------------
楼主 你先学习一下 什么是gridview模板列

gridview 你只学习了10%不到 就会一招BoundField,以后没办法干活啊。 很多东西给你写出来 你还是不明白




------解决方案--------------------
探讨
int rowindex = Convert.ToInt32(e.CommandArgument);
这样不就已经获得当前行的索引了???

------解决方案--------------------
CommandArgument值设置了么?
------解决方案--------------------