Hi,

I have load data method as follows:

    public List<SalesOrderHeader> Load_SaleOrderRelatedInfo()
            {
                try
                {
                    using (var context = new AdventureWorksEntities())
                    {
                        return context.SalesOrderHeaders.Include("SalesOrderDetails").OrderBy(so => so.SalesOrderNumber).ToList();
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

and method for data binding:

public static DataTable Binding_SaleOrderInfoRelated()
        {
            using (var services = new ServiceTransferClient())
            {
                var table = new DataTable();
                SalesOrderHeader[] orders = services.Load_SaleOrderRelatedInfo();


            table.Columns.Add("SalesOrderID", typeof(int));
            table.Columns.Add("RevisionNumber", typeof(int));
            table.Columns.Add("OrderDate", typeof(DateTime));
            table.Columns.Add("DueDate", typeof(DateTime));
            table.Columns.Add("ShipDate", typeof(DateTime));
            table.Columns.Add("Status", typeof(int));
            table.Columns.Add("OnlineOrderFlag", typeof(int));
            table.Columns.Add("SalesOrderNumber", typeof(string));
            table.Columns.Add("PurchaseOrderNumber", typeof(string));
            table.Columns.Add("AccountNumber", typeof(string));
            table.Columns.Add("CustomerID", typeof(int));
            table.Columns.Add("ContactID", typeof(int));
            table.Columns.Add("SalesPersonID", typeof(int));
            table.Columns.Add("TerritoryID", typeof(int));
            table.Columns.Add("BillToAddressID", typeof(int));
            table.Columns.Add("ShipToAddressID", typeof(int));
            table.Columns.Add("ShipMethodID", typeof(int));
            table.Columns.Add("CreditCardID", typeof(int));
            table.Columns.Add("CreditCardApprovalCode", typeof(string));
            table.Columns.Add("CurrencyRateID", typeof(int));
            table.Columns.Add("SubTotal", typeof(decimal));
            table.Columns.Add("TaxAmt", typeof(decimal));
            table.Columns.Add("Freight", typeof(decimal));
            table.Columns.Add("TotalDue", typeof(decimal));
            table.Columns.Add("Comment", typeof(string));

            table.Columns.Add("SalesOrderDetailID", typeof(int));
            table.Columns.Add("CarrierTrackingNumber", typeof(string));
            table.Columns.Add("OrderQty", typeof(int));
            table.Columns.Add("ProductID", typeof(int));
            table.Columns.Add("SpecialOfferID", typeof(int));
            table.Columns.Add("UnitPrice", typeof(decimal));
            table.Columns.Add("UnitPriceDiscount", typeof(decimal));
            table.Columns.Add("LineTotal", typeof(double));

            foreach (var o in orders)
            {
                var rows = table.NewRow();

                rows["SalesOrderID"] = o.SalesOrderID;
                rows["RevisionNumber"] = o.RevisionNumber;
                rows["OrderDate"] = o.OrderDate;
                rows["DueDate"] = o.DueDate;
                rows["ShipDate"] = o.ShipDate;
                rows["Status"] = o.Status;
                rows["OnlineOrderFlag"] = o.OnlineOrderFlag;
                rows["SalesOrderNumber"] = o.SalesOrderNumber;
                rows["PurchaseOrderNumber"] = o.PurchaseOrderNumber;
                rows["AccountNumber"] = o.AccountNumber;
                rows["CustomerID"] = o.CustomerID;
                rows["ContactID"] = o.ContactID;
                rows["SalesPersonID"] = o.SalesPersonID.GetValueOrDefault();
                rows["TerritoryID"] = o.TerritoryID;
                rows["BillToAddressID"] = o.BillToAddressID;
                rows["ShipToAddressID"] = o.ShipToAddressID;
                rows["ShipMethodID"] = o.ShipMethodID;
                rows["CreditCardID"] = o.CreditCardID.GetValueOrDefault();
                rows["CreditCardApprovalCode"] = o.CreditCardApprovalCode;
                rows["CurrencyRateID"] = o.CurrencyRateID.GetValueOrDefault();
                rows["SubTotal"] = o.SubTotal;
                rows["TaxAmt"] = o.TaxAmt;
                rows["Freight"] = o.Freight;
                rows["TotalDue"] = o.TotalDue;
                rows["Comment"] = o.Comment;

                table.Rows.Add(rows);

                foreach (var od in o.SalesOrderDetails)
                {
                    var rows1 = table.NewRow();
                    rows1["SalesOrderID"] = o.SalesOrderID;
                    rows1["SalesOrderDetailID"] = od.SalesOrderDetailID;
                    rows1["CarrierTrackingNumber"] = od.CarrierTrackingNumber;
                    rows1["OrderQty"] = od.OrderQty;
                    rows1["ProductID"] = od.ProductID;
                    rows1["SpecialOfferID"] = od.SpecialOfferID;
                    rows1["UnitPrice"] = od.UnitPrice;
                    rows1["UnitPriceDiscount"] = od.UnitPriceDiscount;
                    rows1["LineTotal"] = od.LineTotal;

                    table.Rows.Add(rows1);
                }
            }

            return table;
        }
    }

because the amount of data so large that when executed method take times. To reduce the waiting and anxious, can use the progress bar, progress bar is finished when the data is loaded.

How can I do?

THANKS!

Set visibility = true before the function Binding_SaleOrderInfoRelated
Set thr progress bar's max property to orders.Count

increment the progress bar's value property by 1 at the end of each iteration

foreach (var o in orders)
{
    .
    .
    .
    progressBar.Value++;
}

Set visibility = false after the function Binding_SaleOrderInfoRelated

Hope it works

Hi samsylvestertty

I do not know how to do? you can specify more clearly. Give me a sample code?

Thanks!

It is worth noting if you are not running a multi-threaded application the load bar will not update normally as the form will 'freeze' until the whole thread has run, ie. bar will jump from 0% to 100%.

Hi MikeyIsMe,

Can you give me a sample to do that in my case to understand more?

Thanks

public static DataTable Binding_SaleOrderInfoRelated()
        {
            using (var services = new ServiceTransferClient())
            {
                var table = new DataTable();
                SalesOrderHeader[] orders = services.Load_SaleOrderRelatedInfo();


            table.Columns.Add("SalesOrderID", typeof(int));
            table.Columns.Add("RevisionNumber", typeof(int));
            table.Columns.Add("OrderDate", typeof(DateTime));
            table.Columns.Add("DueDate", typeof(DateTime));
            table.Columns.Add("ShipDate", typeof(DateTime));
            table.Columns.Add("Status", typeof(int));
            table.Columns.Add("OnlineOrderFlag", typeof(int));
            table.Columns.Add("SalesOrderNumber", typeof(string));
            table.Columns.Add("PurchaseOrderNumber", typeof(string));
            table.Columns.Add("AccountNumber", typeof(string));
            table.Columns.Add("CustomerID", typeof(int));
            table.Columns.Add("ContactID", typeof(int));
            table.Columns.Add("SalesPersonID", typeof(int));
            table.Columns.Add("TerritoryID", typeof(int));
            table.Columns.Add("BillToAddressID", typeof(int));
            table.Columns.Add("ShipToAddressID", typeof(int));
            table.Columns.Add("ShipMethodID", typeof(int));
            table.Columns.Add("CreditCardID", typeof(int));
            table.Columns.Add("CreditCardApprovalCode", typeof(string));
            table.Columns.Add("CurrencyRateID", typeof(int));
            table.Columns.Add("SubTotal", typeof(decimal));
            table.Columns.Add("TaxAmt", typeof(decimal));
            table.Columns.Add("Freight", typeof(decimal));
            table.Columns.Add("TotalDue", typeof(decimal));
            table.Columns.Add("Comment", typeof(string));

            table.Columns.Add("SalesOrderDetailID", typeof(int));
            table.Columns.Add("CarrierTrackingNumber", typeof(string));
            table.Columns.Add("OrderQty", typeof(int));
            table.Columns.Add("ProductID", typeof(int));
            table.Columns.Add("SpecialOfferID", typeof(int));
            table.Columns.Add("UnitPrice", typeof(decimal));
            table.Columns.Add("UnitPriceDiscount", typeof(decimal));
            table.Columns.Add("LineTotal", typeof(double));
            //I assume you have a progress bar control named progressBar1
            //Set the progress bar's maximum attribute to the count
            progressBar1.Maximum = orders.Count; 
            //Show the progress bar
            progressBar1.Visible = true;
            foreach (var o in orders)
            {
                var rows = table.NewRow();

                rows["SalesOrderID"] = o.SalesOrderID;
                rows["RevisionNumber"] = o.RevisionNumber;
                rows["OrderDate"] = o.OrderDate;
                rows["DueDate"] = o.DueDate;
                rows["ShipDate"] = o.ShipDate;
                rows["Status"] = o.Status;
                rows["OnlineOrderFlag"] = o.OnlineOrderFlag;
                rows["SalesOrderNumber"] = o.SalesOrderNumber;
                rows["PurchaseOrderNumber"] = o.PurchaseOrderNumber;
                rows["AccountNumber"] = o.AccountNumber;
                rows["CustomerID"] = o.CustomerID;
                rows["ContactID"] = o.ContactID;
                rows["SalesPersonID"] = o.SalesPersonID.GetValueOrDefault();
                rows["TerritoryID"] = o.TerritoryID;
                rows["BillToAddressID"] = o.BillToAddressID;
                rows["ShipToAddressID"] = o.ShipToAddressID;
                rows["ShipMethodID"] = o.ShipMethodID;
                rows["CreditCardID"] = o.CreditCardID.GetValueOrDefault();
                rows["CreditCardApprovalCode"] = o.CreditCardApprovalCode;
                rows["CurrencyRateID"] = o.CurrencyRateID.GetValueOrDefault();
                rows["SubTotal"] = o.SubTotal;
                rows["TaxAmt"] = o.TaxAmt;
                rows["Freight"] = o.Freight;
                rows["TotalDue"] = o.TotalDue;
                rows["Comment"] = o.Comment;

                table.Rows.Add(rows);

                foreach (var od in o.SalesOrderDetails)
                {
                    var rows1 = table.NewRow();
                    rows1["SalesOrderID"] = o.SalesOrderID;
                    rows1["SalesOrderDetailID"] = od.SalesOrderDetailID;
                    rows1["CarrierTrackingNumber"] = od.CarrierTrackingNumber;
                    rows1["OrderQty"] = od.OrderQty;
                    rows1["ProductID"] = od.ProductID;
                    rows1["SpecialOfferID"] = od.SpecialOfferID;
                    rows1["UnitPrice"] = od.UnitPrice;
                    rows1["UnitPriceDiscount"] = od.UnitPriceDiscount;
                    rows1["LineTotal"] = od.LineTotal;

                    table.Rows.Add(rows1);
                }
                //Increment the progress bar value
                progressBar1.Value++;
            }
            //Hide the progress bar
            progressBar1.Visible = false;
            return table;
        }
    }

As MikeyIsMe said there might be a problem. I tried it at my side and I got the progress bar functioning properly (however the main screen freezes). You try the code and tell if any problem arises then we'll try for a seperate thread to run a progress bar.

Edited 4 Years Ago by samsylvestertty

Hi samsylvestertty,

I follow your instructions but the results are not as expected.

When my program is processing, the progress bar does not run the wait process.

Thanks!

I think it can also paging on datagrid view, instead of load all data (Program runs slower if the data is too large)

This question has already been answered. Start a new discussion instead.