0

我正在使用批量更新来更新我的网格视图。

首先提示用户选择日期,然后单击按钮以显示这些日期的 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;




            }
4

2 回答 2

1

您必须在调用绑定方法之前将数据表/数据集分配给数据源

 GridView1.DataSource = dt;
 GridView1.DataBind();

在这里,您在更新后绑定网格,但未将 DataTable 分配给 GridView1.DataSource。您可以在此处调用 DataGrid 方法,而不是像这样绑定。

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;

删除这些语句并调用之前绑定网格的方法

于 2012-05-18T04:35:13.687 回答
0

我已经使用了下面的代码。我会工作的。您应该再次调用按日期显示网格视图的方法

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;
          }



    }

} 受保护的无效 GridView1_RowUpdating(对象发件人,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();

}

于 2012-05-18T04:52:40.060 回答