我正在使用批量更新来更新我的网格视图。
首先提示用户选择日期,然后单击按钮以显示这些日期的 gridview 数据。更新相关单元格,然后单击保存。
问题是我的 Gridview 在更新后消失了。我希望 Gridview 继续显示更新的数据。我怎么做?
代码片段:
string connStr = ConfigurationManager.ConnectionStrings["bbsConnectionString"].ConnectionString;
using (SqlConnection Con = new SqlConnection(connStr))
{
SqlDataAdapter sdr = new SqlDataAdapter(str, Con);
sdr.SelectCommand.Parameters.AddWithValue("@startdate", startdate);
sdr.SelectCommand.Parameters.AddWithValue("@enddate", enddate);
DataTable dt = new DataTable();
sdr.Fill(dt);
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
Button2.Visible = true;
}
}
protected void Button2_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
TextBox dtm = row.FindControl("DtmTextBox") as TextBox;
string connStr = ConfigurationManager.ConnectionStrings["bbsConnectionString"].ConnectionString;
using (SqlConnection Con = new SqlConnection(connStr))
{
Con.Open();
SqlCommand cmd = new SqlCommand("Update tblAvailable set intqty=@intQty, curprice=@curprice where intresortid=@intresortid and dtm=@dtm and strroomtype=@strroomtype", Con);
// cmd.Parameters.AddWithValue("@dtm", DateTime.ParseExact(dtm.Text.Trim(), "dd/M/yyyy", System.Globalization.CultureInfo.InvariantCulture));
cmd.ExecuteNonQuery();
GridView1.EditIndex = -1;
GridView1.DataBind();
GridView1.Visible = true;
}