本人对Datagrid筛选出来的数据做分页时.  本来10000条数据  只会筛选出1000条,但是当我选择第2页时候. 
只要针对这1000条数据...  
但是我现在一选择下页.又转回BindGrid(); 又会掉用select * from statrecord...
请大哥给条明路.怎么把我筛选的这条语句.来回贯穿在这个界面代码中...
-----------------------------------------------------------------------------------
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
strcn = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
cn = new SqlConnection(strcn);
if(!IsPostBack) BindGrid(); }
                    public void BindGrid()//绑定
{
SqlDataAdapter da = new SqlDataAdapter("select * from statrecord",cn);
DataSet ds = new DataSet();
da.Fill(ds,"zz");
Dgd_user.DataSource=ds.Tables["zz"].DefaultView;
Dgd_user.DataBind();
cn.Close();
}
     private void Dgd_user_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
Dgd_user.CurrentPageIndex=e.NewPageIndex;//分页
BindGrid();
}
private void Button1_Click(object sender, System.EventArgs e)
{
#region 声明变量及得变量值
string vFromDateTime = "";
string vToDateTime = "";
string vFirstTime = "";
string vFirstEndTime = "";
vFromDateTime = Request.Params.Get("txtTime1");
vToDateTime = Request.Params.Get("txtTime2");
vFirstTime = Request.Params.Get("dropFirstTime");
vFirstEndTime = Request.Params.Get("dropEndTime"); vFromDateTime = vFromDateTime += " "+vFirstTime+":00:00"; 
vToDateTime = vToDateTime += " "+vFirstEndTime+":00:00";
#endregion    string a = "select * from statrecord where starttime>='"+vFromDateTime+"' and starttime<='"+vToDateTime+"' order by starttime";
cn.Open();
SqlCommand cm = new SqlCommand(a,cn);
try
{
cm.ExecuteNonQuery();
Label1.Text="搜索成功";

}
catch(SqlException)
{
Label1.Text="搜索失败";
Label1.Style["color"]="red";
}
cm.Connection.Close(); SqlDataAdapter da = new SqlDataAdapter(a,cn); DataSet ds = new DataSet();
da.Fill(ds,"zz");
Dgd_user.DataSource=ds.Tables["zz"].DefaultView;
Dgd_user.DataBind();
cn.Close(); }
}
--------------------我为了时间段筛选,不去调用BindGrid();已经做了很傻的事情,在搜索后自己写个绑定.但是分页等函数里就不好写了..
谢谢帮忙了.

