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;
}
}
Related
I want to remove a selected item in my combobox
I have here a code upon form load, I am filling list items on combobox from database.
private void LoadComboField()
{
//string test = "<ROOT><DATA FieldGroup=\"PAYMENT_VIEW4\" FieldDescription=\"PNAME\" Output=\"1\" Filter=\"1\" FieldName=\"PATIENTNAME\" DataType=\"STRING\"/><DATA FieldGroup=\"PAYMENT_VIEW4\" FieldDescription=\"MEMID\" Output=\"1\" Filter=\"1\" FieldName=\"MEMBERID\" DataType=\"STRING\"/></ROOT>";
ReadXMLData(XMLDOC, dsCombo);
// ReadXMLData(test, dsCombo);
dt = dsCombo.Tables[0];
DataView dv1 = new DataView(dsCombo.Tables[0]);
this.cmbField.Items.Clear();
this.cmbField.DataSource = dv1;
this.cmbField.DisplayMember = "FieldDescription";
this.cmbField.ValueMember = "FieldName";
}
Then I have this code on SelectedValueChanged
private void cmbField_SelectedValueChanged(object sender, EventArgs e)
{
DataGridViewRow GridRowLoc = this.dgvFilter.CurrentRow;
AddGrid(iRowIdx);
int iRowCount = this.dgvFilter.RowCount - 1;
//this.dgvFilter.CurrentRow.IsNewRow
//if (GridRowLoc.IsNewRow) continue;
// MessageBox.Show(this.dgvFilter.RowCount.ToString());
if (this.cmbField.Text != "System.Data.DataRowView")
{
this.dgvFilter.Rows[iRowIdx].Cells["ColumnFieldName"].Value = this.cmbField.Text;
this.dgvFilter.Rows[iRowIdx].Cells["FieldName"].Value = this.cmbField.SelectedValue;
if (iRowCount <= iRowIdx)
{
DataRow drow = dttable.NewRow();
drow["ColumnNames"] = this.cmbField.Text;
drow["FieldName"]= this.cmbField.SelectedValue;
drow["Alias"]=string.Empty;
drow["DataType"]=string.Empty;
drow["Outputs"]=false;
drow["SortType"]=string.Empty;
drow["SortOrder"]=string.Empty;
drow["GroupBy"]=string.Empty;
drow["Filter"]=string.Empty;
drow["Or1"]=string.Empty;
drow["Or2"]=string.Empty;
drow["Or3"]=string.Empty;
drow["Or4"]=string.Empty;
drow["Or5"]=string.Empty;
drow["Or6"]=string.Empty;
drow["Or7"]=string.Empty;
drow["Or8"]=string.Empty;
drow["Or9"]=string.Empty;
drow["Or10"]=string.Empty;
dttable.Rows.Add(drow);
}
else
{
int irow = 0;
foreach (DataRow dr in dttable.Rows)
{
if (irow == iRowIdx)
{
dr["ColumnNames"] = this.cmbField.Text;
dr["FieldName"] = this.cmbField.SelectedValue;
}
irow++;
}
}
CheckAlias(iRowIdx, this.cmbField.Text, dgvFilter);
checkcellvalue(this.cmbField.Text, iRowIdx);
CheckSorting();
if (bGroupBySelected == true)
{
this.dgvFilter.Rows[iRowIdx].Cells["GroupBy"].Value = "Group By";
}
this.dgvFilter.DataSource = dttable;
dsFilter.AcceptChanges();
this.cmbField.Visible = false;
}
// checkcellvalue(this.cmbField.Text, iRowIdx);
//MessageBox.Show(arr_Filter[0]);
CheckoutputEnable();
}
I have this code in SelectedIndexChanged
try
{
DataTable dt1 = new DataTable();
DataRowView oDataRowView = cmbField.SelectedItem as DataRowView;
string sValue = string.Empty;
if (oDataRowView != null)
{
sValue = oDataRowView.Row["FieldDescription"] as string;
}
//int count = dttable.Rows.Count - 1;
ComboBox comboBox = (ComboBox)sender;
// Save the selected employee's name, because we will remove
// the employee's name from the list.
string selectedEmployee = (string)sValue;
int count = 0;
int resultIndex = -1;
// Call the FindStringExact method to find the first
// occurrence in the list.
resultIndex = cmbField.FindStringExact(selectedEmployee);
// Remove the name as it is found, and increment the found count.
// Then call the FindStringExact method again, passing in the
// index of the current found item so the search starts there
// instead of at the beginning of the list.
while (resultIndex != -1)
{
cmbField.Items.RemoveAt(resultIndex);
count += 1;
resultIndex = cmbField.FindStringExact(selectedEmployee,
resultIndex);
}
// Update the text in Textbox1.
txtName.Text = txtName.Text + "\r\n" + selectedEmployee + ": "
+ count;
}
//}
catch (Exception ex)
{
}
But it throws an exception, say that "items collection cannot be modified when the datasource property is set." I don't know how to fix this exception error, I think that's my only problem when removing an item on the combobox.
Please do help me on this one. Thanks in advance!
Use a BindingSource for your DataSource and CurrentItemChanged to react of changed items in CBO:
this.source = new BindingSource();
this.source.DataSource = loDs.Tables[0];
this.cmbField.DataSource = this.source;
this.source.CurrentItemChanged += source_CurrentItemChanged;
Example for eventHandler:
private void source_CurrentItemChanged(object sender, EventArgs e)
{
System.Data.DataRowView view = this.source.Current as System.Data.DataRowView;
if (view != null)
{
System.Diagnostics.Debug.WriteLine(view[0].ToString());
}
}
You can remove an item from the source like this:
this.source.RemoveAt(this.source.Find("FieldName", "PATIENTNAME"));
Show Details: A Detailed Data Binding Tutorial
You can't modify the Items collection when it comes from / is bound to a DataSource. Instead you need to modify the DataSource itself.
To delete the SelectedItem from the DataSource you can try this:
DataRowView item = cmbField.SelectedItem as DataRowView;
if (item != null) item.Delete();
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 am having problem with GridView on row command and stack up the data in another GridView:
private List<DistributionStandardPackingUnitItems> tempDistSPUI
{
get
{
if (ViewState["tempDistSPUI"] == null)
{
return new List<DistributionStandardPackingUnitItems>();
}
else
{
return (List<DistributionStandardPackingUnitItems>)ViewState["tempDistSPUI"];
}
}
set
{
ViewState["tempDistSPUI"] = value;
}
}
protected void gvSPU_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
int packagesNeeded = prodPackBLL.getPackagesNeededByDistributionID(distributionID);
//Get selected standard packing name
int rowNo = int.Parse(e.CommandArgument.ToString());
SPUname = this.gvSPU.DataKeys[rowNo].Value.ToString();
lblSPUname.Text = SPUname;
//Get the record from view state
itemList = tempDistSPUI;
itemList = packBLL.getAllDistSPUItemByDistributionIDnSPUName(distributionID, SPUname);
gvFinalised.DataSource = itemList;
gvFinalised.DataBind();
//Save the last record to view state
this.tempDistSPUI = itemList;
}
Let's say when I first selected a row from gvSPU, it returns an itemList filled with data and display in gvFinalised. What I am trying to do is if I selected another row from gvSPU, the previous records in gvFinalised will still there and stack up another itemList from the secondly selected row instead of wiping up the record previously and display the latest itemList data.
I am using viewState but it does not work.
EDIT
protected void lbnAdd_Click(object sender, EventArgs e)
{
List<DistributionStandardPackingUnitItems> prodVariantDetail = new List<DistributionStandardPackingUnitItems>();
int packagesNeeded = prodPackBLL.getPackagesNeededByDistributionID(distributionID);
// get the last product variant IDs from ViewState
prodVariantIDList = this.SelectedVariantDetailIDs;
foreach (RepeaterItem ri in Repeater1.Items)
{
GridView gvProduct = (GridView)ri.FindControl("gvProduct");
foreach (GridViewRow gr in gvProduct.Rows)
{
CheckBox cb = (CheckBox)gr.FindControl("cbCheckRow");
//Prevent gvFinalised to store duplicate products
if (cb.Checked && !prodVariantIDList.Any(i => i == gvProduct.DataKeys[gr.RowIndex].Value.ToString()))
{
// add the corresponding DataKey to idList
prodVariantIDList.Add(gvProduct.DataKeys[gr.RowIndex].Value.ToString());
}
}
}
for (int i = 0; i < prodVariantIDList.Count; i++)
{
prodVariantDetail.Add(packBLL.getProdVariantDetailByID(prodVariantIDList[i]));
}
//Check if itemList and prodVariantDetail list contains any duplicate records
var Gdupes = itemList.GroupBy(x => new { x.id }).Where(x => x.Skip(1).Any()).ToList();
List<DistributionStandardPackingUnitItems> dupes = Gdupes.SelectMany(x => x).ToList();
prodVariantDetail = itemList.Except(dupes).ToList();
gvFinalised.DataSource = prodVariantDetail;
gvFinalised.DataBind();
foreach (GridViewRow gr in gvFinalised.Rows)
{
//Get the product packaging quantity by productName
string name = gr.Cells[1].Text;
int productQuantity = packBLL.getProductQuantityByName(name, distributionID);
TextBox tb = (TextBox)gr.Cells[5].FindControl("tbQuantity");
if (productQuantity == 0)
{
tb.Text = productQuantity.ToString();
}
else
{
tb.Text = (productQuantity / packagesNeeded).ToString();
}
}
// save prodVariantIDList to ViewState
this.SelectedVariantDetailIDs = prodVariantIDList;
}
private List<string> SelectedVariantDetailIDs
{
get
{
if (ViewState["SelectedVariantDetailIDs"] == null)
{
return new List<string>();
}
else
{
return (List<string>)ViewState["SelectedVariantDetailIDs"];
}
}
set
{
ViewState["SelectedVariantDetailIDs"] = value;
}
}
The problem is the following lines:
//Get the record from view state
itemList = tempDistSPUI;
// here itemList will be replaced
itemList = packBLL.getAllDistSPUItemByDistributionIDnSPUName(distributionID, SPUname);
First you assign tempDistSPUI from ViewState to itemList, but then you replace itemList at the next line. You need to add the elements returned from packBLL.getAllDistSPUItemByDistributionIDnSPUName to itemList instead of replacing itemList. Here's what I would do using List.AddRange Method:
itemList = tempDistSPUI;
// add the returned elements to itemList
itemList.AddRange(packBLL.getAllDistSPUItemByDistributionIDnSPUName(distributionID, SPUname));
UPDATE
To prevent duplication with the previous elements:
itemList = tempDistSPUI;
List<DistributionStandardPackingUnitItems> itemListNew = new List<DistributionStandardPackingUnitItems>();
itemListNew = packBLL.getAllDistSPUItemByDistributionIDnSPUName(distributionID, SPUname);
// get all previous IDs in a List<int>
List<int> previousIDs = itemList.Select(x => x.id).ToList();
// filter itemListNew and add the elements to itemList
itemList.AddRange(itemListNew.Where(x => !previousIDs.Contains(x.id));
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
}
}
};
I am working on exporting data and right now some fields export the value, instead of the text. So I am saving the object that returns the text and value to a list box and matching it to the value in the listbox from the object like so:
MaterialDB materials = new MaterialDB();
DropDownList listBoxMaterials = new DropDownList();
listBoxMaterials.DataSource = materials.GetItems(ModuleId, TabId);
listBoxMaterials.DataBind();
string materialString = "";
foreach (ListItem i in listBoxMaterials.Items)
{
if (i.Value == row["MaterialTypeID"].ToString())
{
materialString = i.Text;
}
}
When I use this for the i.Value it always returns "System.Data.DataRowView" instead of the actual value. I'm doing this all in code behind. Anyway around this to get it to work?
Thanks!
You need to set the DataTextField and DataValueField properties of the DropDownList. Example:
MaterialDB materials = new MaterialDB();
DropDownList listBoxMaterials = new DropDownList();
listBoxMaterials.DataSource = materials.GetItems(ModuleId, TabId);
listBoxMaterials.DataTextField = "MaterialName";
listBoxMaterials.DataTextValue = "MaterialID";
listBoxMaterials.DataBind();
string materialString = "";
foreach (ListItem i in listBoxMaterials.Items)
{
if (i.Value == row["MaterialTypeID"].ToString())
{
materialString = i.Text;
}
}