使用gridview更新数据,不能显示更新后的数据
usingSystem;usingSystem.Configuration;usingSystem.Data;usingSystem.Linq;usingSystem.W...
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
DataSet GetDataSet()
{
SqlConnection myCon = new SqlConnection();
myCon.ConnectionString = "Persist security info=false;User id=saa;pwd=saa;database=Northwind;server=(local)";
myCon.Open();
SqlCommand selectCMD = new SqlCommand("SELECT top 10 CustomerID,CompanyName,Address FROM Customers", myCon);
SqlDataAdapter custDA = new SqlDataAdapter();
custDA.SelectCommand = selectCMD;
DataSet custDS = new DataSet();
custDA.Fill(custDS, "Customers");
myCon.Close();
return custDS;
}
void LoadData()
{
DataSet ds = GetDataSet();
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
LoadData();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection myCon = new SqlConnection();
myCon.ConnectionString = "Persist security info=true;User id=saa;pwd=saa;database=Northwind;server=(local)";
myCon.Open();
SqlCommand myCom = new SqlCommand();
myCom.Connection = myCon;
string sql=
"update Customers set CompanyName='"
+((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString()
+"',Address='"
+ ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString()
+"'where CustomerID='"
+ GridView1.Rows[e.RowIndex].Cells[0].Text.ToString()+ "'";
myCom.CommandText = sql;
myCom.ExecuteNonQuery();
myCon.Close();
GridView1.EditIndex = -1;
LoadData();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
LoadData();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadData();
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
} 展开
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
DataSet GetDataSet()
{
SqlConnection myCon = new SqlConnection();
myCon.ConnectionString = "Persist security info=false;User id=saa;pwd=saa;database=Northwind;server=(local)";
myCon.Open();
SqlCommand selectCMD = new SqlCommand("SELECT top 10 CustomerID,CompanyName,Address FROM Customers", myCon);
SqlDataAdapter custDA = new SqlDataAdapter();
custDA.SelectCommand = selectCMD;
DataSet custDS = new DataSet();
custDA.Fill(custDS, "Customers");
myCon.Close();
return custDS;
}
void LoadData()
{
DataSet ds = GetDataSet();
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
LoadData();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection myCon = new SqlConnection();
myCon.ConnectionString = "Persist security info=true;User id=saa;pwd=saa;database=Northwind;server=(local)";
myCon.Open();
SqlCommand myCom = new SqlCommand();
myCom.Connection = myCon;
string sql=
"update Customers set CompanyName='"
+((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString()
+"',Address='"
+ ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString()
+"'where CustomerID='"
+ GridView1.Rows[e.RowIndex].Cells[0].Text.ToString()+ "'";
myCom.CommandText = sql;
myCom.ExecuteNonQuery();
myCon.Close();
GridView1.EditIndex = -1;
LoadData();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
LoadData();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadData();
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
} 展开
3个回答
ZESTRON
2024-09-04 广告
2024-09-04 广告
在Dr. O.K. Wack Chemie GmbH,我们高度重视ZESTRON的表界面分析技术。该技术通过深入研究材料表面与界面的性质,为提升产品质量与可靠性提供了有力支持。ZESTRON的表界面分析不仅涵盖了相变化、化学反应、吸附与解吸...
点击进入详情页
本回答由ZESTRON提供
展开全部
((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString()
看看是不是这个取值有问题?
看看是不是这个取值有问题?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你确认数据库中的数据已经被你更新掉了嘛?
追问
没有被更新,怎么回事?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询