private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
string conn = "data source=192.168.100.124;database=DBFILE;trusted_connection = yes";
string str1 = "delete from authdescribe where AuthorityID =";
string str2 = ((TextBox)e.Item.FindControl("AuthorityID")).Text;
string str3 = str1 + "'" + str2 + "'" ;
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.Connection = new SqlConnection(conn);
sqlcmd.CommandText = str3;
sqlcmd.Connection.Open();
sqlcmd.CommandType = CommandType.Text;
sqlcmd.ExecuteNonQuery();
sqlcmd.Connection.Close();
setbind(); }
}
protected void btn_Insert_Click(object sender, EventArgs e)
{
if ((this.TextBox1.Text != "") && (this.TextBox2.Text != ""))
{
string StrConnection = "data source=192.168.100.124;database=DBFILE;trusted_connection = yes";
string str1 = "insert into authdescribe(AuthorityID,PowderDescribe) values (";
string str2 = this.TextBox1.Text;
string str3 = this.TextBox2.Text;
string str4 = str1 + "'" + str2 + "'" + "," +"'"+ str3 + "'" + ")";
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.Connection = new SqlConnection(StrConnection);
sqlcmd.CommandText = str4;
sqlcmd.Connection.Open();
sqlcmd.CommandType = CommandType.Text;
sqlcmd.ExecuteNonQuery();
sqlcmd.Connection.Close();
this.TextBox1.Text = "";
this.TextBox2.Text = "";
setbind();
} private void setbind()
{
string conn = "data source=192.168.100.124;database=DBFILE;trusted_connection = yes";
SqlDataAdapter da = new SqlDataAdapter("Select AuthorityID,PowderDescribe from authdescribe ", conn);
DataSet ds = new DataSet();
da.Fill(ds, "table1");
this.DataGrid1.DataSource = ds.Tables["table1"];
this.DataGrid1.DataBind();
}
现在的问题是,我的页面上有2页数据,但是在删除第二页数局的时候,老是报错,删除是成功了,但在榜定的时候出错了,说CurrentPageIndex 有问题,不知道怎么做?和PageCount有关系吗??如何解决?
{
if (e.CommandName == "Delete")
{
string conn = "data source=192.168.100.124;database=DBFILE;trusted_connection = yes";
string str1 = "delete from authdescribe where AuthorityID =";
string str2 = ((TextBox)e.Item.FindControl("AuthorityID")).Text;
string str3 = str1 + "'" + str2 + "'" ;
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.Connection = new SqlConnection(conn);
sqlcmd.CommandText = str3;
sqlcmd.Connection.Open();
sqlcmd.CommandType = CommandType.Text;
sqlcmd.ExecuteNonQuery();
sqlcmd.Connection.Close();
setbind(); }
}
protected void btn_Insert_Click(object sender, EventArgs e)
{
if ((this.TextBox1.Text != "") && (this.TextBox2.Text != ""))
{
string StrConnection = "data source=192.168.100.124;database=DBFILE;trusted_connection = yes";
string str1 = "insert into authdescribe(AuthorityID,PowderDescribe) values (";
string str2 = this.TextBox1.Text;
string str3 = this.TextBox2.Text;
string str4 = str1 + "'" + str2 + "'" + "," +"'"+ str3 + "'" + ")";
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.Connection = new SqlConnection(StrConnection);
sqlcmd.CommandText = str4;
sqlcmd.Connection.Open();
sqlcmd.CommandType = CommandType.Text;
sqlcmd.ExecuteNonQuery();
sqlcmd.Connection.Close();
this.TextBox1.Text = "";
this.TextBox2.Text = "";
setbind();
} private void setbind()
{
string conn = "data source=192.168.100.124;database=DBFILE;trusted_connection = yes";
SqlDataAdapter da = new SqlDataAdapter("Select AuthorityID,PowderDescribe from authdescribe ", conn);
DataSet ds = new DataSet();
da.Fill(ds, "table1");
this.DataGrid1.DataSource = ds.Tables["table1"];
this.DataGrid1.DataBind();
}
现在的问题是,我的页面上有2页数据,但是在删除第二页数局的时候,老是报错,删除是成功了,但在榜定的时候出错了,说CurrentPageIndex 有问题,不知道怎么做?和PageCount有关系吗??如何解决?
解决方案 »
- (Vista/7)如何动态获取管理员权限
- 动态图片,可以在IE6上显示,但是用firefox浏览时 就不显示图片
- 如何建立路径长度超过260字符的文件夹
- WinForm的Datagridview如何禁止DataGridViewButtonColumn列排序
- 也发一个迷你俄罗斯方块(手机版)
- 怎样才可以获取外网IP
- 来~来~一个有趣的问题:根据这段数据库错误提示能对该mysql网站进行注入攻击吗?怎么登陆进去呢?
- 求命名空间
- 请教一个铵钮调用另一个按钮事件的办法???
- 问了两天了,还没有满意的答案,继续!
- 我写了一个服务 要拿到另一台机器上面运行 怎么打包出去???
- 怎样使子窗体填满主窗体的工作区??
baidu搜索一把就能找到解决方案。
//执行删除命令//如果当前页上只有一条数据并且DataGrid的当前页索引大于0,那么就让DataGrid返回上一页
if(DataGrid1.Items.Count == 1 && DataGrid1.CurrentPageIndex > 0){
DataGrid1.CurrentPageIndex--;
}
//重新绑定数据
BindData();
}
在setbind()中判斷。可試試﹕
int p=DataGrid1.CurrentPageIndex;
DataGrid1.CurrentPageIndex=0;
this.DataGrid1.DataSource = ds.Tables["table1"];
this.DataGrid1.DataBind();
if(DataGrid1.PageCount>p) DataGrid1.CurrentPageIndex=p;
if (DataGrid1.Items.Count==1&&DataGrid1.CurrentPageIndex>0)
{
DataGrid1.CurrentPageIndex -=1;
}
//执行删除命令//如果当前页上只有一条数据并且DataGrid的当前页索引大于0,那么就让DataGrid返回上一页
if(DataGrid1.Items.Count == 1 && DataGrid1.CurrentPageIndex > 0){
DataGrid1.CurrentPageIndex--;
}
//重新绑定数据
BindData();
}
XIE XIE
SAN FEN!