private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            string conn = "data source=192.168.100.124;database=DBFILE;trusted_connection = yes";
            string str1 = "delete from authdescribe where AuthorityID =";
            string str2 = ((TextBox)e.Item.FindControl("AuthorityID")).Text;
            string str3 = str1 + "'" + str2 + "'" ;
            SqlCommand sqlcmd = new SqlCommand();
            sqlcmd.Connection = new SqlConnection(conn);
            sqlcmd.CommandText = str3;
            sqlcmd.Connection.Open();
            sqlcmd.CommandType = CommandType.Text;
            sqlcmd.ExecuteNonQuery();
            sqlcmd.Connection.Close();
            setbind();        }
}
    protected void btn_Insert_Click(object sender, EventArgs e)
    {
        if ((this.TextBox1.Text != "") && (this.TextBox2.Text != ""))
        {
            string StrConnection = "data source=192.168.100.124;database=DBFILE;trusted_connection = yes";
            string str1 = "insert into authdescribe(AuthorityID,PowderDescribe) values (";
            string str2 = this.TextBox1.Text;
            string str3 = this.TextBox2.Text;
            string str4 = str1 + "'" + str2 + "'" + "," +"'"+ str3 + "'" + ")";
            SqlCommand sqlcmd = new SqlCommand();
            sqlcmd.Connection = new SqlConnection(StrConnection);
            sqlcmd.CommandText = str4;
            sqlcmd.Connection.Open();
            sqlcmd.CommandType = CommandType.Text;
            sqlcmd.ExecuteNonQuery();
            sqlcmd.Connection.Close();
            this.TextBox1.Text = "";
            this.TextBox2.Text = "";
            setbind();
        } private void setbind()
    {
        string conn = "data source=192.168.100.124;database=DBFILE;trusted_connection = yes";
        SqlDataAdapter da = new SqlDataAdapter("Select AuthorityID,PowderDescribe from authdescribe ", conn);
        DataSet ds = new DataSet();
        da.Fill(ds, "table1");
        this.DataGrid1.DataSource = ds.Tables["table1"];
        this.DataGrid1.DataBind();
        
    }
现在的问题是,我的页面上有2页数据,但是在删除第二页数局的时候,老是报错,删除是成功了,但在榜定的时候出错了,说CurrentPageIndex 有问题,不知道怎么做?和PageCount有关系吗??如何解决?

解决方案 »

  1.   

    当第二页只有一条数据时会报错吧,这种情况要特殊处理。
    baidu搜索一把就能找到解决方案。
      

  2.   

    在DataGrid的DeleteCommand事件处理程序中,可以通过下面的方法,来实现当最后一页没有数据时,自动跳回前一页,否则只需要重新绑定数据即可:void DataGrid1_Delete(object src,DataGridCommandEventArgs e){
    //执行删除命令//如果当前页上只有一条数据并且DataGrid的当前页索引大于0,那么就让DataGrid返回上一页
    if(DataGrid1.Items.Count == 1 && DataGrid1.CurrentPageIndex > 0){
    DataGrid1.CurrentPageIndex--;
    }
    //重新绑定数据
    BindData();
    }
      

  3.   

    DataGrid重綁定時要判斷CurrentPageIndex是否小于PageCount﹐如果不是的話就會出錯。
    在setbind()中判斷。可試試﹕
    int p=DataGrid1.CurrentPageIndex;
    DataGrid1.CurrentPageIndex=0;
    this.DataGrid1.DataSource = ds.Tables["table1"];
    this.DataGrid1.DataBind();
    if(DataGrid1.PageCount>p) DataGrid1.CurrentPageIndex=p;
      

  4.   

    在你的DataGrid1_ItemCommand方法里,调用setbind();前,加上:
    if (DataGrid1.Items.Count==1&&DataGrid1.CurrentPageIndex>0)

     DataGrid1.CurrentPageIndex -=1;
      

  5.   

    在DataGrid的DeleteCommand事件处理程序中,可以通过下面的方法,来实现当最后一页没有数据时,自动跳回前一页,否则只需要重新绑定数据即可:void DataGrid1_Delete(object src,DataGridCommandEventArgs e){
    //执行删除命令//如果当前页上只有一条数据并且DataGrid的当前页索引大于0,那么就让DataGrid返回上一页
    if(DataGrid1.Items.Count == 1 && DataGrid1.CurrentPageIndex > 0){
    DataGrid1.CurrentPageIndex--;
    }
    //重新绑定数据
    BindData();
    }
      

  6.   

    OK LE
    XIE XIE 
    SAN FEN!