Reputation: 3
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);
}
Upvotes: 0
Views: 10935
Reputation: 17614
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.
Upvotes: 4
Reputation:
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);
}
Upvotes: 1
Reputation: 2422
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());
}
}
Upvotes: 0
Reputation:
I think this will be helpful for you.
if (ListBox.SelectedItem != null)
{
ListBox.Items.RemoveAt(ListBox.SelectedIndex);
}`
Upvotes: 2