解决方案 »

  1.   


    if(!IsPostBack) 
    {
    Session["Flag"] = "one"
    BindFile() 
    BindGrid();  
    } private void BindFile(string user)
    {
        strcn = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]; 
        cn = new SqlConnection(strcn);     SqlDataAdapter da = new SqlDataAdapter("select * from statrecord",cn); 
        ds = new DataSet(); //ds放在页首申明
        da.Fill(ds,"zz"); 
        cn.Close();
    }
    private void BindGrid()
    {
        this.Dgd_user.DataSource = ds.Tables[0];
        this.dgFile.DataBind();
    }
    private void Button1_Click(object sender, System.EventArgs e) 
    {
        GetSource();
        BindGrid();
        Session["Flag"] = "two";
    }
    private void GetSource()
    {
    string vFromDateTime = ""; 
    string vToDateTime = ""; 
    string vFirstTime = ""; 
    string vFirstEndTime = ""; 
    vFromDateTime = Request.Params.Get("txtTime1"); 
    vToDateTime = Request.Params.Get("txtTime2"); 
    vFirstTime = Request.Params.Get("dropFirstTime"); 
    vFirstEndTime = Request.Params.Get("dropEndTime"); vFromDateTime = vFromDateTime += " "+vFirstTime+":00:00"; 
    vToDateTime = vToDateTime += " "+vFirstEndTime+":00:00"; 
    #endregion   string a = "select * from statrecord where starttime>='"+vFromDateTime+"' and starttime <='"+vToDateTime+"' order by starttime"; 
    cn.Open(); 
    SqlCommand cm = new SqlCommand(a,cn); 
    try 

    cm.ExecuteNonQuery(); 
    Label1.Text="搜索成功"; } 
    catch(SqlException) 

    Label1.Text="搜索失败"; 
    Label1.Style["color"]="red"; 

    cm.Connection.Close(); SqlDataAdapter da = new SqlDataAdapter(a,cn); DataSet ds = new DataSet(); 
    da.Fill(ds,"zz");     
    }
    private void Dgd_user_PageIndexChanged(object source,System.Web.UI.WebControls.DataGridPageChangedEventArgs e) 

    Dgd_user.CurrentPageIndex=e.NewPageIndex;//分页 
    if((string)Session["Flag"] == "one" )
    {
        BindFile(); 
        BindGrid();  
    }
    else
    {
        GetSource();
        BindGrid();} 
    } 大概就是这样,试试看行不行
      

  2.   

    你把查询条件用 ViewState 来保存起来,只有点击查询的时候,再重新给值using System;
    using System.Data;
    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.Drawing;public partial class Sys_Right_RoleList : PageBase
    {
        protected Troika.Gps.BLL.Sys_Role bll = new Troika.Gps.BLL.Sys_Role();
        HtmlTableRow tr1;
        Label err;    protected void Page_Load(object sender, EventArgs e)
        {
            tr1 = (HtmlTableRow)Master.FindControl("tr1");
            err = (Label)Master.FindControl("lblerr");
            if (!IsPostBack)
            {
                BindGridView();
            }
        }    //数据绑定
        private void BindGridView()
        {
            DataView dv = null;
            DataSet ds = null;
            try
            {
                ds = bll.GetList(FilterExpression);
                dv = ds.Tables[0].DefaultView;
                PagedDataSource objPage = new PagedDataSource();
                if (dv.Count > 0)
                {
                    objPage.DataSource = dv;
                    objPage.AllowPaging = true;
                    objPage.PageSize = AspNetPager1.PageSize;
                    objPage.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
                    AspNetPager1.RecordCount = dv.Count;
                    AspNetPager1.SetEnable();
                    tr1.Visible = false;
                    err.Text = "";
                    GridView1.DataSource = objPage;
                    GridView1.DataBind();
                }
                else
                {
                    ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
                    GridView1.DataSource = ds.Tables[0].DefaultView;
                    GridView1.DataBind();
                    int count = GridView1.Rows[0].Cells.Count;
                    GridView1.Rows[0].Cells.Clear();
                    GridView1.Rows[0].Cells.Add(new TableCell());
                    GridView1.Rows[0].Cells[0].ColumnSpan = count;
                    AspNetPager1.RecordCount = 0;
                    GridView1.Rows[0].Cells[0].Text = "没有符合条件的数据......";
                    GridView1.Rows[0].Cells[0].ForeColor = Color.Red;
                }
               
            }
            catch
            {
                tr1.Visible = true;
                err.Text = "查询出错。";
            }
        }    //翻页控件的事件(注意:要手动在aspx页面添加 AspNetPager1_PageIndexChanged)
        public void AspNetPager1_PageIndexChanged(object sender, EventArgs e)
        {
            BindGridView();
        }    //搜索功能
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            FilterExpression = Search();
            BindGridView();
        }
        //GridView命令响应事件
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "DataDel":
                    string err = "删除角色";
                    string RoleID = e.CommandArgument.ToString();
                    try
                    {
                        bll.Delete(RoleID);
                        BindGridView();
                        err += "成功";
                    }
                    catch
                    {
                        err += "失败";
                    }
                    //写日志
                    WriteLog(err, "");
                    break;
            }
        }    //记录绑定事件
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            GridViewRow row = e.Row;        DataControlRowType RowType = e.Row.RowType;        if (RowType != DataControlRowType.Header && RowType != DataControlRowType.Footer)
            {
                ((LinkButton)e.Row.FindControl("lbnDelete")).Attributes.Add("onclick", "return confirm('您确定要删除吗?');");            row.Cells[0].Text = Convert.ToString(e.Row.RowIndex + 1 + AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1));
            }
        }    /// <summary>
        /// 组成查询条件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>    
        protected string Search()
        {
            //只列出本单位的数据
            string sql = " 1=1 and RoleType=1 and UnitID='" + Session["UnitID"] + "'";
            string RoleName = this.txtRoleName.Text.Trim();        if (!RoleName.Equals(""))
                sql += " and RoleName like '%" + RoleName + "%'";        return sql;
        }    /// <summary>
        /// 查询条件
        /// </summary>
        public string FilterExpression
        {
            get
            {
                if (this.ViewState["FilterExpression"] != null)
                    return (string)this.ViewState["FilterExpression"];
                return Search();
            }
            set
            {
                this.ViewState["FilterExpression"] = value;        }
        }
    }
      

  3.   

    如果数据量少的话可以考虑保存搜索结果到ViewSate或者Cache,如果多的话可以考虑使用分页查询
      

  4.   

    请使用 ViewState 来保存查询条件
    public partial class Sys_Right_RoleList : PageBase
    {
        protected Troika.XMGQ.BLL.Sys_Role bll = new Troika.XMGQ.BLL.Sys_Role();
        HtmlTableRow tr1;
        Label err;    protected void Page_Load(object sender, EventArgs e)
        {
            tr1 = (HtmlTableRow)Master.FindControl("tr1");
            err = (Label)Master.FindControl("lblerr");
            if (!IsPostBack)
            {
                BindGridView();
            }
        }    //数据绑定
        private void BindGridView()
        {
            DataView dv = null;
            DataSet ds = null;
            try
            {
                ds = bll.GetList(FilterExpression);
                dv = ds.Tables[0].DefaultView;
                PagedDataSource objPage = new PagedDataSource();
                if (dv.Count > 0)
                {
                    objPage.DataSource = dv;
                    objPage.AllowPaging = true;
                    objPage.PageSize = AspNetPager1.PageSize;
                    objPage.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
                    AspNetPager1.RecordCount = dv.Count;
                    AspNetPager1.SetEnable();
                    tr1.Visible = false;
                    err.Text = "";
                    GridView1.DataSource = objPage;
                    GridView1.DataBind();
                }
                else
                {
                    ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
                    GridView1.DataSource = ds.Tables[0].DefaultView;
                    GridView1.DataBind();
                    int count = GridView1.Rows[0].Cells.Count;
                    GridView1.Rows[0].Cells.Clear();
                    GridView1.Rows[0].Cells.Add(new TableCell());
                    GridView1.Rows[0].Cells[0].ColumnSpan = count;
                    AspNetPager1.RecordCount = 0;
                    GridView1.Rows[0].Cells[0].Text = "没有符合条件的数据......";
                    GridView1.Rows[0].Cells[0].ForeColor = Color.Red;
                }
               
            }
            catch
            {
                tr1.Visible = true;
                err.Text = "查询出错。";
            }
        }    //翻页控件的事件(注意:要手动在aspx页面添加 AspNetPager1_PageIndexChanged)
        public void AspNetPager1_PageIndexChanged(object sender, EventArgs e)
        {
            BindGridView();
        }    //搜索功能
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            FilterExpression = Search();
            BindGridView();
        }
        //GridView命令响应事件
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "DataDel":
                    string err = "删除角色";
                    string RoleID = e.CommandArgument.ToString();
                    try
                    {
                        bll.Delete(RoleID);
                        BindGridView();
                        err += "成功";
                    }
                    catch
                    {
                        err += "失败";
                    }
                    //写日志
                    WriteLog(err, "");
                    break;
            }
        }    //记录绑定事件
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            GridViewRow row = e.Row;        DataControlRowType RowType = e.Row.RowType;        if (RowType != DataControlRowType.Header && RowType != DataControlRowType.Footer)
            {
                ((LinkButton)e.Row.FindControl("lbnDelete")).Attributes.Add("onclick", "return confirm('您是否删除 " + row.Cells[1].Text + " 角色?');");            row.Cells[0].Text = Convert.ToString(e.Row.RowIndex + 1 + AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1));
            }
        }    /// <summary>
        /// 组成查询条件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>    
        protected string Search()
        {
            //只列出本单位的数据
            string sql = " 1=1 and RoleType=1 and UnitID='" + Session["UnitID"] + "'";
            string RoleName = this.txtRoleName.Text.Trim();        if (!RoleName.Equals(""))
                sql += " and RoleName like '%" + RoleName + "%'";        return sql;
        }    /// <summary>
        /// 查询条件
        /// </summary>
        public string FilterExpression
        {
            get
            {
                if (this.ViewState["FilterExpression"] != null)
                    return (string)this.ViewState["FilterExpression"];
                return Search();
            }
            set
            {
                this.ViewState["FilterExpression"] = value;        }
        }
    }