Click here to Skip to main content
15,892,269 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a method called Search(), its task is to retrieve many purchase orders and show them in a datagridview as a BindingList but adding an additional column manually. The problem is that everything works correctly when I physically press the search button however when I call the method on the form load event it will work except that the extra column that I added manually does not show its cell values, it will only show them after I click the button.

C#
private void Search()
        {
            dgvResults.DataSource = null;
            List<PurchaseOrder> pos = new List<PurchaseOrder>();

            // Check if search criteria is valid
            bool searchCriteriaOk = false;
            if (rdoById.Checked && txtSearch.Text.Length == 8)
            {
                pos = service.GetPurchaseOrders(1, LoginData.EmployeeId, Convert.ToInt32(txtSearch.Text), DateTime.Today, DateTime.Today, null);
                searchCriteriaOk = true;
            }
            else if (rdoById.Checked && txtSearch.Text.Length != 8)
            {
                MessageBox.Show("The purchase order id must be 8 digits long");
            }
            else if (rdoByDate.Checked && dtpStart.Value < dtpEnd.Value)
            {
                POStatus? status = null;
                if (rdoPending.Checked)
                {
                    status = POStatus.Pending;
                }
                if (rdoClosed.Checked)
                {
                    status = POStatus.Closed;
                }
                pos = service.GetPurchaseOrders(2, LoginData.EmployeeId, 0, dtpStart.Value, dtpEnd.Value, status);
                searchCriteriaOk = true;
            }
            else if (rdoByDate.Checked && !(dtpStart.Value < dtpEnd.Value))
            {
                MessageBox.Show("Start date must be less than end date");
            }

            if (searchCriteriaOk)
            {
                if (pos.Count > 0)
                {
                    if (LoginData.EmployeeType == EmployeeType.RegularEmployee || LoginData.EmployeeType == EmployeeType.HREmployee)
                    {
                        foreach (var po in pos.ToList())
                        {
                            if (po.EmployeeId != LoginData.EmployeeId)
                            {
                                pos.Remove(po);
                            }
                        }
                    }

                    dgvResults.DataSource = new BindingList<PurchaseOrder>(pos);
                    dgvResults.Columns["TotalAfterTax"].DefaultCellStyle.Format = "c2";
                    foreach (DataGridViewColumn column in dgvResults.Columns)
                    {
                        if (column.Name != "PurchaseOrderId" && column.Name != "CreationDate" && column.Name != "TotalAfterTax")
                        {
                            column.Visible = false;
                        }

                        if (column.Name == "PurchaseOrderId")
                        {
                            column.HeaderText = "PO Number";
                        }

                        if (column.Name == "CreationDate")
                        {
                            column.HeaderText = "Date";
                        }

                        if (column.Name == "TotalAfterTax")
                        {
                            column.HeaderText = "Total";
                        }
                    }

                    if (dgvResults.Columns["PoItems"] != null)
                    {
                        dgvResults.Columns.Remove("PoItems");
                    }

                    dgvResults.Columns.Add("PoItems", "Items");
                    foreach (DataGridViewRow row in dgvResults.Rows)
                    {
                        string itemToAddToDgv = "";
                        int poId = Convert.ToInt32(row.Cells["PurchaseOrderId"].Value);
                        foreach (PurchaseOrder purchaseOrder in pos.ToList())
                        {
                            if (purchaseOrder.PurchaseOrderId == poId)
                            {
                                foreach (Item item in purchaseOrder.Items.ToList())
                                {
                                    itemToAddToDgv += " *" + item.Name;
                                }
                            }
                        }
                        row.Cells["PoItems"].Value = itemToAddToDgv;
                    }

                    dgvResults.Rows[0].Selected = true;
                }
                else
                {
                    MessageBox.Show("No Purchase Orders found");
                }
            }
        }


What I have tried:

I have even tried to call the .PerformClick() method on the form load but it won't work
Posted
Updated 8-Jun-20 10:02am
Comments
Maciej Los 8-Jun-20 3:07am    
Have you tried to debug your programme to find out why Search procedure does not fire-up?
Richard MacCutchan 8-Jun-20 4:36am    
You should add the various fields to your binding source before you bind it to the view.

1 solution

I solved this by adding a new property to the model instead of adding the column after binding the list
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900