问题
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);
}
}
}
回答1:
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;
}
}

回答2:
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);
}
}
来源:https://stackoverflow.com/questions/13810959/update-textbox-based-on-combobox-selection-csv-array