2013-10-19 61 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); 
     }