如何清除GridView中的数据?

时间:2016-01-28 02:52:18

标签: c# asp.net gridview

我在Gridview中清除数据时遇到问题。我遵循了关于这个问题的其他问题中给出的所有示例,但我的Gridview并不清楚。当我第二次搜索时,它只会添加到当前的Gridview数据。我错误地使用了这种方法吗?

例如,我已经通过用按钮搜索用户来填充我的GridView。它将在gridview中添加6个数据。当我想搜索另一个用户时,不是先清除数据并添加,而是叠加当前gridview中的当前数据,总共给出了12个数据。

public partial class Update_User_Schedule : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[5] { new DataColumn("Test Name"), new DataColumn("Duration"), new DataColumn("Start Date"), new DataColumn("End Date"), new DataColumn("Limit") });
            ViewState["Patient"] = dt;
            this.BindGrid();
        }
    }

    protected void BindGrid()
    {
        GridView1.DataSource = (DataTable)ViewState["Patient"];
        GridView1.DataBind();
    }

    protected void btnUser_Click(object sender, EventArgs e)
    {
        GridView1.DataSource = null;
        GridView1.DataBind();

        int User_ID = 0, TS_IDstr = 0, Test_ID = 0, Duration = 0, Limit = 0;
        string Test_Name = "";
        string Name = cmbName.Text;
        string start = "", end = "";

        List<int> TS_ID = new List<int>();

        SqlConnection myConn = new SqlConnection("Data Source=localhost;" + "Initial Catalog=IBBTS_DB; Integrated Security =SSPI");
        SqlCommand retrieveUser_IDCmd = new SqlCommand("SELECT User_ID FROM Patient where Name ='" + Name + "' ;", myConn);
        myConn.Open();
        SqlDataReader reader = retrieveUser_IDCmd.ExecuteReader();
        while (reader.Read())
        {
            User_ID = (Convert.ToInt32(reader.GetValue(0).ToString()));
        }
        reader.Close();

        SqlCommand retrieveTS_IDCmd = new SqlCommand("SELECT TS_ID FROM testSet where User_ID ='" + User_ID + "' ;", myConn);
        SqlDataReader reader2 = retrieveTS_IDCmd.ExecuteReader();
        while (reader2.Read())
        {
            TS_ID.Add(Convert.ToInt32(reader2.GetValue(0).ToString()));
        }
        reader2.Close();

        foreach (int element in TS_ID)
        {
            TS_IDstr = element;

            SqlCommand retrieveTest_IDCmd = new SqlCommand("SELECT Test_ID FROM testSet where TS_ID ='" + TS_IDstr + "' ;", myConn);
            SqlDataReader reader3 = retrieveTest_IDCmd.ExecuteReader();
            while (reader3.Read())
            {
                Test_ID = (Convert.ToInt32(reader3.GetValue(0).ToString()));
            }
            reader3.Close();

            SqlCommand retrieveTest_NameCmd = new SqlCommand("SELECT Test_Name FROM Test where Test_ID ='" + Test_ID + "' ;", myConn);
            SqlDataReader reader4 = retrieveTest_NameCmd.ExecuteReader();
            while (reader4.Read())
            {
                Test_Name = (reader4.GetValue(0).ToString());
            }
            reader4.Close();

            SqlCommand retrieveDurationCmd = new SqlCommand("SELECT Duration FROM testSet where TS_ID ='" + TS_IDstr + "' ;", myConn);
            SqlDataReader reader5 = retrieveDurationCmd.ExecuteReader();
            while (reader5.Read())
            {
                Duration = (Convert.ToInt32(reader5.GetValue(0).ToString()));
            }
            reader5.Close();

            SqlCommand retrieveStart_DateCmd = new SqlCommand("SELECT startDate FROM testSet where TS_ID ='" + TS_IDstr + "' ;", myConn);
            SqlDataReader reader6 = retrieveStart_DateCmd.ExecuteReader();
            while (reader6.Read())
            {
                start = (reader6.GetValue(0).ToString());
            }
            reader6.Close();

            SqlCommand retrieveEnd_DateCmd = new SqlCommand("SELECT endDate FROM testSet where TS_ID ='" + TS_IDstr + "' ;", myConn);
            SqlDataReader reader7 = retrieveEnd_DateCmd.ExecuteReader();
            while (reader7.Read())
            {
                end = (reader7.GetValue(0).ToString());
            }
            reader7.Close();

            SqlCommand retrieveLimitCmd = new SqlCommand("SELECT Limit FROM testSet where TS_ID ='" + TS_IDstr + "' ;", myConn);
            SqlDataReader reader8 = retrieveLimitCmd.ExecuteReader();
            while (reader8.Read())
            {
                Limit = (Convert.ToInt32(reader8.GetValue(0).ToString()));
            }
            reader8.Close();


            DataTable dt = (DataTable)ViewState["Patient"];
            dt.Rows.Add(Test_Name, Duration,start, end, Limit);
            ViewState["Patient"] = dt;
            this.BindGrid();
        }
    }
}

0 个答案:

没有答案