我正在研究这个项目,并被要求编写一个代码来将InvoiceDB和CustomerDB中的数据连接到Form1.Cs。 我完成了我的代码但不知何故它没有用。当我运行程序时,form1中的Listview无法从类中获取数据。 我想请求专家提供一些帮助。 谢谢。
我的代码Form1.cs
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 Class
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 :(得分:0)
当ListView处于详细模式时,您应该定义ColumnHeaderCollection添加要在ListView中显示的列。 如果没有定义ColumnHeaderCollection,ListView将显示为空
MSDN的这句话解释了它
Columns属性返回包含的集合 ListView控件中显示的ColumnHeader对象。 ColumnHeader对象定义显示在的列 View属性设置为Details时的ListView控件。每一栏 用于显示ListView中每个项目的子项信息。 有关如何操作中的项目的更多信息 集合,请参阅ListView.ColumnHeaderCollection。
您应该将列添加到ListView,类似这样
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);
添加方法有many overloads,选择最适合您的任务