奇怪,我明明是在第二页点击的删除按钮,但是获取到的是第一页的相同位置的行值???
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string mName = GridView1.DataKeys[e.RowIndex].Values["mName"].ToString();
        Response.Write(mName);
    }

解决方案 »

  1.   

    你是不是在  page_load方法里面绑定的数据源  或者你没有在!page.ispostback判断这个里面绑定   你肯定是每次页面加载都绑定一次数据源 
      

  2.   

    因为有分页,所以肯定要在 page_load 里绑定
    绑定了两个        if (!IsPostBack) bindGrid();
            else
            {
                //...
                bindGrid(mPermission, IsLock, pageSize);
            }
      

  3.   

    else里面不用绑定了吧  你这样每次都绑定了 也就是数据不管你点了分页没有每次都要重新绑定数据源 这样一来每次都是同样的数据
      

  4.   

    但是页面里有一些数据筛选条件,如果不再 page_load 里绑定
    那些选择就会丢失怎么办?
      

  5.   

    如果 page_load 中绑定了 gridview 数据源(不是 ispostback)
    就不能在 gridview 翻页后删除行,那 gridview 功能也就大打折扣了。。
    有没有遇到这种情况,给个思路也行
      

  6.   

    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.IO;
    public partial class TKT_AllTickt : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bind();
            }
        }
        public void bind()
        {
            string cmdtxt1 = "server=.;database=testweb;Uid=sa;Pwd=sa";
            SqlConnection Con = new SqlConnection(cmdtxt1);
            Con.Open();
            string sql = "select   *   from   [MATKT]   where  1=1   ";// 实现搜索功能
            string sqlend = "order   by   id   desc";
            string sqlwhere = string.Empty;
            if (this.shaagent.Text.Trim() != "")
            {
                sqlwhere += "and   VARAGENT   like   '%" + this.shaagent.Text.Trim() + "%'   ";
            } 
            if (this.branch.Text.Trim() != "")
            {
                sqlwhere += "and   VARBRANCH   like  '%" + this.branch.Text.Trim() + "%'  ";
            }
            if (this.rl.Text.Trim() != "")
            {
                sqlwhere += "and   VARRL    like  '%" + this.rl.Text.Trim() + "%'  ";
            }
            if (this.airline.Text.Trim() != "")
            {
                sqlwhere += "and  VARAIRLINE  like  '%" + this.airline.Text.Trim() + "%'  ";
            }    
            sql += sqlwhere + sqlend;
            SqlDataAdapter Da = new SqlDataAdapter(sql, Con);
            DataSet ds = new DataSet();
            Da.Fill(ds);
            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();
            Con.Close();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            bind();
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            SqlConnection con = new SqlConnection("server=.;database=testweb;uid=sa;pwd=sa");
            con.Open();
            SqlCommand com = new SqlCommand("delete from [MATKT] where id='" + GridView1.DataKeys[e.RowIndex].Value + "'", con);
            com.ExecuteNonQuery();
            SqlDataAdapter ada = new SqlDataAdapter("select * from [MATKT] order by id desc", con);
            DataSet ds = new DataSet();
            ada.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
            con.Close();
            bind();
        }    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();
            bind();
            //分页功能
        }    
    }不知道你的删除是怎么做的,没有看懂。我这个做出来的功能有你说的那个功能,而且是完全正确的,我的代码估计也不规范,你看看吧,希望有用...
      

  7.   

    自己搞定了,page_load 里判断一下
    如果 !ispostback 直接 bind()
    如果 ispostback 取出gridview.pageindex保存在session中,把 bind 过程从再一次,bind(pageindex)
    然后删除是不论是在那一页,都能取到当前页的数据