我收到了一些錯誤,並且我的代碼還未完成。我正在使用另一個Stackoverflow問題來設置它,但它不適合我的需要。C#從多個文本文件中讀取數據,將行分割成List,然後加載到列表框中
我有三個文本文件,其中數據被逗號分隔,如「Name,25,25.6」so string,int,decimal。我有全部三個文本文件,它們有三列,相同的數據類型,但只是不同的名稱/數字。
我有三個不同的列表框,我想分割它們,但我無法獲得三個不同的分割列表項進入三個不同的列表框。我將複製並粘貼我擁有的所有代碼。我也使用組合框來允許用戶選擇他們想要加載到組合框的文件,我相信我已經做對了。
我得到的錯誤是在displayLists()中,它說lstItemName.DataSource = Inventory;庫存在當前上下文中不存在的行。還有其他一些錯誤。
任何幫助將不勝感激,我會複製和粘貼我的代碼。我有一個Windows窗體和我使用Visual Studio Express的2012年C#
namespace TCSCapstone
{
public partial class frmInventory : Form
{
public frmInventory()
{
InitializeComponent();
}
string cstrItemName;
int cintNumberOfItems;
decimal cdecPrice;
decimal cdecTotalPrices;
string selectedList = "";
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
selectedList = this.cmbList.GetItemText(this.cmbList.SelectedItem);
if (selectedList == "Creative Construction")//if the selected combo
box item equals the exact string selected
{
selectedList = "creative"; //then the string equals creative,
which is creative.txt but I add the .txt in the btnLoadInfo method
} else if (selectedList == "Paradise Building")
{
selectedList = "paradise";//this is for paradise.txt
}
else if (selectedList == "Sitler Construction")
{
selectedList = "sitler";//this is for sitler.txt
}
else
{
MessageBox.Show("Please select one of the items.");
}
}
private void btnLoadInfo_Click(object sender, EventArgs e)
{
List<frmInventory> Inventory = new List<frmInventory>();
using (StreamReader invReader = new StreamReader(selectedList +
".txt"))
{
while (invReader.Peek() >= 0)
{
string str;
string[] strArray;
str = invReader.ReadLine();
strArray = str.Split(',');
frmInventory currentItem = new frmInventory();
currentItem.cstrItemName = strArray[0];
currentItem.cintNumberOfItems = int.Parse(strArray[1]);
currentItem.cdecPrice = decimal.Parse(strArray[2]);
Inventory.Add(currentItem);
}
}
displayLists();
}//end of btnLoadInfo
void displayLists()
{
int i;
lstItemName.Items.Clear();
lstNumberOfItems.Items.Clear();
lstPrice.Items.Clear();
lstTotalPrices.Items.Clear();
lstItemName.DataSource = Inventory;
lstItemName.ValueMember = "cstrItemName";
lstItemName.DisplayMember = "cintNumberOfItems";
}
}//end of frmInventory
}//end of namespace
你創造表格列表? –