请教:有一个分页的datagrid,如何删除datagrid中的一行,但并不删除数据库中的记录。如何实现呢?

解决方案 »

  1.   

    从datasource里移除数据
    然后重绑定
      

  2.   

    把你绑定gridview的代码发来看看
      

  3.   

    public void BindGrid()
    {
    //将将M_EBOOK_SERIAL表中的数据绑定在在dgList上DbHand dbHand = new DbHand(); DbHand dbHand = new DbHand(); DataSet ds = new DataSet();
    DataSet dsCurEbook  = new DataSet(); string strSql = ""; string strCurBook = ""; strSql = " select ebook_no, bkc, ebook_serial,start_date, bkc_descr, hs_code ";
    strSql = strSql + " from m_ebook_serial ";
    strSql = strSql + " order by ebook_no, ebook_serial asc"; try
    {
    dbHand.OpenConnection(); ds = dbHand.ReadData( strSql, "ebook_list"); dgList.DataSource = ds.Tables["ebook_list"].DefaultView;
    this.dgList.DataBind();

    lblCurrentIndex.Text = "第 " + (dgList.CurrentPageIndex + 1).ToString() + " 页";
    lblPageCount.Text = "总共 " + dgList.PageCount.ToString() + " 页";
    }
    catch( System.Exception  ex )
    {
    lblMsg.Text = ex.Message.ToString().Trim();
    }
    finally
    {
    dbHand.CloseConnection();
    } }
      

  4.   


    public void Remove(int iRowIndex)
            {
                //将将M_EBOOK_SERIAL表中的数据绑定在在dgList上DbHand dbHand = new DbHand();            DbHand dbHand = new DbHand();            DataSet ds = new DataSet();
                DataSet dsCurEbook  = new DataSet();            string strSql = "";            string strCurBook = "";            strSql = " select ebook_no, bkc, ebook_serial,start_date, bkc_descr, hs_code ";
                strSql = strSql + " from m_ebook_serial ";
                strSql = strSql + " order by ebook_no, ebook_serial asc";            try
                {
                    dbHand.OpenConnection();                ds = dbHand.ReadData( strSql, "ebook_list");
                    ds.Tables["ebook_list"].rows.remove(ds.Tables["ebook_list"].rows[iRowIndex]); //这样来移除数据
                   
                    dgList.DataSource = ds.Tables["ebook_list"].DefaultView;
                    this.dgList.DataBind();
                    
                    lblCurrentIndex.Text = "第 " + (dgList.CurrentPageIndex + 1).ToString() + " 页";
                    lblPageCount.Text = "总共 " + dgList.PageCount.ToString() + " 页";
                }
                catch( System.Exception  ex )
                {
                    lblMsg.Text = ex.Message.ToString().Trim();
                }
                finally
                {
                    dbHand.CloseConnection();
                }            }
      

  5.   

    对于分页的datagrid来说,这个iRowIndex如何取得呢?
    我用e.Item.ItemIndex是当前页上的行号。这样移除数据对否?
      

  6.   

    ds.Tables["ebook_list"].rows.remove(ds.Tables["ebook_list"].rows[iRowIndex]); 
    这一句的操作对象是table,这个不存在分页情况,和gridview没关系....你就是直接移除了table的这一行....
    建议根据主键找到那条数据,在table里移除掉,再绑定
      

  7.   

    这个意思我明白了,但是我想获取的是datagrid当前绑定的数据表,而不是每次都从数据库去取得表。
    如何能获取到datagrid当前所绑定的数据表的内容呢?
      

  8.   

    看看这里有没有你需要哒,个人收集的DATAGRID资料大全,满意的话,给个分吧
    DataGrid的EditCommandColumn怎么使用图片作为按钮
    Asp.net的DataGrid怎么纵向显示数据
    Asp.net的DataGrid中怎么用联合主键的DataKeys定位记录
    Asp.net的DataGrid为什么取不到值
    Asp.net的DataGrid在PostBack后怎么定位记录
    Asp.net怎么触发DataGrid模板列中控件的事件
    怎么在绑定后动态改变DataGrid中的内容
    Asp.net中DataGrid利用DataRelation显示主从表信息
    ASP.NET中DataGrid和DataList控件用法比较
    Asp.net创建跨多列以及多行表头的DataGrid
    Asp.net为DataGrid整行添加服务器事件代码
    更多Asp.net DataGrid