Gridview在更新后显示

时间:2012-05-18 04:28:12

标签: c# asp.net gridview web-controls

我正在使用批量更新来更新我的gridview。

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




            }

2 个答案:

答案 0 :(得分:1)

在调用bind方法之前,必须将datatable / DataSet分配给数据源

 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;

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

答案 1 :(得分: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;
          }



    }

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

}