基于组合框选择更新文本框(.CSV / Array)

时间:2012-12-10 23:16:00

标签: c# .net winforms csv properties

我有一个包含列的.csv文件。我已经有一个允许用户插入信息并将其保存到此.csv文件的表单。

在单独的表单上,我创建了按项目ID选择项目的功能。我有一个组合框,它显示.csv文件中的项目ID下拉列表。现在,唯一的商品ID是1000-1003。

    public void Items()
    {
        int itemID = 0;
        Inventory[] IArray = Inventory.getInv();
        for (int v = 0; v < IArray.Length; v++)
        {
            if (IArray[v] != null)
            {
                itID = IArray[v].getIID();
                Field.Items.Add(itID + "\r\n");
            }
        }
    }

当我选择某个商品ID时,我希望我的ReadOnly文本框根据文件以适当的名称/折扣/价格更新。问题是,无论我选择什么ID,它都会显示.csv文件的最后一行(现在最后一行是Item ID 1003)。

    private void Field_SelectedIndexChanged(object sender, EventArgs e)
    {
        string itName = "";
        double disc = 0;
        double price = 0;
        Inventory[] IArray = Inventory.getInv();
        for (int v = 0; v < IArray.Length; v++)
        {
            if (IArray[v] != null)
            {
                itName = IArray[v].getItName();
                disc = IArray[v].getDisc();
                price = IArray[v].getPrice();

                itNameS.Text = (itName);
                itPriceS.Text = Convert.ToString(price);
                itDiscountS.Text = Convert.ToString(disc);
            }
        }
    }

2 个答案:

答案 0 :(得分:2)

您可以使用以下项目列表绑定到您的组合框。然后,您可以在更改索引时设置文本框文本值。

我的属性与您的属性完全不符,但是每次更改索引时都不需要获取项目,因此可以节省您的时间。

class Item : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(string info)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(info));
    }

    private string _itemId;
    public String ItemID
    {
        get { return _itemId; }
        set { _itemId = value; NotifyPropertyChanged("ItemID"); }
    }

    private string _itemName;
    public String ItemName
    {
        get { return _itemName; }
        set { _itemName = value; NotifyPropertyChanged("ItemName"); }
    }

    private double _discountValue;
    public Double DiscountValue
    {
        get { return _discountValue; }
        set { _discountValue = value; NotifyPropertyChanged("DiscountValue"); }
    }

    private double _price;
    public Double Price
    {
        get { return _price; }
        set { _price = value; NotifyPropertyChanged("Price"); }
    }
}


public partial class Form1 : Form
{
    BindingList<Item> OurItems = new BindingList<Item>();

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        PopulateItems();
        BindComboBox();


    }

    private void BindComboBox()
    {
        cboItems.DataSource = OurItems;
        cboItems.DisplayMember = "ItemID";
        cboItems.ValueMember = "ItemID";
    }

    private void PopulateItems()
    {
        StreamReader sr = new StreamReader(@"New Text Document (4).txt");
        foreach (string sLine in sr.ReadToEnd().Split(new string[] {"\r\n"}, StringSplitOptions.RemoveEmptyEntries ))
        {
            var oProps = sLine.Split(new char[] {','});
            OurItems.Add(new Item() {ItemID = oProps[0], ItemName = oProps[1], DiscountValue = Double.Parse(oProps[2]), Price = Double.Parse(oProps[3])});
        }
    }

    private void cboItems_SelectedIndexChanged(object sender, EventArgs e)
    {
        var oSelected = (Item)cboItems.SelectedItem;

        tbName.Text = oSelected.ItemName;
        tbDiscount.Text = oSelected.DiscountValue.ToString();
        tbPrice.Text = oSelected.Price.ToString();

    }

    private void button1_Click(object sender, EventArgs e)
    {
        OurItems[0].Price = OurItems[0].Price + 50;
    }
}

enter image description here

答案 1 :(得分:1)

您正在浏览整个数组并更新每个记录的行(行;数组元素)。

您需要添加一个停止处理/更新的条件。有点像:

    for (int v = 0; v < myInvArray.Length; v++)
    {
        if ((myInvArray[v] != null) || (myInvArray[v].getItemID() == the_id_from_the_combobox))
        {
            itemName = myInvArray[v].getItemName();
            discountValue = myInvArray[v].getDiscountValue();
            price = myInvArray[v].getPrice();

            itemNameShown.Text = (itemName);
            itemPriceShown.Text = Convert.ToString(price);
            itemDiscountShown.Text = Convert.ToString(discountValue);
        }
    }