我是一名大学生,正在为一个单位做一个项目。我们正在创建一个 Web 应用程序仪表板,我似乎无法从列表框 (customerNameListBox) 中删除重复项。
public partial class Graphs : System.Web.UI.MasterPage
{
string FullDataCSV = Path.Combine(HttpContext.Current.Server.MapPath
("~/App_Data/Full_Data.csv"));
List<CSVEntry> CSVList = new List<CSVEntry>();
public void ReadFile()
{
try
{
StreamReader inputFile;
string line;
CSVEntry entry = new CSVEntry();
char[] delim = { ',' };
inputFile = File.OpenText(FullDataCSV);
while (!inputFile.EndOfStream)
{
line = inputFile.ReadLine();
string[] tokens = line.Split(delim);
entry.Value0 = tokens[0];
entry.customerName = tokens[22];
entry.Value29 = tokens[29];
CSVList.Add(entry);
}
}
catch
{
Response.Redirect("Error.aspx");
}
}
private void DisplayCustomerName()
{
foreach (CSVEntry entry in CSVList)
{
customerNameListBox.Items.Add(entry.customerName);
}
}
private void SortCustomerName()
{
CSVList = CSVList.OrderBy(x => x.customerName).ToList();
}
protected void Page_Load(object sender, EventArgs e)
{
ReadFile();
SortCustomerName();
DisplayCustomerName();
}
protected void historyButton_Click(object sender, EventArgs e)
{
Response.Redirect("History.aspx");
}
protected void exportButton_Click(object sender, EventArgs e)
{
Response.Redirect("Export.aspx");
}
protected void printButton_Click(object sender, EventArgs e)
{
Response.Redirect("Print.aspx");
}
}
我尝试使用以下代码删除 customerNameTextBox 中的重复项,但它根本不起作用。
protected void goButton_Click(object sender, EventArgs e)
{
List<string> removals = new List<string>();
foreach (string s in customerNameListBox.Items)
{
removals.Add(s);
}
foreach (string s in removals)
{
customerNameListBox.Items.Remove(s);
}