Removing duplicate items (string) from listBox

一个人想着一个人 提交于 2020-01-06 13:55:29

问题


I'm a university student doing a project for a unit. We're creating a web application dashboard, and I can't seem to remove duplicates from a 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");
    }
}

I have tried using the below code to remove the duplicate items in the customerNameTextBox, but it's not working at all.

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);
        }

回答1:


Update your code and check for duplicate before adding item in the dropdown list.

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

Now you will not require to delete duplicate values from your dropdown list.

Or even you can select distinct values in your collection using linq.




回答2:


I think this will be helpful for you.

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



回答3:


it can help you

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

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



回答4:


i think it also can help you...

    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());
        }
    }


来源:https://stackoverflow.com/questions/19462923/removing-duplicate-items-string-from-listbox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!