程序重新启动后才会刷新数据

时间:2018-09-24 12:32:51

标签: c# winforms listview

我有一个类,我正在实现属性更改事件,但由于某种原因,我的列表视图将不会更新数据,除非我关闭该应用程序并重新启动它。

public BindingList<SalesOrders> GetSalesOrders()
{
    BindingList<SalesOrders> _salesOrdersList = new BindingList<SalesOrders>();

    try
    {


        string sageDsn = ConfigurationManager.AppSettings["SageDSN"];
        string sageUsername = ConfigurationManager.AppSettings["SageUsername"];
        string sagePassword = ConfigurationManager.AppSettings["SagePassword"];

        //using (var connection = new OdbcConnection("DSN=SageLine50v24;Uid=Manager;Pwd=;"))
        using (var connection = new OdbcConnection(String.Format("DSN={0};Uid={1};Pwd={2};", sageDsn, sageUsername, sagePassword)))
        {

            connection.Open();

            //string sql = string.Format(getInvoiceSql, customerCode, DateTime.Today.AddMonths(-1).ToString("yyyy-MM-dd"));
            string fromD = dtpFrom.Value.ToString("yyyy-MM-dd");
            string toD = dtpTo.Value.ToString("yyyy-MM-dd");

            String SQL = string.Format("SELECT 'ORDER_NUMBER', 'ORDER_OR_QUOTE', 'ANALYSIS_1','ACCOUNT_REF','ORDER_DATE','NAME', 'COURIER_NUMBER','COURIER_NAME','CUST_TEL_NUMBER' ,'DESPATCH_DATE','ACCOUNT_REF',  'DEL_NAME', 'DEL_ADDRESS_1', 'DEL_ADDRESS_2', 'DEL_ADDRESS_3', 'DEL_ADDRESS_4', 'DEL_ADDRESS_5',  'INVOICE_NUMBER','INVOICE_NUMBER_NUMERIC', 'CONTACT_NAME','CONSIGNMENT', 'NOTES_1', 'ITEMS_NET' ,'ITEMS_GROSS','QUOTE_STATUS' FROM SALES_ORDER WHERE ORDER_DATE >='{0}' and ORDER_DATE <='{1}'", fromD, toD);
            using (var command = new OdbcCommand(SQL, connection))
            {
                backgroundWorker1.ReportProgress(15);

                using (var reader = command.ExecuteReader())



                {

                    while (reader.Read())
                    {
                        backgroundWorker1.ReportProgress(35);

                        counter++;

                        var salesOrders = new SalesOrders();
                        if ((reader["ORDER_NUMBER"] != ""))
                        {
                            string orderNumber = Convert.ToString(reader["ORDER_NUMBER"]);
                            salesOrders.ACCOUNT_REF = Convert.ToString(reader["ACCOUNT_REF"]);
                            salesOrders.RecordIdentifier = "SHN";
                            salesOrders.ShipmmentId = Convert.ToString(reader["ORDER_NUMBER"]);
                            salesOrders.OrderDate = Convert.ToDateTime(reader["ORDER_DATE"]);
                            salesOrders.OrderNumber = Convert.ToString(reader["ORDER_NUMBER"]);

                            salesOrders.Company = "hackett";
                            salesOrders.Carrier = Convert.ToString(reader["COURIER_NUMBER"]);
                            salesOrders.CarrierService = Convert.ToString(reader["COURIER_NAME"]);
                            salesOrders.CustomerName = Convert.ToString(reader["NAME"]);
                            salesOrders.ShipToAddress1 = Convert.ToString(reader["DEL_ADDRESS_1"]);
                            salesOrders.ShipToAddress2 = Convert.ToString(reader["DEL_ADDRESS_2"]);
                            salesOrders.ShipToAddress3 = Convert.ToString(reader["DEL_ADDRESS_3"]);
                            salesOrders.ShipToAddress4 = Convert.ToString(reader["DEL_ADDRESS_4"]);
                            salesOrders.ShipToAddress5 = Convert.ToString(reader["DEL_ADDRESS_5"]);
                            salesOrders.ShiptoAttention = Convert.ToString(reader["DEL_NAME"]);
                            salesOrders.ShiptoPhoneNo = Convert.ToString(reader["CUST_TEL_NUMBER"]);
                            salesOrders.Country = Convert.ToString(reader["ANALYSIS_1"]);
                            salesOrders.ShiptoEmail = "";
                            salesOrders.MakeAddressDefault = "Y";
                            salesOrders.ExporteDateTime = GetExportedDate(orderNumber);
                            bool isProcessed = hasbeenProcessed(orderNumber);
                            if (isProcessed == true)
                                salesOrders.Exported = true;
                            _salesOrdersList.Add(salesOrders);

                        }
                        backgroundWorker1.ReportProgress(80);
                    }
                }
            }

        }
        backgroundWorker1.ReportProgress(100);

    }
    catch (Exception ex)
    {

    }

    return _salesOrdersList;
}

            }
            backgroundWorker1.ReportProgress(100);

        }
        catch (Exception ex)
        {

        }

        return _salesOrdersList;
    }

