Datagrid won't clear

203 views Asked by At

The datagrid I have made won't let me clear the values. When I attempt to do a datagrid.columns.clear(); then it scrunches the rows together and doesn't display anything more than lines and it still doesn't clear.

I have tried datagrid.items.clear(); as I have used Datagrid.items.add(); to add items to my datagrid. I have tried datagrid.items.refresh(); and setting the datacontext to null. None of this is working.

public  void FillDataGridWithCustInfo()
        {



            CustomerDataGrid.Items.Clear();

            CustomerDataGrid.ItemsSource = null;

            CustomerDataGrid.Items.Refresh();


            int ArrayNum = 0;

            Int32 length = CustomerFirstName.Count;

            while (length > ArrayNum)
            {




                CustomerInformation TempCust = new CustomerInformation();
                TempCust.FirstName = CustomerFirstName[ArrayNum];
                TempCust.MiddleInital = CustomerMiddleInitial[ArrayNum];
                TempCust.LastName = CustomerLastName[ArrayNum];
                TempCust.TaxClass = CustomerTaxClass[ArrayNum];
                TempCust.Email = CustomerEmail[ArrayNum];

                CustomerDataGrid.Items.Add(TempCust);




                ArrayNum = ArrayNum + 1;

            }





        }

I want it to clear the datagrid before filling it when I close out of this particular window, but it is currently taking all the data in the datagrid and just adding to it so I am getting duplicates of all the information.

1

There are 1 answers

1
HO LI Pin On

I am not sure is datagrid was use by desktop application , in web application we have a similar object call as gridview which show data as table format. If you want to force clear all row , you can assign an empty or dummy data table , something like below shall do the trick..

     GridView1.DataSource = new DataTable();
     GridView1.DataBind();

If you want to maintain proper column header and just want to remove row that contain data , make sure you proper structure your datatable..something like this.

        DataTable dt = new DataTable();
        dt.Columns.Add("Column1", Type.GetType("System.String"));
        dt.Columns.Add("Column2", Type.GetType("System.String"));
        GridView1.DataSource = dt;
        GridView1.DataBind();