0

This may be quite a common asked question with regards to the search results, however, I haven't yet found the answer to my problem.

I am loading 3 different types of DataGridView's in 3 different Pages (Window forms), and in each and every single one of them, previously viewed data rows are added onto it (the newly opened DataGridView).

Does anyone know of a way I can prevent this from happening?

 private void Emp_Awaiting_Dispatch_Orders_Load(object sender, EventArgs e)
    {
        foreach (TBL_Order ord in DataLOrders.Get_Status_AwaitingDispatchOrder())
        {
            ViewAwaitDipOrd_DGV.Rows.Add(ord.OrderID, ord.CustomerID, ord.ProductID, ord.Status, ord.Quantity, "Switch to Dispatched");
        }
    }
    private void ViewAwaitDipOrd_DGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if (ViewAwaitDipOrd_DGV.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
        {
            if (e.ColumnIndex == 5)
            {
                BusinessLOrders.OrderDispatched();

                foreach (TBL_Order ord in DataLOrders.Get_Status_AwaitingDispatchOrder())
                {                 
                    Int32.TryParse(ViewAwaitDipOrd_DGV.Rows[e.RowIndex].Cells[0].FormattedValue.ToString(), out cellID);

                    if (ord.ProductID == cellID)
                    {
                        Int32.TryParse(ViewAwaitDipOrd_DGV.Rows[e.RowIndex].Cells[0].FormattedValue.ToString(), out cellID);
                    }
                }
            }
        }
    }
1
  • Add a bool column - which can be invisible - to the datasource and use a RowFilter to filter them out. Commented Jan 1, 2018 at 23:38

1 Answer 1

1

you can check for row count

dataGridView.Rows.Count

and if there are existing rows you can clear them

dataGridView.Rows.Clear();

Make sure you add header row first if it is not automatically added by source. Hope it helps

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.