0

I have a .csv file containing columns. I already have a form which allows the user to insert and save the information to this .csv file.

On a separate form, I created the ability to select items by their Item ID. I have a combobox which is displaying a drop down of Item IDs from the .csv file. Right now, the only Item IDs are 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");
            }
        }
    }

When I select a certain Item ID, I would like to have my ReadOnly textboxes update with the appropriate Name/Discount/Price according to the file. The problem is, no matter what ID I choose, it's displaying the last line of the .csv file (right now the last line is 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);
            }
        }
    }
HNVDB
  • 39
  • 1
  • 7

2 Answers2

2

You may utilize a List of Items such as the following to bind to your combobox. You may then set the text boxes text values on index changed.

My properties don't match yours exactly, but will save you time becasue you do not need to fetch the items every time you change index.

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

KreepN
  • 8,528
  • 1
  • 40
  • 58
  • Hey, I have a super quick question when you come online.:) – HNVDB Dec 13 '12 at 01:20
  • I thought I could update "rewrite" a line if it contained a certain Customer ID..http://stackoverflow.com/questions/13852331/updating-a-csv-file-using-arrays – HNVDB Dec 13 '12 at 04:24
1

You are going through the entire array and updating the boxes for every single record (line; array element).

You need to add a condition that stops the processing/updating. Something along the lines of:

    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);
        }
    }
Sam Axe
  • 33,313
  • 9
  • 55
  • 89
  • Thank you for your reply. when you say "the_id_from_the_combobox" do you mean, I should add the variable "itemID" and then edit my code to if ((myInvArray[v] != null) || myInvArray[v].getItemID() == (itemID)) – HNVDB Dec 10 '12 at 23:42
  • Because it's not working for me. So I have a feeling that I am misinterpreting "the_id_from_the_combobox"...Thanks again! – HNVDB Dec 10 '12 at 23:58
  • I don't know how you are getting the values into the combobox.. so it will be something like ordItemIDField.SelectedText (you need to get the ItemID value of the selected item in the combobox, and the code you use to do that will depend on how you populated the combo. Try the `.SelectedText` property first) – Sam Axe Dec 11 '12 at 00:03