首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

Gridview剔除的时候提示Index was out of range

2013-06-26 
Gridview删除的时候提示Index was out of range本帖最后由 semanwmj 于 2012-02-20 23:33:14 编辑asp:Gri

Gridview删除的时候提示Index was out of range
本帖最后由 semanwmj 于 2012-02-20 23:33:14 编辑


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



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



点击删除的时候会提示Index was out of range,在服务器上会,本地不会,请问是什么原因?
[解决办法]
1. 把showdata(0);拿到onintial事件中。
2.  GridView1.DataBind(); 拿到onprerender事件中
3. 去掉删除事件中的showdata(0);
[解决办法]
onintial 初始化 
onprerender 呈现

热点排行