DataGrid更新数据困惑!update了后台得数据库,但是DataGrid页面没有更新,非得再次编辑状态按下取消,就见到新根新的数据了 !

junlion 2005-01-04 09:02:59
主要代码如下,愿听详解!
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
this.sqlDataAdapter1.Fill(dataSet31);
if(!IsPostBack)
{

this.DataGrid1.DataBind();
}
}

private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
DataGrid1.DataBind();

}

private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
DataGrid1.DataBind();

}

private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string UpdateCmd="update maintenance set Disposal_State=@state ,Remark=@remark where ID=@id";
SqlCommand MyCommand=new SqlCommand(UpdateCmd,this.sqlConnection1);
MyCommand.Parameters.Add(new SqlParameter("@state", SqlDbType.Bit,1));
MyCommand.Parameters.Add(new SqlParameter("@remark", SqlDbType.VarChar,100));
MyCommand.Parameters.Add(new SqlParameter("@id", SqlDbType.BigInt,8));
MyCommand.Parameters["@id"].Value = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();

// 取得 TableCell 物件的数目,也就是每一数据列之储存格的数目(等於 DataGrid 伺服器控制项中数据行的数目)
int NumCols = e.Item.Cells.Count;

CheckBox CurrentCheckBox = (CheckBox)e.Item.Cells[5].Controls[1];
if(CurrentCheckBox.Checked==true)
MyCommand.Parameters["@state"].Value=1;
else
MyCommand.Parameters["@state"].Value=0;

TextBox CurrentTextBox = (TextBox)e.Item.Cells[6].Controls[1];
MyCommand.Parameters["@remark"].Value=CurrentTextBox.Text.ToString();

MyCommand.Connection.Open();
MyCommand.ExecuteNonQuery();
MyCommand.Connection.Close();

//// 完成更新作业后使数据行跳出编辑模式
DataGrid1.EditItemIndex = -1;
// this.sqlDataAdapter1.Update(dataSet31);
DataGrid1.DataBind();




}



}
...全文
222 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
junlion 2005-01-05
  • 打赏
  • 举报
回复
自己解决了代码如下
public class missionmanage : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid MyDataGrid;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button btReturn;
public SqlConnection myConnection;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(Session["Student_User"]==null||Session["Groups"]==null)
Response.Redirect("adminlogin.aspx");

myConnection=new SqlConnection("server=(local);uid=jnu;pwd=nok;initial catalog=netservice");
if (!IsPostBack)
BindGrid();


}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.MyDataGrid.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.MyDataGrid_CancelCommand);
this.MyDataGrid.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.MyDataGrid_EditCommand);
this.MyDataGrid.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.MyDataGrid_UpdateCommand);
this.btReturn.Click += new System.EventHandler(this.btReturn_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void MyDataGrid_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
MyDataGrid.EditItemIndex = (int)e.Item.ItemIndex;
BindGrid();

}

private void MyDataGrid_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string UpdateCmd="update maintenance set Disposal_State=@state ,Remark=@remark where ID=@id";
SqlCommand MyCommand=new SqlCommand(UpdateCmd,myConnection);
MyCommand.Parameters.Add(new SqlParameter("@state", SqlDbType.Bit,1));
MyCommand.Parameters.Add(new SqlParameter("@remark", SqlDbType.VarChar,100));
MyCommand.Parameters.Add(new SqlParameter("@id", SqlDbType.BigInt,8));
MyCommand.Parameters["@id"].Value = MyDataGrid.DataKeys[e.Item.ItemIndex].ToString();

// 取得 TableCell 物件的数目,也就是每一数据列之储存格的数目(等於 DataGrid 伺服器控制项中数据行的数目)
int NumCols = e.Item.Cells.Count;

CheckBox CurrentCheckBox = (CheckBox)e.Item.Cells[5].Controls[1];
if(CurrentCheckBox.Checked==true)
MyCommand.Parameters["@state"].Value=1;
else
MyCommand.Parameters["@state"].Value=0;

TextBox CurrentTextBox = (TextBox)e.Item.Cells[6].Controls[1];
MyCommand.Parameters["@remark"].Value=CurrentTextBox.Text.ToString();

