I have a checkboxlist which I have bind with database in which there are around 9000 items , some of them are selected.I have list in which I have 5000 items.I have to check these 5000 items in checkboxlist and remaining unchecked. Please suggest optimized way.
What I try
foreach (var eachName in Namelist)
{
foreach (ListItem eachCblNameItem in cblName.Items)
{
if (eachCblNameItem.Value == eachName)
{
eachCblNameItem.Selected = true;
}
else
{
eachCblNameItem.Selected = false;
}
}
}
Just an idea as (linq) pseudo-code:
var itemsSelected = from item in checkBoxList.Items
join dbItem in database.SelectedItems
on item.UniqueKey equals dbItem.UniqueKey
select item;
foreach( var item in itemsSelected )
{
item.Selected = true;
}
Related
I have a scenario where I do filters depending of checkbox checked, now I have only 2 checkbox and I need to cover all scenarios into if, else conditionals like:
//List Example:
var projectTechnicians = (from DataRow dr in dtEmployeGuid.Rows
where dr["Title"].ToString().Contains("Project Technician")
select new
{
EmpGuid = (Guid)dr["EmpGuid"]
}).ToList();
if (!chkProjectTechs.Checked && !chkTeamLeader.Checked)
{
foreach (DataRowView list in lstTech.SelectedItems)
{
var selectedEmpGuid = (Guid)list[0];
EmpGuid.Add(selectedEmpGuid);
}
parameters = ToDataTable(EmpGuid);
}
else if (!chkTeamLeader.Checked && chkProjectTechs.Checked)
{
foreach (var technician in projectTechnicians)
{
EmpGuid.Add(technician.EmpGuid);
}
parameters = ToDataTable(EmpGuid);
}
else if (!chkProjectTechs.Checked && chkTeamLeader.Checked)
{
foreach (var teamLeader in teamLeaders)
{
EmpGuid.Add(teamLeader.EmpGuid);
}
parameters = ToDataTable(EmpGuid);
}
else if (chkProjectTechs.Checked && chkTeamLeader.Checked)
{
foreach (var technician in projectTechnicians)
{
EmpGuid.Add(technician.EmpGuid);
}
parameters = ToDataTable(EmpGuid);
foreach (var teamLeader in teamLeaders)
{
EmpGuid.Add(teamLeader.EmpGuid);
}
parameters = ToDataTable(EmpGuid);
}
But I need to Add more checkbox, but foreach checkbox I will add to my form I need to add it to each conditional, and at the final of the day I will get a very long code. Is there another way to do this?
There is an issue in you last else if condition. parameters is getting overwritten by teamleaders empGuids
foreach (var technician in projectTechnicians)
{
EmpGuid.Add(technician.EmpGuid);
}
parameters = ToDataTable(EmpGuid);
foreach (var teamLeader in teamLeaders)
{
EmpGuid.Add(teamLeader.EmpGuid);
}
parameters = ToDataTable(EmpGuid); // Overwriting projectTechnicians added above.
You can simplify your code by using if condition only, check my comments in the code.
// add EmpGuids from lstTech if no check box is selected.
// you need to add all checkboxes not selected in the condition
if (!chkProjectTechs.Checked && !chkTeamLeader.Checked)
{
foreach (DataRowView list in lstTech.SelectedItems)
{
var selectedEmpGuid = (Guid)list[0];
EmpGuid.Add(selectedEmpGuid);
}
}
// if projectTechnicians is checked, then add projectTechnicians EmpGuids to parameters
if (chkProjectTechs.Checked)
{
foreach (var technician in projectTechnicians)
{
EmpGuid.Add(technician.EmpGuid);
}
}
// add team leader Empguids if checked.
if (chkTeamLeader.Checked)
{
foreach (var teamLeader in teamLeaders)
{
EmpGuid.Add(teamLeader.EmpGuid);
}
}
// Add new checkbox conditions here
//
// perform your next operation on parameters here.
// At this point you will have either lstTech employees or employees based on checkbox selection in EmpGuid list.
parameters = ToDataTable(EmpGuid);
EDIT
if you are using LINQ, then you can use below LINQ query to add EmpGuids instead of using foreach, for example.
EmpGuid.AddRange(teamLeader.Select(x=>x.EmpGuid));
I got a problem with wpf ComboBox.
I first added a Textbox as the first item to use it for my filtering propose
I then added about 20 Checkboxes in the Combobox through a Foreach loop.
like this
now when i filter them out (i check if true then Visibility.Collapsed) their trace are still in the Combobox like this
Remember that the items are one by one added to the combobox
like this
DataTable machinesTable = machineModel.GetAllMachines().Tables[0];
List<CheckBox> list = new List<CheckBox>();
foreach (DataRow item in machinesTable.Rows)
{
string ID = item["ID"].ToString();
string manufacture = item["MANUFACTURER"].ToString();
string model = item["MODEL"].ToString();
MachinesComboBox.Items.Add(new CheckBox() { Uid = ID, Content = manufacture + " - " + model });
}
and the filtering system works like this
foreach (object item in MachinesComboBox.Items)
{
if (item is CheckBox)
{
if (((CheckBox)item).Content.ToString().Contains(MachinFilterTextbox.Text) || MachinFilterTextbox.Text=="")
{
((CheckBox)item).Visibility = Visibility.Visible;
}
else
{
((CheckBox)item).Visibility = Visibility.Collapsed;
}
}
}
You should look into the MVVM design pattern but as a quick fix you could set the Visibility of the parent ComboBoxItem container:
foreach (CheckBox item in MachinesComboBox.Items.OfType<CheckBox>())
{
ComboBoxItem container = MachinesComboBox.ItemContainerGenerator.ContainerFromItem(item) as ComboBoxItem;
if (item.Content.ToString().Contains(MachinFilterTextbox.Text) || MachinFilterTextbox.Text == "")
{
container.Visibility = Visibility.Visible;
}
else
{
container.Visibility = Visibility.Collapsed;
}
}
Uhmm... Excuse me can anybody help... I'm trying to compare if item already existing in listview, but first when I add an item it's okay. But when I change the item then add the item it still says duplicate entry. What I want to do is when every time I add an item it will detect if item is already in listview. Thanks in advance!!
while (myReadersup1.Read())
{
string item = myReadersup1.GetString("ProdName");
string price = myReadersup1.GetFloat("ProdPrice").ToString("n2");
string noi = cmbNOI.SelectedItem.ToString();
bool alreadyInList = false;
foreach (ListViewItem itm in lvCart.Items)
{
if (lvCart.Items.Cast<object>().Contains(itm))
{
alreadyInList = true;
MessageBox.Show("Duplicate Entry!");
break;
}
}
if (!alreadyInList)
{
lvCart.Items.Add(new ListViewItem(new string[] { item, price, noi }));
}
In your foreach loop, you need an else that will set alreadyInList to false if the item isn't found. Otherwise, it's always true.
As you foreach through the current items, you are checking to see if it is in the items collection, and that will always be true. You may want to change it to something similar:
private bool AddListViewItem(string item, string price, string noi)
{
if (this.DetectDuplicate(item, price, noi))
return false;
string[] content = new string[] { item, price, noi };
ListViewItem newItem = new ListViewItem();
newItem.Content = content;
_listView.Items.Add(newItem);
return true;
}
private bool DetectDuplicate(string item, string price, string noi)
{
foreach (ListViewItem lvwItem in _listView.Items)
{
string[] itemContent = lvwItem.Content as string[];
Debug.Assert(itemContent != null);
if (itemContent[0].Equals(item) &&
itemContent[1].Equals(price) &&
itemContent[2].Equals(noi))
return true;
}
return false;
}
I try to add item in list view .
But this code like not working at all.
Where did i do wrong ?
btn.Click += (senders, eventArgs) =>
{
foreach (ListViewItem lvis in lvSales.Items)
{
if (lvis.SubItems[0].Text == btn.Text)
{
MessageBox.Show("!!!!!!!");
}
else
{
lvis.Text = count.ToString();
lvis.SubItems.Add(btn.Text);
lvis.SubItems.Add(btn.Name);
lvis.SubItems.Add(count.ToString());
lvis.SubItems.Add(btn.Tag.ToString()); // Email
lvSales.Items.Add(lvis);
count++;
}
}
};
I wan add item to list view.
If the item already added it will add the quantity
else it will add new .
btw when I clicked the button nothing happen .
you cannot add items to the collection you are iterating through with foreach (lvSales.Items). Consider changing foreach to some other loop like 'for(...'
Try this. It is not completely correct but try and fix minor bugs.
var itemFound = false;
foreach (var listViewItem in lvSales.Items)
{
if (listViewItem.SubItems[0].Text == btn.Text)
{
itemFound = true; break;
}
}
if (!itemFound)
{
var newlistViewItem = new ListViewItem();
newlistViewItem.Text = count.ToString();
newlistViewItem.SubItems.Add(btn.Text);
newlistViewItem.SubItems.Add(btn.Name);
newlistViewItem.SubItems.Add(count.ToString());
newlistViewItem.SubItems.Add(btn.Tag.ToString()); // Email
lvSales.Items.Add(lvis);
}
btn.Click += (senders, eventArgs) =>
{
foreach (ListViewItem lvis in lvSales.Items)
{
if (lvis.SubItems[0].Text == btn.Text)
{
//get current quantity of listitem, increment it,
//add the new value to this listitem quantity value...
//keep track of current index, use that to set the new value...
}
else
{
//re instantiate listviewitem, set its values, and add it
}
}
};
This is what i'm trying to accomplish. I want to select only distinct values from all Rows in Column[0].
Then I want to get all the distinct values from column[2] and group them on column[0].
so basically i got a DataTable like so:
Fruit|Apples
Fruit|Pears
Vegetables|Peas
Vegetables|Carrots
so I want to do a foreach on the distinct values, so I would Enumerate Fruit once and then pick up Apples and Pears, and VegeTables once and pick up Peas and Carrots.
I'm doing this to create Accordion Panes, where I want to group my results under one header, the below code does such, however, it creates two panes of Fruit because it does not realize it already went though fruit.
foreach (DataRow dtrow in dtTable.Rows)
{
string idRow = dtrow[0].ToString();
AccordionPane currentPane = new AccordionPane();
currentPane.ID = "AccordionPane" + Guid.NewGuid().ToString();
currentPane.HeaderContainer.Controls.Add(new LiteralControl(dtrow[0].ToString()));
foreach(DataRow dtRow2 in dtTable.Rows)
{
if(dtRow2[0].ToString() == idRow)
{
currentPane.ContentContainer.Controls.Add(new LiteralControl(dtRow2[1].ToString()));
}
}
NavigateAccordion.Panes.Add(currentPane);
}
You can accomplish this easily when using linq, see for yourself:
var groupedRows = from row in dtTable.Rows.AsEnumerable()
group row by row[0] into grouped
select grouped;
foreach (var group in groupedRows)
{
currentPane = new AccordionPane();
currentPane.HeaderContainer.Controls.Add(group.Key.ToString());
foreach (var row in group)
{
currentPane.ContentContainer.Controls.Add(row[1].ToString());
}
}
Or if you want to stick with your current non-linq approach:
foreach (DataRow dtrow in dtTable.Rows)
{
bool skip = false;
foreach (var pane in NavigateAccordion.Panes)
{
if (pane.HeaderContainer.Controls[0].Text == dtRow[0].ToString())
{
skip = true;
break;
}
}
if (!skip)
{
string idRow = dtrow[0].ToString();
AccordionPane currentPane = new AccordionPane();
currentPane.ID = "AccordionPane" + Guid.NewGuid().ToString();
currentPane.HeaderContainer.Controls.Add(new LiteralControl(dtrow[0].ToString()));
foreach(DataRow dtRow2 in dtTable.Rows)
{
if(dtRow2[0].ToString() == idRow)
{
currentPane.ContentContainer.Controls.Add(new LiteralControl(dtRow2[1].ToString()));
}
}
NavigateAccordion.Panes.Add(currentPane);
}
}