将DataSet重新绑定新的数据源,但是翻页后没有显示新数据源的数据,仍然显示的是老数据源的数据,这应该如何处理?

解决方案 »

  1.   

    翻页后有没有重新DataBind()
    绑定了是否在翻页事件中设了翻页属性没有
      

  2.   

    private void DGResult_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
    DGResult.CurrentPageIndex = e.NewPageIndex;
    }
      

  3.   

    没有绑定
    private void dgrdCustomerInfo_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
    dgrdCustomerInfo.CurrentPageIndex = e.NewPageIndex; BindDataGrid();//DataSet重新绑定也要写在这个方法里
    }
      

  4.   

    没有用,我的也一样,我在用DataGrid的时候,里面加了一个模板列,结果也出现这种情况,在第一次的时候模板列里的TEXTBOX里的内容显示的是正确的,但到了第二页,里面的内容却还是第一页的内容,真有点晕了:(
    CS代码如下:
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;namespace WebNoteBook
    {
    /// <summary>
    /// admin_msg_Test 的摘要说明。
    /// </summary>
    public class admin_msg_Test : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Button btdel;
    protected System.Web.UI.WebControls.DataGrid DataGrid1;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    this.btdel.Attributes.Add("onclick", "return confirm('您真的要删除所选项吗?');");
    if(!this.IsPostBack)
    {
    this.BindNoteWordDataGrid();
    }
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
    this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
    this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
    this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
    this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
    this.btdel.Click += new System.EventHandler(this.btdel_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion
    private void BindNoteWordDataGrid()
    {
    SqlConnection con=DB.createCon();
    SqlDataAdapter sda=new SqlDataAdapter();
    sda.SelectCommand=new SqlCommand("select * from NoteBook",con);
    DataSet ds=new DataSet();
    sda.Fill(ds,"NB");
    this.DataGrid1.DataSource=ds.Tables["NB"];
    this.DataGrid1.DataBind();
    this.DataGrid1.Columns[3].Visible=false;
    foreach(DataGridItem dgi in this.DataGrid1.Items)
    {
    //以下绑定非编辑状态TextBox 
                    TextBox tbv=(TextBox)dgi.FindControl("tbview");
    if(tbv!=null)
    {
    tbv.Text=ds.Tables["NB"].Rows[dgi.ItemIndex]["NoteWord"].ToString();
    }
    //以下绑定编辑状态TextBox 
    TextBox tbe=(TextBox)dgi.FindControl("tbedit");
    if(tbe!=null)
    {
    tbe.Text=ds.Tables["NB"].Rows[dgi.ItemIndex]["NoteWord"].ToString();
    }
    }
    con.Close();
    } private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
    this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
    this.BindNoteWordDataGrid();
    } private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    this.DataGrid1.EditItemIndex=-1;
    this.BindNoteWordDataGrid();
    } private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    this.DataGrid1.EditItemIndex=e.Item.ItemIndex;
    this.BindNoteWordDataGrid();
    } //在编辑的时候对TextBox的宽度进行修改
    private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if (e.Item.ItemType == ListItemType.EditItem) 
    {
    TextBox t;
        t=(TextBox)e.Item.Cells[2].Controls[0];
    t.Width=120;
    // t=(TextBox)e.Item.Cells[4].Controls[0];
    // t.Width=450;
    t=(TextBox)e.Item.Cells[5].Controls[0];
    t.Width=70;
    // for (int i=2;i<5;i++)
    // {
    // if(e.Item.Cells[i].Controls.Count>0)
    // {
    // TextBox t =(TextBox)e.Item.Cells[i].Controls[0];
    // switch(i)
    // {
    // case 2:
    // t.Width=120;
    // break;
    // case 4:
    // t.Width=450;
    // break;
    // case 5:
    // t.Width=70;
    // break;
    // }
    // }
    // }
    }
    } private void btdel_Click(object sender, System.EventArgs e)
    {
    foreach(DataGridItem dgi in this.DataGrid1.Items)
    {
    CheckBox cb = (CheckBox)dgi.FindControl("cbselect");
    if(cb.Checked)
    {
    //以下执行删除操作
    string strid = Convert.ToString(dgi.Cells[0].Text);
    string strSql = "delete from NoteBook where ID='"+strid+"'";
    DB.ExecuteSql(strSql);
    }
    }
    this.DataGrid1.CurrentPageIndex = 0;
    this.BindNoteWordDataGrid();
    }
    public void CheckedAllmsg(object sender, System.EventArgs e)
    {
    CheckBox cball = (CheckBox)sender;
    if(cball.Text=="全选")
    {
    foreach(DataGridItem dgi in this.DataGrid1.Items)
    {
    CheckBox cb = (CheckBox)dgi.FindControl("cbselect");
    cb.Checked = cball.Checked;
    }
    }
    } private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    string strid=e.Item.Cells[0].Text;//处于非编辑状态
    string strtitle = ((TextBox)(e.Item.Cells[2].Controls[0])).Text;//处于编辑状态
    string strword = ((TextBox)e.Item.Cells[4].FindControl("tbedit")).Text;
    string strdatatime = ((TextBox)(e.Item.Cells[5].Controls[0])).Text;
    string strsql = "update NoteBook set NoteTitle='"+strtitle+"',NoteWord='"+strword+"',NoteTime='"+strdatatime+"' where ID='"+strid+"'";
    DB.ExecuteSql(strsql);
    this.DataGrid1.EditItemIndex = -1;
    this.BindNoteWordDataGrid();
    }
    }
    }