protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除课程:\"" + e.Row.Cells[1].Text + "\"吗?')");
            }
        } 
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string sc = GridView1.DataKeys[e.RowIndex].Value.ToString();
        string ml = "delete from cla_info where kc_id='" + sc + "'";
        SqlConnection sqlcon = new SqlConnection(strCon);
        xw = new SqlCommand(ml, sqlcon);
        sqlcon.Open();
        xw.ExecuteNonQuery();
        sqlcon.Close();
        bind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string ud = "update cla_info set kc_name='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',kc_teacher='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',xq='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim() + "' where kc_id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
        sqlcon = new SqlConnection(strCon);
        sqlcon = new SqlConnection(strCon);
        sqlcon.Open();
        xw = new SqlCommand(ud, sqlcon);
        xw.ExecuteNonQuery();
        xw.Clone();
        GridView1.EditIndex = -1;
        bind();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.PagerSettings.Mode = PagerButtons.Numeric;
        bind();
    }

解决方案 »

  1.   

     protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            bind();
        }
      

  2.   

    各位高手,这个GridView分页删除,在本地不管在第几页都可以删除,但是放到服和器上,点击分页后,删除就会提示
    Server Error in '/' Application.
    --------------------------------------------------------------------------------Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index 
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: indexSource Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  
      

  3.   

    我的意见是重新换一种方法吧,这个问题我也遇见过,我的处理办法,
    javascriptfunction GetAllCheck(CheckAll) {
        var items = document.getElementsByTagName("input");
        for (i = 0; i < items.length; i++) {
            if (items[i].type == "checkbox") {
                items[i].checked = CheckAll.checked;
            }
        }
    }
    //删除选择
    function getIDStr() {    var thisContent = "";
        var objGroup = eval("form1.chk_del");    if (objGroup == undefined) {
            alert("没有记录");
        }
         else 
        {
            if (objGroup.length == undefined)
             {
                 if (objGroup.checked)
                 {
                    thisContent = objGroup.value;
                }
            }
            else 
            {
                for (var i = 0; i < objGroup.length; i++)
                 {
                    var obj1 = eval("form1.chk_del[" + i + "]");
                    if (obj1.checked)
                     {
                        if (thisContent == "" || thisContent == null) 
                        {
                            thisContent = obj1.value;
                        } else {
                            thisContent += "," + obj1.value;
                        }
                    }
                }
            }
        }
        form1.thisIDStr.value = thisContent;
        document.all("btnDelete").click();
    }//拆卸选择
    function getIDStr_cx() {    var thisContent = "";
        var objGroup = eval("form1.chk_del");    if (objGroup == undefined) {
            alert("没有记录");
        } else {
            if (objGroup.length == undefined) {
                if (objGroup.checked) {
                    thisContent = objGroup.value;
                }
            } else {
                for (var i = 0; i < objGroup.length; i++) {
                    var obj1 = eval("form1.chk_del[" + i + "]");
                    if (obj1.checked) {
                        if (thisContent == "" || thisContent == null) {
                            thisContent = obj1.value;
                        } else {
                            thisContent += "," + obj1.value;
                        }
                    }
                }
            }
        }
        form1.thisIDStr.value = thisContent;
        document.all("btnCxsb").click();
    }.aspx页面中加入如下代码,  <tr>
                    <td class="td1">
                        监测点名称:
                    </td>
                    <td class="td2">
                        <asp:TextBox ID="txtKeyword" runat="server"></asp:TextBox>
                        &nbsp;&nbsp;&nbsp;&nbsp;
                        <asp:Button ID="btnSearch" runat="server" Text="查询" OnClick="btnSearch_Click"></asp:Button>
                        &nbsp;&nbsp;&nbsp;&nbsp;
                        <asp:Button ID="btnAdd" runat="server" Text="新增" OnClick="btnAdd_Click"></asp:Button>
                        &nbsp;&nbsp;&nbsp;&nbsp;
                             <input type ="button" name="Delall" id="Delall" runat="server"  value="删除" onclick ="getIDStr()" />
                                    &nbsp;&nbsp;&nbsp;&nbsp;                  <span style ="display:none"> 
                        <asp:Button ID="btnDelete" runat="server" Text="删除" OnClick="btnDelete_Click" />
                            <input type="hidden" id="thisIDStr" name ="thisIDStr" runat ="server" />
                        </span>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" class="td_content">
                        <asp:GridView ID="gridView"  SkinID="myGridView"   runat="server" AllowPaging="True" Width="100%" CellPadding="3"
                            OnPageIndexChanging="gridView_PageIndexChanging" BorderWidth="1px" DataKeyNames="ID"
                            AutoGenerateColumns="false" PageSize="15" RowStyle-HorizontalAlign="Center" >
                            <Columns>
                                    <asp:TemplateField ControlStyle-Width="30" HeaderText="选择">
                                    <HeaderTemplate>
                                        <asp:CheckBox ID="chkAll" onclick='GetAllCheck(this)' runat="server" />
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                         <input type ="checkbox" name="chk_del" value="<%#DataBinder.Eval(Container.DataItem,"ID") %>"/>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                  <asp:BoundField DataField="ID" HeaderText="监测点ID" ItemStyle-HorizontalAlign="Center" />
                                <asp:BoundField DataField="Gqbh" HeaderText="监测点编号" SortExpression="Gqbh" ItemStyle-HorizontalAlign="Center" />
                                <asp:BoundField DataField="Gqmc" HeaderText="监测点名称" SortExpression="Gqmc" ItemStyle-HorizontalAlign="Center" />
                                <asp:BoundField DataField="JWDmc" HeaderText="所属段" SortExpression="Ssjwd" ItemStyle-HorizontalAlign="Center" />
                                <asp:BoundField DataField="Demo" HeaderText="备注" SortExpression="Demo" ItemStyle-HorizontalAlign="Center" />
                            
                                <asp:HyperLinkField HeaderText="编辑" ControlStyle-Width="50" DataNavigateUrlFields="ID"
                                    DataNavigateUrlFormatString="ModifyGq.aspx?id={0}" Text="编辑" />
                             
                            </Columns>
                        </asp:GridView>
                    </td>
                </tr>
            </table>.cs中的代码   protected void btnDelete_Click(object sender, EventArgs e)
            {
                string idlist = this.thisIDStr.Value.Trim();
                //Response.Write(""+idlist.ToString()+"");
                if (idlist.Trim().Length == 0)
                    return;
                bll.DeleteList(idlist);
                BindData();
            }思路,通过客户端选择代码,选取所有被选择的ID,然后存入隐藏的thisIDStr的值中,通过后台代码,删除