简体   繁体   中英

C# DataGridView Removing Data Rows

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);
                    }
                }
            }
        }
    }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM