4

私はリストを持っています

List<SalesDetail>  SalesList = new List<SalesDetail>();
SalesDetail detail = new SalesDetail();  

「SalesDetail」はクラスです。ボタン (追加) があり、追加ボタンのクリック イベントのコードは SalesList.Add(details); です。details は、{set; のパブリック変数を含むクラス SalesDetail のオブジェクトです。そして取得;}

しかし、リストの各項目を取得しようとすると、最後の項目しか取得できません。各アイテムを取得する私のコードは

foreach(SalesDetail sd in SalesList)
{

    messageBox.show(SalesList);

}

私のクラス SalesDetail には、次のコードがあります

Public string Brand{get; set;}
Public string Product{get; set;}

リストから各アイテムを取得してデータベースに保存したいのですが、データの取得中にどこで間違いを犯したのか知りたいのですが..助けてくださいよろしくbunzitop

4

3 に答える 3

2

sd現在のアイテムを参照するオブジェクトを使用する必要がありますSalesList

試す:

foreach(SalesDetail sd in SalesList)
{

    messageBox.show(sd.Brand);
    messageBox.show(sd.Product);

}

チャットから:

List<SalesDetail> SalesList = new List<SalesDetail>();

public void button1_click() {

    SalesDetail detail = new SalesDetail();
    detail.Brand = textBox1.Text
    detail.Product= textBox2.Text` 
    SalesList.Add(detail);

}
于 2013-03-26T12:49:06.557 に答える
2

SalesListタイプです。sdループで (変化する値) を使用する必要があります。

于 2013-03-26T12:49:52.653 に答える
0

Brandまず、Productプロパティのタイプを省略し、public可視性修飾子を小文字にする必要があるため、クラス定義が間違っています。

使用ToString()するには、クラスのメソッドをオーバーライドする必要があります。

public class SalesDetail
{
    public string Brand {get; set;}
    public string Product {get; set;}

    public override string ToString()
    {
        return string.Format("Brand: {0}, Product {1}", Brand, Product);
    }
}

次に、Linq をAggregateリストに使用して、その内容を表示できます。

var items = SalesList.Select(s => s.ToString()).Aggregate((s, s1) => s + Environment.NewLine + s1);
MessageBox.Show(items);
于 2013-03-26T13:19:53.963 に答える