我正在做这个项目,有人要求我写一个代码来连接从InvoiceDB和CustomerDB到Form1.Cs的数据。我已经完成了我的代码,但不知何故它不能工作。当我运行一个程序时,form1中的列表视图无法从类中获取数据。我想向专家寻求一些帮助。谢谢。
我的代码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类
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;
}发布于 2016-03-24 16:17:14
当ListView处于详细模式时,您应该定义ColumnHeaderCollection,添加要在ListView中显示的列。如果不定义ColumnHeaderCollection,ListView将显示为空
来自MSDN的评论解释了这一点
Columns属性返回一个集合,该集合包含在ListView控件中显示的ColumnHeader对象。当View属性设置为Details时,ColumnHeader对象定义在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);有Add方法的many overloads,选择最适合您的任务
https://stackoverflow.com/questions/36195559
复制相似问题