How do I clear data in my GridView?
<p>I have a problem regarding clearing data in my Gridview. I followed all the example that was given in other questions regarding this problem but my Gridview is not being clear. When I search the second time, it will just add on to the current Gridview data. Am I using the method wrongly?</p>

<p>For example, I've already filled up my GridView by searching user with a button. It will add 6 data into the gridview. when I want to search another user, instead of clearing the data first and adding on, it will just stack over the current data in the current gridview giving me 12 data in total.</p>

<pre><code>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&lt;int&gt; TS_ID = new List&lt;int&gt;();

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