2016-03-24 18 views
0

私はこのプロジェクトに取り組んでおり、InvoiceDBとCustomerDBのデータをForm1.Cに接続するコードを書くように求められました。 私のコードは終わっていますが、何とかうまくいきませんでした。プログラムを実行すると、form1のListviewはクラスからデータを取得できませんでした。 私は専門家から助けを求めたいと思います。 ありがとうございます。カスタマーインボイスフォーム。一般的なリスト。 C#

private void Form1_Load(object sender, EventArgs e) 
    { 
     { 
      **//Get data from Invoice and Customer Class** 
      List<Invoice> invoiceList = InvoiceDB.GetInvoices(); 
      List<Customer> customerList = CustomerDB.GetCustomers(); 

      **//Query expression** 
      var invoices = from invoice in invoiceList 
           join customer in customerList 
           on invoice.CustomerID equals customer.CustomerID 
           orderby customer.Name, invoice.InvoiceTotal descending 
          select new {customer.Name, invoice.InvoiceID, invoice.InvoiceDate, invoice.InvoiceTotal}; 

      **//Executes the query** 
      string customerName = " "; 
      int i = 0; 
      foreach (var invoice in invoices) 
      { 
       if (invoice.Name != customerName) 
       { 
        lvInvoice.Items.Add(invoice.Name); 
        customerName = invoice.Name; 
       } 
       else 
       { 
        lvInvoice.Items.Add(" "); 
       } 
       lvInvoice.Items[i].SubItems.Add(invoice.InvoiceID.ToString()); 
       lvInvoice.Items[i].SubItems.Add(Convert.ToDateTime(invoice.InvoiceDate).ToShortDateString()); 
       lvInvoice.Items[i].SubItems.Add(invoice.InvoiceTotal.ToString("C")); 
       i += 1; 
      } 
    } 
    } 

のCustomerDBクラス

public static class CustomerDB 
{ 

    private const string Dir = @""; 
    private const string Path = Dir + "CustomersX23.txt"; 

    public static List<Customer> GetCustomers() 
    { 
     List<Customer> customers = new List<Customer>(); 

     StreamReader textIn = new StreamReader(
      new FileStream(Path, FileMode.Open, FileAccess.Read)); 

     while (textIn.Peek() != -1) 
     { 
      string row = textIn.ReadLine(); 
      string[] columns = row.Split('|'); 
      Customer customer = new Customer(); 
      customer.CustomerID = Convert.ToInt32(columns[0]); 
      customer.Name = columns[1]; 
      customers.Add(customer); 
     } 

     textIn.Close(); 
     return customers; 
    } 

InvoiceDBクラス

public static class InvoiceDB 
{ 

    private const string Dir = @""; 
    private const string Path = Dir + "Invoices.txt"; 

    public static List<Invoice> GetInvoices() 
    { 
     List<Invoice> invoices = new List<Invoice>(); 

     StreamReader textIn = new StreamReader(
      new FileStream(Path, FileMode.Open, FileAccess.Read)); 

     while (textIn.Peek() != -1) 
     { 
      string row = textIn.ReadLine(); 
      string[] columns = row.Split('|'); 
      Invoice invoice = new Invoice(); 
      invoice.InvoiceID = Convert.ToInt32(columns[0]); 
      invoice.CustomerID = Convert.ToInt32(columns[1]); 
      invoice.InvoiceDate = Convert.ToDateTime(columns[2]); 
      invoice.ProductTotal = Convert.ToDecimal(columns[3]); 
      invoice.SalesTax = Convert.ToDecimal(columns[4]); 
      invoice.Shipping = Convert.ToDecimal(columns[5]); 
      invoice.InvoiceTotal = Convert.ToDecimal(columns[6]); 
      invoices.Add(invoice); 
     } 

     textIn.Close(); 
     return invoices; 
    } 

答えて

0

Form1.csの私のコードリストビューは詳細モードの場合、あなたがそのあなたの列を追加しColumnHeaderCollectionを定義する必要がありますListViewに表示したい ColumnHeaderCollectionにリストビューを定義せずにこれはMSDNから発言

Columnsプロパティは、ListViewコントロールに表示されている のColumnHeaderオブジェクトを含むコレクションを返し、それ

説明空になったように見えます。 ColumnHeaderオブジェクトは、ViewプロパティがDetailsに設定されている場合に、 ListViewコントロールに表示される列を定義します。各列 は、ListViewの各項目の副明細情報を表示するために使用されます。 コレクション内のアイテムを操作する方法の詳細については、ListView.ColumnHeaderCollectionを参照してください。

あなたはありAddメソッドのmany overloadsは、あなたのタスク

+0

に最も適したあなたの応答をありがとう選択し、ですが、あなたのリストビューに、この

lvInvoice.Columns.Add("Name", -2, HorizontalAlignment.Left); lvInvoice.Columns.Add("ID", -2, HorizontalAlignment.Left); lvInvoice.Columns.Add("Date", -2, HorizontalAlignment.Left); lvInvoice.Columns.Add("Total", -2, HorizontalAlignment.Left); 

のようなものを列を追加する必要があります私はすでにデザインフォームに列を追加しました。他の人の意見ですか? – Duke

+0

次に、デバッガの使用をお勧めします。データを読み込むメソッドと、それらを結合するlinqクエリの後にブレークポイントを設定します。必要なデータがあるかどうかを確認します。 – Steve

+0

私はあなたのコードをコピーして問題を再現しようとしましたが、コードに列を定義してからListViewに要素を追加して表示するまでは同じ問題があります。 _lvInvoice.Columns.Clear(); _を追加し、コードから列を追加してください。 – Steve