从listBox中删除重复项(字符串)

时间:2013-10-19 05:59:53

标签: c# asp.net listbox

我是一名大学生,为一个单位做项目。我们正在创建一个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);
        }

4 个答案:

答案 0 :(得分:4)

在下拉列表中添加项目之前,请更新您的代码并检查重复。

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

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

甚至您可以使用linq在集合中选择不同的值。

答案 1 :(得分:2)

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

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

答案 2 :(得分: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);
        }

答案 3 :(得分: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());
        }
    }