protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//string connstr = ConfigurationManager.AppSettings["LocalSqlserver"];
//SqlConnection conn = new SqlConnection(connstr);
//conn.Open();
//SqlCommand cmd = new SqlCommand();
//cmd.Connection = conn;
//cmd.CommandText = "DELETE FROM newsinfo WHERE newsid ="+GridView1.SelectedRow.Cells[0].Text.ToString();
//cmd.CommandType = CommandType.Text;
//cmd.ExecuteNonQuery();
Label1.Text = GridView1.DataKeys[e.RowIndex].Values["newsid"].ToString();
}<%@ Page Language="C#" AutoEventWireup="true" CodeFile="manager_news.aspx.cs" Inherits="admin_manager_news" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>新闻管理</title>
</head>
<body>
<form id="form1" runat="server" method="post">
<div style="text-align: center">
<table cellpadding="0" cellspacing="0" border="0" style="font-size: 11px; width: 69%;">
<tr>
<td align="center" valign="middle" style="width: 662px" >
<asp:GridView ID="GridView1" runat="server" Width="94%" CellPadding="4" ForeColor="#333333"
AutoGenerateColumns="False" AllowPaging="True" PageSize="15" BorderColor="Silver" BorderStyle="Solid" BorderWidth="1px" style="text-align: center" Height="369px" OnRowDeleting="GridView1_RowDeleting" OnSelectedIndexChanged="GridView1_SelectedIndexChanged2" >
<PagerSettings Visible="False" />
<FooterStyle Font-Bold="True" />
<RowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle Font-Bold="False" Font-Italic="False" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField HeaderText="newsid" FooterText="newsid" DataField="newsid" SortExpression="title"/>
<asp:BoundField HeaderText="标题" FooterText="标题" DataField="title" SortExpression="title"/>
<asp:CommandField ShowDeleteButton="true" />
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ImageUrl="~/pic/del.gif" runat="server" ID="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
<tr>
<td align="center" style="height: 25px; width: 662px;">
<asp:Label ID="lblCurrentPage" runat="server" Text="当前页为:" Width="96px"></asp:Label> <asp:HyperLink ID="lnkPrev" runat="server">上一页</asp:HyperLink> <asp:HyperLink ID="lnkNext" runat="server">下一页</asp:HyperLink>
</td>
</tr>
</table>
<asp:Label ID="Label1" runat="server" Text="Label" Width="122px"></asp:Label></div>
</form>
</body>
</html>
错误为:Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
{
//string connstr = ConfigurationManager.AppSettings["LocalSqlserver"];
//SqlConnection conn = new SqlConnection(connstr);
//conn.Open();
//SqlCommand cmd = new SqlCommand();
//cmd.Connection = conn;
//cmd.CommandText = "DELETE FROM newsinfo WHERE newsid ="+GridView1.SelectedRow.Cells[0].Text.ToString();
//cmd.CommandType = CommandType.Text;
//cmd.ExecuteNonQuery();
Label1.Text = GridView1.DataKeys[e.RowIndex].Values["newsid"].ToString();
}<%@ Page Language="C#" AutoEventWireup="true" CodeFile="manager_news.aspx.cs" Inherits="admin_manager_news" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>新闻管理</title>
</head>
<body>
<form id="form1" runat="server" method="post">
<div style="text-align: center">
<table cellpadding="0" cellspacing="0" border="0" style="font-size: 11px; width: 69%;">
<tr>
<td align="center" valign="middle" style="width: 662px" >
<asp:GridView ID="GridView1" runat="server" Width="94%" CellPadding="4" ForeColor="#333333"
AutoGenerateColumns="False" AllowPaging="True" PageSize="15" BorderColor="Silver" BorderStyle="Solid" BorderWidth="1px" style="text-align: center" Height="369px" OnRowDeleting="GridView1_RowDeleting" OnSelectedIndexChanged="GridView1_SelectedIndexChanged2" >
<PagerSettings Visible="False" />
<FooterStyle Font-Bold="True" />
<RowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle Font-Bold="False" Font-Italic="False" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField HeaderText="newsid" FooterText="newsid" DataField="newsid" SortExpression="title"/>
<asp:BoundField HeaderText="标题" FooterText="标题" DataField="title" SortExpression="title"/>
<asp:CommandField ShowDeleteButton="true" />
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ImageUrl="~/pic/del.gif" runat="server" ID="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
<tr>
<td align="center" style="height: 25px; width: 662px;">
<asp:Label ID="lblCurrentPage" runat="server" Text="当前页为:" Width="96px"></asp:Label> <asp:HyperLink ID="lnkPrev" runat="server">上一页</asp:HyperLink> <asp:HyperLink ID="lnkNext" runat="server">下一页</asp:HyperLink>
</td>
</tr>
</table>
<asp:Label ID="Label1" runat="server" Text="Label" Width="122px"></asp:Label></div>
</form>
</body>
</html>
错误为:Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
错误是说,超出索引范围, 看看,这里的 GridView1.SelectedRow.Cells[0].Text.ToString();
GridView1.SelectedRow.Cells[0].Text.ToString();
请知道
参数名: index 解决方案
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。
参数名: index
try:首先设置一个GridView 一个主键 对应于你数据库表的主键 GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "id" };//主键
GridView1.DataBind();
sqlcon.Close(); 然后写SQL语句:select 字段A,字段B where id=‘"+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "‘";
而我的错误原因就是因为我没有设置主键,也就是DataKeyNames