Reputation: 721
I'm using bulk update to update my gridview.
The user is first prompted to select dates and then click a button to display the gridview data for those dates. Update the relevant cells and then click on save.
The thing is my Gridview disappears after it gets updated. I want the Gridview to remain displaying the updated data. How do I do that?
Code Snippets:
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;
}
Upvotes: 0
Views: 2688
Reputation: 326
I have Already used below code . I will work .you should call again that Method in which you are displaying Grid view by date
public void DisplyGridview(){
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 GridView1_RowUpdating(object sender, GridViewUpdateEventArgs 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;
}
DisplyGridview();
}
Upvotes: 0
Reputation: 148130
You have to assign datatable/DataSet to data source before bind method is called
GridView1.DataSource = dt;
GridView1.DataBind();
Here you are binding grid after update but not assigning DataTable to GridView1.DataSource. You can call the DataGrid method here instead of binding like this.
private void FillGrid()
{
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;
}
}
}
//You should as call the method FillGrid that loads data and bind grid previously
cmd.ExecuteNonQuery();
GridView1.EditIndex = -1;
//GridView1.DataBind();
FillGrid();
GridView1.Visible = true;
Remove these statement and call method which bind the grid previously
Upvotes: 1