求救:向GridView中添加一行数据,其他的数据全部跟着更改,高手帮忙!谢谢
namespace MeApp
{
public partial class editwe_2 : System.Web.UI.Page
{
dataOperate mydo = new dataOperate();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
public void Bind()
{
string strCon = ConfigurationManager.AppSettings["Constr"].ToString();
string sql = "select * from WeatherInfo ";
SqlConnection con = new SqlConnection(strCon);
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
con.Open();
da.Fill(ds);
gvweather.DataSource = ds;
gvweather.DataKeyNames = new string[] { "weinfo_id" };
gvweather.DataBind();
con.Close();
}
protected void gvweather_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvweather.PageIndex = e.NewPageIndex; //设置当前页的索引
Bind(); //重新绑定GridView控件
}
protected void gvweather_RowEditing(object sender, GridViewEditEventArgs e)
{
gvweather.EditIndex = e.NewEditIndex;
Bind();
}
protected void gvweather_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string id = gvweather.DataKeys[e.RowIndex].Value.ToString();
string date = ((TextBox)(gvweather.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
string time = ((TextBox)(gvweather.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
string area = ((TextBox)(gvweather.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
string weather = ((TextBox)(gvweather.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim();
string tem = ((TextBox)(gvweather.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim();
string hum = ((TextBox)(gvweather.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString().Trim();
string air = ((TextBox)(gvweather.Rows[e.RowIndex].Cells[7].Controls[0])).Text.ToString().Trim();
string sql = "update WeatherInfo set weinfo_date='" + date + "',weather_time='" + time +
"',area='" + area + "',weather='" + weather + "',temperature='" + tem + "',huminity='" + hum +
"',airQuality='" + air + "'";
try
{
mydo.updateData(sql);
gvweather.EditIndex = -1;
}
catch
{
Response.Write("<Script language=javascript>alert('修改失败!');</script>");
}
}
protected void gvweather_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id = gvweather.DataKeys[e.RowIndex].Value.ToString();
string sql = "delete from WeatherInfo where weinfo_id='" + id + "'";
mydo.adlData(sql); //将此条图书信息删除
Bind(); //调用自定义方法重新绑定图书信息
}
protected void gvweather_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvweather.EditIndex = -1;
Bind();
}
}
}
[解决办法]
那就得看你的 sql 具体内容了,自己看看,是不是条件范围太宽
或者说 RowUpdating 被循环执行了,每一条数据都被更新了
断点调试吧
[解决办法]
同意楼上,update语句连where条件都没有。。。