1

「List all Customers」ボタンをクリックすると、コードが Customer.csv ファイルを読み取り、「List All Customers」というフォームに情報を表示する必要があります。

どうやってやるの?

public static void ReadFile()
{
    StreamReader sr = File.OpenText("Customer.csv");
}

public static void LoadCustomers()
{
    try
    {
        if (File.Exists("Customer.csv"))
        {
            string temp = null;
            int count = 0;

            using (StreamReader sr = File.OpenText(@"Customer.csv"))
            {
                while ((temp = sr.ReadLine()) != null)
                {
                    temp = temp.Trim();
                    string[] lineHolder = temp.Split(',');
                    Customer tempCust = new Customer();
                    tempCust.customerName = lineHolder[0];
                    tempCust.customerAddress = lineHolder[1];
                    tempCust.customerZip = Convert.ToInt32(lineHolder[2]);
                    myCustArray[count] = tempCust;
                    count++;
                }//end for loop
            }
        }
        else
        {
            File.Create("Customer.csv");
        }
    }
    catch (Exception e)
    {
        System.Windows.Forms.MessageBox.Show("File Loading Error: " + e.Message);
    }
}
4

2 に答える 2

0

まず、リストオブジェクトを利用します。

public static void ReadFile()
{
    StreamReader sr = File.OpenText("Customer.csv");
}

public static void LoadCustomers()
{
    try
    {
        if (File.Exists("Customer.csv"))
        {
            string temp = null;
            var retList = new List<Customer>();
            using (StreamReader sr = File.OpenText(@"Customer.csv"))
            {
                while ((temp = sr.ReadLine()) != null)
                {
                    temp = temp.Trim();
                    string[] lineHolder = temp.Split(',');
                    retlist.add(new Customer(){
                      customerName = linerHolder[0],
                      customerAddress = lineHolder[1],
                      customerZip = Convert.ToInt32(lineHolder[2])
                    });
                }//end for loop
            }
        }
        else
        {
            File.Create("Customer.csv");
        }
    }
    catch (Exception e)
    {
        System.Windows.Forms.MessageBox.Show("File Loading Error: " + e.Message);
    }
}

それをクラスにまとめ、コントローラーからifを呼び出して、結果を入力するだけです。このデータを更新する頻度によっては、データのキャッシュを検討する場合があるため、ユーザーごとにX秒ごとにこのプロセスを実行する必要はありません。

于 2012-12-16T05:42:49.050 に答える
0

このデータをどの種類のコントロールに表示したいかはわかりませんが、メソッドは のリストを返すだけでCustomer、 に追加できますListBox,ListViewまたはDataGrid

public static IEnumerable<Customer> LoadCustomers(string filename)
{
    if (File.Exists(filename))
    {
        foreach (var line in File.ReadAllLines(filename).Where(l => l.Contains(',')))
        {
            var splitLine = line.Split(',');
            if (splitLine.Count() >= 3)
            {
                yield return new Customer
                {
                    customerName = splitLine[0].Trim(),
                    customerAddress = splitLine[1].Trim(),
                    customerZip = Convert.ToInt32(splitLine[2].Trim())
                };
            }
        }
    }
}

リストボックス

 listBox1.DisplayMember = "customerName";
 listBox1.Items.AddRange(LoadCustomers(@"G:\Customers.csv").ToArray());
于 2012-12-16T05:42:02.827 に答える