MyCommand.Connection.Open();
MyCommand.ExecuteNonQuery();
MyCommand.Connection.Close();

//// 完成更新作业后使数据行跳出编辑模式

MyDataGrid.EditItemIndex = -1;
BindGrid();
}

private void MyDataGrid_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
MyDataGrid.EditItemIndex = -1;
BindGrid();
}

public void BindGrid()
{
string strSelect="select ID, User_ID, User_Name, User_IP, Dorm_Num, " +
" Disposal_State, Remark from maintenance"
+" where(Respond_State='1' and Disposal_State='0' and Dorm_Num>"
+Session["Groups"].ToString().Trim()+"000"
+" and "+"Dorm_Num<"+Session["Groups"].ToString().Trim()+"999"+ ")";

SqlDataAdapter myCommand = new SqlDataAdapter(strSelect,myConnection);

DataSet ds = new DataSet();
myCommand.Fill(ds, "mission");

MyDataGrid.DataSource=ds.Tables["mission"].DefaultView;
MyDataGrid.DataBind();
}

private void btReturn_Click(object sender, System.EventArgs e)
{
Response.Redirect("query.aspx");
}

}
}
建议参考微软的ASP.NET 入门教程 http://chs.gotdotnet.com/quickstart/aspplus/
junlion 2005-01-04
  • 打赏
  • 举报
回复
由于DataGrid的数据连接是用vs.net studio生成也就是说数据库的连接都在写在InitializeComponent();里面了,而这个函数的调用是在page_load之前.所以不能用楼上的方法
greystar 2005-01-04
  • 打赏
  • 举报
回复
更新后要记得再次绑定一下
噯卟釋手 2005-01-04
  • 打赏
  • 举报
回复
那就把你绑定的所有代码都加到if(!IsPostBack)
{
//这里加入所有的绑定代码 数据库连接 数据库源 绑定语句
}
junlion 2005-01-04
  • 打赏
  • 举报
回复
"this.sqlDataAdapter1.Update(dataSet31);这句不删除应该就可以了" 这句话没有删掉,结果是一样,
"把这个this.sqlDataAdapter1.Fill(dataSet31);放到这里
if(!IsPostBack)
{
this.sqlDataAdapter1.Fill(dataSet31); //加进来
this.DataGrid1.DataBind();
}
"这样也是不行的
噯卟釋手 2005-01-04
  • 打赏
  • 举报
回复
把这个this.sqlDataAdapter1.Fill(dataSet31);放到这里
if(!IsPostBack)
{
this.sqlDataAdapter1.Fill(dataSet31); //加进来
this.DataGrid1.DataBind();
}
wwh207 2005-01-04
  • 打赏
  • 举报
回复
// this.sqlDataAdapter1.Update(dataSet31);这句不删除应该就可以了
junlion 2005-01-04
  • 打赏
  • 举报
回复
哎呀,各位大侠给个具体的代码吧?光说很难解决问题的,因为小弟水平有限啊!
jackief 2005-01-04
  • 打赏
  • 举报
回复
update完了之后一定要重新在绑定一次。
junlion 2005-01-04
  • 打赏
  • 举报
回复
数据集得根新如何写 ?this.sqlDataAdapter1.Update(dataSet31)这个不算吗?
exing 2005-01-04
  • 打赏
  • 举报
回复
只有DataGrid1.DataBind();是不够的,因为你的数据集没有更新
fluxayxxx 2005-01-04
  • 打赏
  • 举报
回复
自己写个绑定数据的函数,每次更新后都要调用
a7lang 2005-01-04
  • 打赏
  • 举报
回复
更新后重新绑定
designonline 2005-01-04
  • 打赏
  • 举报
回复
DataGrid1.DataBind();这个算不算是重新绑定啊?
应该本身带的不算,你自己写个函数来实现吧
junlion 2005-01-04
  • 打赏
  • 举报
回复
都有啊Page_Load,DataGrid1_EditCommand,DataGrid1_UpdateCommand 里面都有 DataGrid1.DataBind();这个算不算是重新绑定啊?
myregret 2005-01-04
  • 打赏
  • 举报
回复
是不是更新之后没有重新绑定数据啊

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