2013-10-19 37 views
0

我是一个大学生为单位做项目。我们正在创建一个Web应用程序仪表板,我似乎无法从listBox(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); 
     } 
+2

边注:请不要写那么多没用的意见(我去除了大部分来自你的代码)。如果您的函数或变量名称不表示您意图重命名而不是添加注释。 'ReadFile(); //读取文件'是荒谬的。查看http://www.sscce.org以获得编写问题示例代码的指导。 –

回答

2

更新您的代码并在下拉列表中添加项目前检查是否有重复。

private void DisplayCustomerName() 
{ 
    foreach (CSVEntry entry in CSVList) 
    { 
      ListItem item = new ListItem(entry.customerName); 
     if (! customerNameListBox.Items.Contains(item)) 
     { 
       customerNameListBox.Items.Add(item); 
     } 
    } 
} 

现在您不需要从下拉列表中删除重复的值。

甚至可以使用LINQ选择您的收藏不同的值。

+0

OP声称要做大学的作业 - 线性搜索将纯粹反映... –

+0

哦该死!这工作!感谢堆队友。一直在努力争取这一点,我想我最终只是太糊涂了。谢谢! – user2897087

2

我认为这会对你有所帮助。

if (ListBox.SelectedItem != null) 
{ 
    ListBox.Items.RemoveAt(ListBox.SelectedIndex); 
}` 
0

我认为它也可以帮助你......

foreach (DataRow row in dtTemp.Rows) 
    { 
     ListItem lstim = new ListItem(); 
     lstim.Text = row["ExamleColumnName1"].ToString(); 

     if (lstSelectedWorkout.Items.Contains(lstim) == true) 
     { 
      // Response.Write("<script>alert('" + lstMajorMuscles.SelectedItem.Text + " already exist in the list')</script>"); 
     } 
     else 
     { 
      lstSelectedWorkout.Items.Add(row["ExamleColumnName1"].ToString()); 
     } 
    } 
1

它可以帮助你

List<string> removals = new List<string>(); 
     foreach (string str in ListBox1.Items) 
     { 
      removals.Add(str); 
     } 

     foreach (string str in removals) 
     { 
      ListBox1.Items.Remove(str); 
     }