You must Sign In to post a response.
  • Category: ASP.NET

    How to clear the data table value in asp.net using c#

    how to clear the datatable value.

    in run mode as follows

    Major code AMC (Dropdownlist1) Minor code AFF(Dropdownlist2) Button(Add)
    CTF

    When i click Add button the AMC and AFF will be displayed in the gridview.

    In gridview as follows

    Major code Minor code
    AMC AFF

    Add button code as follows

    protected void btnadd_Click(object sender, EventArgs e)
    {
    if (Session["MyTable"] == null)
    {
    dt1 = new DataTable("MyTable");
    dt1.Columns.Add("Major Code", typeof(string));
    dt1.Columns.Add("Minor Code", typeof(string));
    }
    else
    {
    dt1 = (DataTable)Session["MyTable"];
    }
    DataRow dt_row;
    dt_row = dt1.NewRow();
    dt_row["Major Code"] = ddlmjrcode.SelectedItem.ToString();
    dt_row["Minor Code"] = ddlminorcode.SelectedItem.ToString();
    dt1.Rows.Add(dt_row);
    Session["MyTable"] = dt1;
    gvmajormaster.DataSource = (DataTable)Session["MyTable"];
    gvmajormaster.DataBind();
    }

    I have one clear button.clear button code as follows.

    protected void btnClear_Click(object sender, EventArgs e)
    {
    gvmajormaster.DataSource = null;
    gvmajormaster.DataBind();
    }

    When i click the clear button. gridview rows will be empty.

    similarily When i click Add button the AMC and CTF will be displayed in the gridview.

    In gridview as follows
    Major code Minor code
    AMC AFF
    AMC CTF

    But previouse row AMC and AFF is displayed in the gridview.
    i dont want the AMC and AFF in gridview
    when i click the add button only AMC and CTF only to be displayed in the gridview.
  • #762241
    Hi

    You can try this following code for Datatable Clear in c#


    //Step 1
    DataTable dt = new DataTable();

    //Step 2
    dt = null;

    //Step 3
    dt = new DataTable();

    //Step 4
    dt.Clear();

    //Step 4
    dt.Rows.Clear();

    DataTable dt = new DataTable();
    // Remove all
    while(dt.Count > 0)
    {
    dt.Rows[0].Delete();
    }


    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #762256
    Hello Rao,

    DataTable dt = new DataTable();
    dt.Clear(); // Clears the DataTable of all data.

    dt.Reset(); // Resets the DataTable to its original state. Such as data, indexes, columns, rows, relations,

    dt = null; // Set null value to datatable


    Hope this will work for you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #762258
    Hi rao,

    I guess you want to clear your table rows only not columns right..?

    In that case you can clear the rows by using below sample


    dt.Rows.Clear();


    this will clear the all the rows.

    Hope this will helpful to you.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #762309
    Hi

    To clear data table use below code
    protected void btnClear_Click(object sender,
    EventArgs e)
    {
    DataTable dtTable=new DataTable();
    dtTable.Clear();
    GridView1.DataSource = dtTable;
    Gridview1.DataBind();
    }

    Regards

    Sridhar Thota.

    Sridhar Thota.
    Editor: DNS Forum.


Sign In to post your comments