当前位置: 代码迷 >> ASP.NET >> Gridview删除的时候提示Index was out of range,该怎么解决
  详细解决方案

Gridview删除的时候提示Index was out of range,该怎么解决

热度:10771   发布时间:2013-02-25 00:00:00.0
Gridview删除的时候提示Index was out of range
HTML code
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" CellPadding="3" Font-Size="9pt" BackColor="White" BorderColor="#3399FF" BorderStyle="Solid" BorderWidth="1px"  OnPageIndexChanging="fangye" PageSize="10" AllowPaging="True" OnRowDeleting="RowDeleting"  OnRowDataBound="tishi">    <FooterStyle BackColor="White" ForeColor="#000066"/>    <Columns>        <asp:BoundField DataField="id" HeaderText="编号" />        <asp:BoundField DataField="type" HeaderText="分类"  HeaderStyle-Width="80px" />        <asp:BoundField DataField="title" HeaderText="标题"  HeaderStyle-Width="400px" ItemStyle-HorizontalAlign="Left" />        <asp:BoundField DataField="author" HeaderText="作者"  HeaderStyle-Width="80px" />        <asp:BoundField DataField="date" HeaderText="发表日期"  HeaderStyle-Width="120px" />        <asp:BoundField DataField="num" HeaderText="阅读数"  HeaderStyle-Width="80px" />        <asp:CommandField HeaderText="删除" ShowDeleteButton="True"/>        <asp:BoundField DataField="id" HeaderText="修改" HeaderStyle-Width="30px" />    </Columns>    <RowStyle ForeColor="#000066" />    <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />    <PagerStyle BackColor="white" ForeColor="#000066" HorizontalAlign="Left" CssClass="PagerCss" />    <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White"/></asp:GridView>


C# code
protected void Page_Load(object sender, EventArgs e)    {        if (!IsPostBack)        {            showdata(0);        }    }    //显示数据    protected void showdata(int pageIndex)    {        SqlConnection sqlcon;        string strCon = ConfigurationSettings.AppSettings["ConnectionString"];        string sqlstr = "select * from tjnews where type='发表文章' or type='实验室活动'";        sqlcon = new SqlConnection(strCon);        SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);        DataSet myds = new DataSet();        sqlcon.Open();        myda.Fill(myds, "tjnews");        DataView view = myds.Tables["tjnews"].DefaultView;        string sort = "date desc,id desc";        view.Sort = sort;        GridView1.DataKeyNames = new string[] { "id" };//不加这个不能执行"删除"功能        GridView1.DataSource = view;        GridView1.PageIndex = pageIndex;        GridView1.DataBind();    }    //翻页事件    protected void fangye(object sender, GridViewPageEventArgs e)    {        showdata(e.NewPageIndex);    }    //提示    protected void tishi(object sender, GridViewRowEventArgs e)    {        //如果是绑定数据行 ,判断是否是数据行        if (e.Row.RowType == DataControlRowType.DataRow)        {            //当鼠标停留时更改背景色            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");            //当鼠标移开时还原背景色            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)            {                ((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除吗?')");            }        }        //显示编号        if (e.Row.RowIndex != -1)        {            int id = e.Row.RowIndex + 1;            e.Row.Cells[0].Text = id.ToString();        }    }    //删除    protected void RowDeleting(object sender, GridViewDeleteEventArgs e)    {        GridViewRow row = GridView1.Rows[e.RowIndex];        DataSet ds = new DataSet();        string strcn = ConfigurationSettings.AppSettings["ConnectionString"];        SqlConnection cn = new SqlConnection(strcn);        cn.Open();        string sql = "delete from tjnews where id ='" + int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString()) + "'";        SqlCommand cm = new SqlCommand(sql, cn);        cm.ExecuteNonQuery();//执行        cn.Close();        showdata(0);    }
  相关解决方案