I have a "Clear" button which sets the gridview datasource to null and binds it. However, when I add a new entry to it, the old rows seem to show up again (in addition to the new row)...not sure what I am doing wrong....
gv.DataSource = null;
gv.DataBind();
Shouldn't the above clear out the state of the gridview? TIA
protected void btnAddFactor_Click(object sender, EventArgs e)
{
if (txtfactor.Text != "")
{
if (ViewState["factor"] == null)
{
DataTable dtbl = new DataTable();
dtbl.Columns.Add("Factor");
dtbl.Columns.Add("Weight");
DataRow dr = dtbl.NewRow();
dr[0] = txtfactor.Text.Trim();
dr[1] = Convert.ToInt32(ddlWeight.SelectedValue);
dtbl.Rows.Add(dr);
ViewState["factor"] = dtbl;
}
else
{
DataTable dtbl = (DataTable)(ViewState["factor"]);
DataRow dr = dtbl.NewRow();
dr[0] = txtfactor.Text.Trim();
dr[1] = Convert.ToInt32(ddlWeight.SelectedValue);
dtbl.Rows.Add(dr);
ViewState["factor"] = dtbl;
}
gvFactor.DataSource = (DataTable)(ViewState["factor"]);
gvFactor.DataBind();
//gvFactor.Rows.
}
}
protected void btnClearFactor_Click(object sender, EventArgs e)
{
gvFactor.DataSource = null;
gvFactor.DataBind();
}