一个DataGrid。有分页功能。每一行都有“删除” 按钮。
问题是:
如果最后一页,就有一行记录了,删除后,页数应该减一。
就是这个减一,我不会做,该怎么做呢?
我的代码:
private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(e.CommandName=="删除")
{
string idnum = e.CommandArgument.ToString();
string sql = "delete CompactManage where id='"+ idnum + "'";
SqlCon sc = new SqlCon();
sc.ExecuteSql(sql);
string clientstr = @"<script language=javascript>alert('删除成功!')</script>";
Response.Write(clientstr);
Session["buttonsql"] = "";
con = new OleDbConnection();
con.ConnectionString = Application["strcon"].ToString();
con.Open();
DataGrid1.DataSource = ds.Tables["plan"];
DataGrid1.DataBind();
Label2.Text = "当前第1页/共"+DataGrid1.PageCount+"页";
}
else
{
//DataGrid1.Visible = true;
//Response.Write("没有数据");
DataGrid1.DataSource = ds.Tables["plan"];
DataGrid1.DataBind();
Label2.Visible = false;
}
//Response.Write(DataGrid1.Visible.ToString());
con.Close();
}
}一般报错,就出在DataGrid1.DataBind();上
问题是:
如果最后一页,就有一行记录了,删除后,页数应该减一。
就是这个减一,我不会做,该怎么做呢?
我的代码:
private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(e.CommandName=="删除")
{
string idnum = e.CommandArgument.ToString();
string sql = "delete CompactManage where id='"+ idnum + "'";
SqlCon sc = new SqlCon();
sc.ExecuteSql(sql);
string clientstr = @"<script language=javascript>alert('删除成功!')</script>";
Response.Write(clientstr);
Session["buttonsql"] = "";
con = new OleDbConnection();
con.ConnectionString = Application["strcon"].ToString();
con.Open();
DataGrid1.DataSource = ds.Tables["plan"];
DataGrid1.DataBind();
Label2.Text = "当前第1页/共"+DataGrid1.PageCount+"页";
}
else
{
//DataGrid1.Visible = true;
//Response.Write("没有数据");
DataGrid1.DataSource = ds.Tables["plan"];
DataGrid1.DataBind();
Label2.Visible = false;
}
//Response.Write(DataGrid1.Visible.ToString());
con.Close();
}
}一般报错,就出在DataGrid1.DataBind();上
if((e.Item.ItemIndex==0) && (((DataGrid)sender).CurrentPageIndex == ((DataGrid)sender).PageCount-1) && (((DataGrid)sender).CurrentPageIndex > 0))
((DataGrid)sender).CurrentPageIndex --;
((DataGrid)sender).EditItemIndex = -1;
//绑定语句.............
if(DataGrid1.Items.Count==1)
DataGrid1.CurrentPageIndex=DataGrid1.PageCount-2;
{
if(e.CommandName=="删除")
{
string idnum = e.CommandArgument.ToString();
string sql = "delete CompactManage where id='"+ idnum + "'";
SqlCon sc = new SqlCon();
sc.ExecuteSql(sql);
string clientstr = @"<script language=javascript>alert('删除成功!')</script>";
Response.Write(clientstr);
Session["buttonsql"] = "";
con = new OleDbConnection();
con.ConnectionString = Application["strcon"].ToString();
con.Open();
DataGrid1.DataSource = ds.Tables["plan"];
try
{
DataGrid1.DataBind();
}
catch
{
DataGrid1.CurrentPageIndex=this.DataGrid1.PageCount-1;
DataGrid1.DataBind();
}
Label2.Text = "当前第1页/共"+DataGrid1.PageCount+"页";
}
else
{
//DataGrid1.Visible = true;
//Response.Write("没有数据");
DataGrid1.DataSource = ds.Tables["plan"];
DataGrid1.DataBind();
Label2.Visible = false;
}
//Response.Write(DataGrid1.Visible.ToString());
con.Close();
}
}
是什么意思?
if(DataGrid1.Item.Count%DataGrid1.PageSize == 1)
{
DataGrid1.CurrentPageIndex -= 1;
}
DataGrid1.DataSource = ds.Tables["plan"];
DataGrid1.DataBind();
if(DataGrid1.CurrentPageIndex == DataGrid1.PageCount-1 && DataGrid1.Item.Count%DataGrid1.PageSize == 1)
是取当前DATAGRID页面有几条记录。
try
{
MyDataGrid1.DataBind();
}
catch
{
int i=MyDataGrid1.CurrentPageIndex;
while(i>=MyDataGrid1.PageCount)
{
i--;
}
MyDataGrid1.CurrentPageIndex=i;
MyDataGrid1.DataBind ();
}
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string 代码 = e.Item.Cells[2].Text;
int ret = 行业信息维护类.删除行业信息(代码);
if(ret==1)
{
客户端.alert(this,"删除成功!");
}
else
客户端.alert(this,"删除失败!");
try
{
Bind(DataGrid1); //绑定函数
}
catch
{
DataGrid1.CurrentPageIndex-- ;
Bind(DataGrid1);
}
}
((DataGrid)sender).CurrentPageIndex --;
((DataGrid)sender).EditItemIndex = -1;
他得方法好
((DataGrid)sender).CurrentPageIndex --;
((DataGrid)sender).EditItemIndex = -1;
(DataGrid)sender就是e啊
if((e.Item.ItemIndex==0) && (ListLibDataGrid.CurrentPageIndex == ListLibDataGrid.PageCount-1) && (ListLibDataGrid.CurrentPageIndex > 0))
{
ListLibDataGrid.CurrentPageIndex --;
ListLibDataGrid.EditItemIndex = -1;
}
BindData();
这样子可以刚试过