日期:2014-05-18  浏览次数:20474 次

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