注册 登录
编程论坛 ASP.NET技术论坛

DataGrid分页的问题

nply 发布于 2007-11-08 09:57, 630 次点击
DataGrid 里我是用ImageButton按钮,首页,上一页,下一页,尾页,这四个ImageButton按钮,当我在第二页只有一条记录时,要删除这条记录时,会出现CurrentPageIndex < PageCount.
要怎么解决?要怎么绑定?才可以回到上一页。谢谢了。
3 回复
#2
cyyu_ryh2007-11-08 13:10
重新绑定整个数据
#3
tianlei2007-11-08 16:52
删除后重新绑定
#4
chashen88882007-11-08 18:45
论坛有不少,何不搜一下?

private void BindDataGrid()
{
SqlConnection myConnection=new SqlConnection();
string strConn="server=Localhost;uid=sa;pwd=123456;database=stucard";
myConnection.ConnectionString=strConn;
SqlDataAdapter myCommand=new SqlDataAdapter("select id,name,sex,pwd from admini",myConnection);
DataSet ds=new DataSet();
myCommand.Fill(ds,"scores");
DataGrid1.DataSource=ds.Tables["scores"].DefaultView;
DataGrid1.DataBind();
}//这个是绑定的。
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex=e.NewPageIndex;
BindDataGrid();
}//这个是分页的。
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
error();
SqlConnection myConnection=new SqlConnection("server=localhost;uid=sa;pwd=123456;database=stucard");
string strCommand="delete from admini where id='"+DataGrid1.DataKeys[(int)e.Item.ItemIndex]+"'";
SqlCommand myCommand=new SqlCommand(strCommand,myConnection);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
BindDataGrid();//删除后记得绑定。
}//这个是删除的。
你看着办吧。
1