DataGrid翻页问题,请高手指点.在线等等等高手!
说明:这个页面做了一个搜索功能.页面默认载入初始数据并绑定GridData控件,其分页功能完全正常.但是按搜索功能后,有时会出现如下面的错误提示:
1.如果按部门搜索到多于一页记录,比如有三页,那么第一页显示正常的,当按下一页时则变成页面默认载入时的页面了.而不是刚刚搜索到的数据.请问这是怎么回事.
有人说是DataGrid1.CurrentPageIndex = e.NewPageIndex;的问题,但是我如何在搜索函数里调用这个呢?或者有没有其他更好的办法?请高手予以详细指点.
另外,再向各位高手请教,我觉得我的程序写的很繁烦,如何改进?
namespace ryb
{
private void Page_Load(object sender, System.EventArgs e)
{
//DataGrid控件初始化
if(!IsPostBack)
{
getdepname();
BindGrid();
Showstats();
}
}
//初始化部门控件值
private void getdepname()
{
Drp_dep.Items.Clear();
SqlConnection conn=new SqlConnection(path);
conn.Open();
SqlCommand cmd=new SqlCommand("sp_seldepname",conn);
cmd.CommandType=CommandType.StoredProcedure;
SqlDataReader myDataReader=cmd.ExecuteReader();
Drp_dep.Items.Add("所有部门");
while(myDataReader.Read()==true)
{
Drp_dep.Items.Add(myDataReader.GetString(1).ToString());
}
conn.Close();
}
private void BindGrid()
{
SqlConnection con=new SqlConnection(path);
SqlCommand cmd=new SqlCommand("sp_showhumanlist",con);
con.Open();
DataSet ds = new DataSet();
cmd.CommandType = CommandType.StoredProcedure; // use stored proc for perf
SqlDataAdapter dap = new SqlDataAdapter();
dap.SelectCommand = cmd;
dap.Fill(ds);
DataGrid1.DataSource = ds.Tables[0].DefaultView;
DataGrid1.DataBind();
Showstats();
Label1.Text=DataGrid1.Items.Count.ToString();
}
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
BindGrid();
Label1.Text=DataGrid1.Items.Count.ToString();
}
private void Showstats()
{
lblCurrentIndex.Text="第"+(DataGrid1.CurrentPageIndex+1).ToString()+"页";
lblPagecount.Text="总共"+DataGrid1.PageCount.ToString()+"页";
}
private void Button1_Click(object sender, System.EventArgs e)
{
SqlConnection conn=new SqlConnection(path);
conn.Open();
//假设所选部门为所有部门
if(Drp_dep.SelectedItem.Value.Trim().ToString()=="所有部门")
{
//假设所选部门为所有部门,姓名为空的条件下
if(human_name.Text.Trim().ToString()=="")
{
string strsql="select id,name,sex,birthday=convert(char(11),birthday,111),age,mingzhu,xueli,xueli,marrige,prolitics,sfzid,jiguan,jrbdwsj,gzlb,depment,zhiwu,school,specialy,finishtime=convert(char(11),finishtime,111),tel from db_human";
SqlCommand cmd=new SqlCommand(strsql,conn);
cmd.CommandType=CommandType.Text;
SqlDataAdapter dap = new SqlDataAdapter();
DataSet ds=new DataSet();
dap.SelectCommand = cmd;
dap.Fill(ds);
DataGrid1.DataSource = ds.Tables[0].DefaultView;
DataGrid1.DataBind();
Showstats();
Label1.Text=ds.Tables[0].Rows.Count.ToString();
}
//假设所选部门为所有部门,但姓名不为空的条件下
else
{
string strname=human_name.Text.Trim().ToString();
string strsql_sybm_havename="select id,name,sex,birthday=convert(char(11),birthday,111),age,mingzhu,xueli,xueli,marrige,prolitics,sfzid,jiguan,jrbdwsj,gzlb,depment,zhiwu,school,specialy,finishtime=convert(char(11),finishtime,111),tel from db_human where name=" + "'" +strname+"'";
SqlCommand cmd1=new SqlCommand(strsql_sybm_havename,conn); cmd1.CommandType=CommandType.Text;
SqlDataAdapter adp=new SqlDataAdapter();
DataSet ds=new DataSet();
adp.SelectCommand=cmd1;
adp.Fill(ds);
DataGrid1.DataSource=ds.Tables[0].DefaultView;
//省略判断语句,查到数据库中有记录,并显示
DataGrid1.DataBind();
Showstats();
Label1.Text=ds.Tables[0].Rows.Count.ToString();
}
}
}
//所选部门不为所有部门的处理
else
{
//所选部门不为所有部门,所有人员信息
if(human_name.Text.Trim().ToString()=="")
{
string depname=Drp_dep.SelectedItem.Value.Trim().ToString();
string strsql_depname="select id,name,sex,birthday=convert(char(11),birthday,111),age,mingzhu,xueli,xueli,marrige,prolitics,sfzid,jiguan,jrbdwsj,gzlb,depment,zhiwu,school,specialy,finishtime=convert(char(11),finishtime,111),tel from db_human where depment=" + "'"+depname+"'";
SqlCommand cmd2=new SqlCommand(strsql_depname,conn);
cmd2.CommandType=CommandType.Text;
SqlDataAdapter dap = new SqlDataAdapter();
DataSet ds=new DataSet();
dap.SelectCommand = cmd2;
dap.Fill(ds);
DataGrid1.DataSource = ds.Tables[0].DefaultView;
DataGrid1.DataBind();
Showstats();
Label1.Text=ds.Tables[0].Rows.Count.ToString();
}
//所选部门不为所有部门,显示按姓名搜索结果
else
{
string depname=Drp_dep.SelectedItem.Value.Trim().ToString();
string strname=human_name.Text.Trim().ToString();
string strsql_depname_name="select id,name,sex,birthday=convert(char(11),birthday,111),age,mingzhu,xueli,xueli,marrige,prolitics,sfzid,jiguan,jrbdwsj,gzlb,depment,zhiwu,school,specialy,finishtime=convert(char(11),finishtime,111),tel from db_human where depment=" + "'"+depname+"'"+" "+"and"+" "+"name="+"'"+strname+"'";
SqlCommand cmd3=new SqlCommand(strsql_depname_name,conn); cmd3.CommandType=CommandType.Text;
SqlDataAdapter dap3 = new SqlDataAdapter();
DataSet ds=new DataSet();
dap3.SelectCommand = cmd3;
dap3.Fill(ds);
... 省略判断......
DataGrid1.DataSource = ds.Tables[0].DefaultView;
DataGrid1.DataBind();
Showstats();
Label1.Text=ds.Tables[0].Rows.Count.ToString();
}}}}}}
1.如果按部门搜索到多于一页记录,比如有三页,那么第一页显示正常的,当按下一页时则变成页面默认载入时的页面了.而不是刚刚搜索到的数据.请问这是怎么回事.
有人说是DataGrid1.CurrentPageIndex = e.NewPageIndex;的问题,但是我如何在搜索函数里调用这个呢?或者有没有其他更好的办法?请高手予以详细指点.
另外,再向各位高手请教,我觉得我的程序写的很繁烦,如何改进?
namespace ryb
{
private void Page_Load(object sender, System.EventArgs e)
{
//DataGrid控件初始化
if(!IsPostBack)
{
getdepname();
BindGrid();
Showstats();
}
}
//初始化部门控件值
private void getdepname()
{
Drp_dep.Items.Clear();
SqlConnection conn=new SqlConnection(path);
conn.Open();
SqlCommand cmd=new SqlCommand("sp_seldepname",conn);
cmd.CommandType=CommandType.StoredProcedure;
SqlDataReader myDataReader=cmd.ExecuteReader();
Drp_dep.Items.Add("所有部门");
while(myDataReader.Read()==true)
{
Drp_dep.Items.Add(myDataReader.GetString(1).ToString());
}
conn.Close();
}
private void BindGrid()
{
SqlConnection con=new SqlConnection(path);
SqlCommand cmd=new SqlCommand("sp_showhumanlist",con);
con.Open();
DataSet ds = new DataSet();
cmd.CommandType = CommandType.StoredProcedure; // use stored proc for perf
SqlDataAdapter dap = new SqlDataAdapter();
dap.SelectCommand = cmd;
dap.Fill(ds);
DataGrid1.DataSource = ds.Tables[0].DefaultView;
DataGrid1.DataBind();
Showstats();
Label1.Text=DataGrid1.Items.Count.ToString();
}
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
BindGrid();
Label1.Text=DataGrid1.Items.Count.ToString();
}
private void Showstats()
{
lblCurrentIndex.Text="第"+(DataGrid1.CurrentPageIndex+1).ToString()+"页";
lblPagecount.Text="总共"+DataGrid1.PageCount.ToString()+"页";
}
private void Button1_Click(object sender, System.EventArgs e)
{
SqlConnection conn=new SqlConnection(path);
conn.Open();
//假设所选部门为所有部门
if(Drp_dep.SelectedItem.Value.Trim().ToString()=="所有部门")
{
//假设所选部门为所有部门,姓名为空的条件下
if(human_name.Text.Trim().ToString()=="")
{
string strsql="select id,name,sex,birthday=convert(char(11),birthday,111),age,mingzhu,xueli,xueli,marrige,prolitics,sfzid,jiguan,jrbdwsj,gzlb,depment,zhiwu,school,specialy,finishtime=convert(char(11),finishtime,111),tel from db_human";
SqlCommand cmd=new SqlCommand(strsql,conn);
cmd.CommandType=CommandType.Text;
SqlDataAdapter dap = new SqlDataAdapter();
DataSet ds=new DataSet();
dap.SelectCommand = cmd;
dap.Fill(ds);
DataGrid1.DataSource = ds.Tables[0].DefaultView;
DataGrid1.DataBind();
Showstats();
Label1.Text=ds.Tables[0].Rows.Count.ToString();
}
//假设所选部门为所有部门,但姓名不为空的条件下
else
{
string strname=human_name.Text.Trim().ToString();
string strsql_sybm_havename="select id,name,sex,birthday=convert(char(11),birthday,111),age,mingzhu,xueli,xueli,marrige,prolitics,sfzid,jiguan,jrbdwsj,gzlb,depment,zhiwu,school,specialy,finishtime=convert(char(11),finishtime,111),tel from db_human where name=" + "'" +strname+"'";
SqlCommand cmd1=new SqlCommand(strsql_sybm_havename,conn); cmd1.CommandType=CommandType.Text;
SqlDataAdapter adp=new SqlDataAdapter();
DataSet ds=new DataSet();
adp.SelectCommand=cmd1;
adp.Fill(ds);
DataGrid1.DataSource=ds.Tables[0].DefaultView;
//省略判断语句,查到数据库中有记录,并显示
DataGrid1.DataBind();
Showstats();
Label1.Text=ds.Tables[0].Rows.Count.ToString();
}
}
}
//所选部门不为所有部门的处理
else
{
//所选部门不为所有部门,所有人员信息
if(human_name.Text.Trim().ToString()=="")
{
string depname=Drp_dep.SelectedItem.Value.Trim().ToString();
string strsql_depname="select id,name,sex,birthday=convert(char(11),birthday,111),age,mingzhu,xueli,xueli,marrige,prolitics,sfzid,jiguan,jrbdwsj,gzlb,depment,zhiwu,school,specialy,finishtime=convert(char(11),finishtime,111),tel from db_human where depment=" + "'"+depname+"'";
SqlCommand cmd2=new SqlCommand(strsql_depname,conn);
cmd2.CommandType=CommandType.Text;
SqlDataAdapter dap = new SqlDataAdapter();
DataSet ds=new DataSet();
dap.SelectCommand = cmd2;
dap.Fill(ds);
DataGrid1.DataSource = ds.Tables[0].DefaultView;
DataGrid1.DataBind();
Showstats();
Label1.Text=ds.Tables[0].Rows.Count.ToString();
}
//所选部门不为所有部门,显示按姓名搜索结果
else
{
string depname=Drp_dep.SelectedItem.Value.Trim().ToString();
string strname=human_name.Text.Trim().ToString();
string strsql_depname_name="select id,name,sex,birthday=convert(char(11),birthday,111),age,mingzhu,xueli,xueli,marrige,prolitics,sfzid,jiguan,jrbdwsj,gzlb,depment,zhiwu,school,specialy,finishtime=convert(char(11),finishtime,111),tel from db_human where depment=" + "'"+depname+"'"+" "+"and"+" "+"name="+"'"+strname+"'";
SqlCommand cmd3=new SqlCommand(strsql_depname_name,conn); cmd3.CommandType=CommandType.Text;
SqlDataAdapter dap3 = new SqlDataAdapter();
DataSet ds=new DataSet();
dap3.SelectCommand = cmd3;
dap3.Fill(ds);
... 省略判断......
DataGrid1.DataSource = ds.Tables[0].DefaultView;
DataGrid1.DataBind();
Showstats();
Label1.Text=ds.Tables[0].Rows.Count.ToString();
}}}}}}
...全文
相关推荐
jQuery EasyUI datagrid在翻页以后仍能记录被选中行的实现代码
这个问题可以通过监听datagrid的事件和维护一个外部的数据结构来解决。下面将详细介绍如何实现这一功能。首先,我们需要引入jQuery EasyUI的相关库,并设置datagrid的基本属性。
datagrid datagrid-scrollview 列宽不够时,无法翻页问题
近期在用datagrid-scrollview时,发现如果列数较少,不产生横向滚动条时,则不会触发虚拟滚动翻页的动作,只能查出第一批次的数据信息,经过大量的时间测试改善,发现需要改变源码中的判断条件。
带有上下翻页跳转的DataGrid控件
带有上下翻页跳转的DataGrid控件,可以自动翻页的DataGrid控件
wpf DataGrid 自定义样式!
示例代码:```xml<Window> <Window.Resources> <Style TargetType="DataGrid"> <!
在jquery easyui中的datagrid中添加searchbox!
</tr> </thead></table><div id="toolbar"> <!