在后台工作人员中,我在此处创建列表

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
        var listOrders = new List<SalesOrders>();
        listOrders = GetSalesOrders().OrderByDescending(o => o.OrderDate).ToList();
        var listBindingOrders = new BindingList<SalesOrders>(listOrders);
        SalesOrders = listBindingOrders;


}

这是我绑定上面的构造函数。

public BindingList<SalesOrders> SalesOrders = new BindingList<BusinessObjects.SalesOrders>();

这是我要在正在更改的导出标志上使用通知更改属性的类,但只有在关闭应用程序时才会更新。

public class SalesOrders : INotifyPropertyChanged 
{

    public bool selected { get; set; }
    public string ORDER_OR_QUOTE { get; set; }

    public string OrderNumber { get; set; }

    public string ACCOUNT_REF { get; set; }



    public string RecordIdentifier { get; set; }

    public DateTime OrderDate { get; set; }
    public string  ShipmmentId { get; set; }

    public string Company { get; set; }

    public string Carrier { get; set; }

    public string CarrierService { get; set; }
    public string Customer { get; set; }
    public string CustomerName { get; set; }
    public string ShiptoName { get; set; }
    public string ShipToAddress1 { get; set; }

    public string ShipToAddress2 { get; set; }

    public string ShipToAddress3 { get; set; }

    public string ShipToAddress4 { get; set; }

    public string ShipToAddress5 { get; set;


    }

    public string ShiptoAttention { get; set; }

    public string ShiptoPhoneNo { get; set; }

    public string ShiptoEmail { get; set; }

    public string County { get; set; }

    public string MakeAddressDefault { get; set; }
    public string Address
    {
        get
        {
            var sb = new StringBuilder();
            sb.Append(ShipToAddress1);
            sb.Append(ShipToAddress2);
            sb.Append(ShipToAddress3);

            return sb.ToString();
        }
    }

private bool ExportedValue = false;

public bool Exported
{
    get { return this.ExportedValue; }

    set
    {
        if (value != this.ExportedValue)
        {
            this.ExportedValue = value;
            NotifyPropertyChanged();
        }
    }
}
public event PropertyChangedEventHandler PropertyChanged;

// This method is called by the Set accessor of each property.
// The CallerMemberName attribute that is applied to the optional propertyName
// parameter causes the property name of the caller to be substituted as an argument.
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

    // The cons

    public DateTime ExporteDateTime { get; set; }
    public String Country  { get; set; }

}

我认为在这里展示我的绑定事件很重要。

// <summary>
/// Bind the list view to the sales order collection
/// </summary>
/// <param name="_salesOrders"></param>
private void BindListView(BindingList<SalesOrders> _salesOrders)
{

        invoiceListView.Items.Clear();
        foreach (SalesOrders _pur in _salesOrders)
        {

            invoiceListView.Items.Add(CreateListViewItem(_pur));

        }

        foreach (ListViewItem lvw in invoiceListView.Items)
        {
            Boolean hasExported = Convert.ToBoolean(lvw.SubItems[8].Text);
            if (hasExported == true)
            {
                lvw.BackColor = Color.Wheat;
            }
        }
}

我希望有人能帮我解决这个问题,但是在其他一切正常工作的那一刻,我却发疯了。

2 个答案:

答案 0 :(得分:0)

您是否已确保使用ListView1.ItemsSource = bindingList来与ListView关联的BindingList,然后坚持修改实际的BindingList而不是ListView? (例如,不使用listView1.Items.Add,而只是将其添加到listViews绑定集合中)

答案 1 :(得分:-1)

您是否尝试过每次都使用.refresh()方法