How to Remove an dropdown Item by finding Text - c#

I am databinding my dropdown in the code behind as such
AttributeStatusDdl.Items.Clear();
AttributeStatusDdl.DataSource = StatusDs;
AttributeStatusDdl.DataTextField = "AttributeStatus";
AttributeStatusDdl.DataValueField = "AttributeStatus";
AttributeStatusDdl.DataBind();
Now I would like find any items which is a string 'Test' to be removed... How can i achieve this task..
I have tried using findByText but somehow not able to remove the items with text Test... Thank you in advance
myDropDown.Items.Remove(myDropDown.Items.FindByValue("Test"));

Your code should work, try this one
for(int i= AttributeStatusDdl.Items.Count -1; i>= 0; i--)
{
if (AttributeStatusDdl.Items[i].Text == "a")
AttributeStatusDdl.Items.RemoveAt(i);
}

Related

Find Multiple Selected ListView Item Indices in UWP App

I have a ListView named sandwichToppings which displays various sandwich toppings and allows the user to select multiple. In my controller code, I must capture the selected toppings by their index in the ListView, and send those indices back using an array.
The code which causes me to stumble is visible below. I have not been able to figure out the part which captures one or more toppings to the sandwich (the rest works fine).
void readSandwichSelection()
{
int[] toppings = null;
// One or many toppings were added to the sandwich.
if(toppingsAvailable.SelectedItems.Count > 0)
{
toppings = new int[toppingsAvailable.SelectedItems.Count];
int toppingIndex = 0;
for(int i = 0; i < toppingsAvailable.Items.Count; i++)
{
ListViewItem test = (ListViewItem)toppingsAvailable.Items.ElementAt(i);
if(test.IsSelected == true)
{
toppings[toppingIndex] = i;
toppingIndex++;
}
}
}
// No sandwich topping.
else
{
order.addSandwich(sandwichesAvailable.SelectedIndex + 1);
}
}
Along my journey, I have tried a couple solutions found on Telerik. The first:
foreach (ListViewDataItem item in radListView1.SelectedItems)
{
int example = radListView1.Items.IndexOf(item);
}
The above example fails to work because
ListViewDataItem does not exist.
When replaced by ListViewItem, a run-time error occurs: System.InvalidCastException: 'Unable to cast object of type 'System.String' to type 'Windows.UI.Xaml.Controls.ListViewItem'.'
The second attempted solution:
for (int i = 0; i < radListView1.Items.Count; i++)
{
if (this.radListView1.Items[i].Selected)
{
int example = i;
}
}
This solution cannot work because the .Selected property simply does not exist.
In all my hour of attempted work, I come up with either of these two problems. Either some form of cast exception occurs, or it is impossible to reach the property.
In my example above, I did have success in reaching the isSelected property by making a copy of each list item, and testing whether it had been selected. However, although Visual Studio lets me make the assignment
ListViewItem test = (ListViewItem)toppingsAvailable.Items.ElementAt(i);
this statement cannot run at compile time, causing an error as written previously. Which datatype other than ListViewItem must be used to count over list view items?
toppingsAvailable.SelectedItems will return a List<> of selected items. However, how could I know from this list which list index selected items belong to?
You can try the following code to get the selected toppings's indexes and put the indexes in an array. The indexes will be saved in the ToppingArray.
void readSandwichSelection()
{
int[] ToppingArray = new int[toppingsAvailable.SelectedItems.Count];
for (int i = 0; i < toppingsAvailable.SelectedItems.Count; i++)
{
var selectedIndex = toppingsAvailable.Items.IndexOf(toppingsAvailable.SelectedItems[i]);
ToppingArray[i] = selectedIndex;
}
}
Please try the following. It is Selected rather than IsSelected.
for (int i = 0; i < toppingsAvailable.Items.Count; i++)
{
var test = toppingsAvailable.Items[i];
if (test.Selected)
{
toppings[toppingIndex] = i;
toppingIndex++;
}
}

Copy entire column from listview to another listview with same column format

I am trying to have 2 listviews that in the end will act as mirrors of each other. In the first listview I am able to populate it from a DB with no issues. I have another list view that when a row is checked in the first listview(listVariants) the user can move the entire row to the second listview (addFCList). With the code below it is working, sort of. The issue I am having is when another item is checked from listVariants, the way it is added to addFCList is inline and it just wraps. each item and subitem is being added to the listview as an individual item.
I have both listview cloumns setup the same. My question is how to I get the second listview(addFCList) to be the same as the the first listview(listVariants)? Am I missing something obvious...
EDIT I have solved this but will leave it here to help someone out in the future.
for (int i = 0; i < listVariants.Items.Count; i++)
{
if (listVariants.Items[i].Checked)
{
string UNI = listVariants.Items[i].SubItems[9].Text;
bool nameInsert = true;
for (int t = 0; t < addFCList.Items.Count; t++)
{
if (addFCList.Items[t].SubItems[9].Text == UNI)
{
nameInsert = false;
break;
}
}
if (nameInsert)
{
addFCList.Items.Add((ListViewItem)listVariants.Items[i].Clone());
}
}
}
Thanks for any help and suggestions.
listView1.Columns.AddRange((from ColumnHeader item in listView2.Columns
select (ColumnHeader)item.Clone()).ToArray());

get value based on index dropdown

I need to select a value from dropdown based on index. It is a super easy question. cannot find the property:
I though doing something like:
dll.Items[index]
But still do not know how to get value for this index.
You can use dll.Items.FindByIndex(index); or dll.Items.FindByValue(val); according to your needs.
This loops through all items of an ASP combobox:
DataTable dt = (DataTable)comboBox1.DataSource;
for(int i = 0; i < dt.Rows.Count; ++i)
{
string displayText = dt.Rows[i][comboBox1.DisplayMember].ToString();
string valueItem = dt.Rows[i][comboBox1.ValueMember].ToString();
}
can be done like below
dll.Items[index].value
btw, FindByIndex, is like not available

Finding an item in asp.ListView with Value when paging is enabled

I'm trying to find the selected Item in an aspx ListView that is on a separate page, then switch the page and select the item. I have the value property of the ListViewItem I am looking for, but cannot seem to get it to work. Here is what I tried:
for (int i = 0; i < lvProject.Items.Count; i++)
{
if (((Label)lvProject.Items[i].FindControl("Project_IDLabel")).Text == project.ToString())
{
lvProject.SelectItem(i);
break;
}
}
So lvProject is my list view. The project Variable is an Int64 which represents the UID of my Project. This is also the Value of my ListViewItems. The problem with the code above is that when paging is enabled, and the item is on a different page this will not work because the listView.Items.Count is set to the # of Items on the current page only.
My goal is to find the item, set the listview to display the correct page, and finally select the item. You would figure that I could just set the SelectedValue property, but this is not that simple as it is read only. Any ideas would help greatly, thanks in advance.
--Roman
In order to get the total record count from the object data source, you should use the Selected event as follows:
protected void ObjectDataSource1_Selected(object sender, ObjectDataSourceStatusEventArgs e)
{
// Get total count from the ObjectDataSource
DataTable dt = e.ReturnValue as DataTable;
if (dt != null) recordCount = dt.Rows.Count; // recordCount being declared outside the method
}
You would then be able to search for the item as follows:
for (int i = 0; i < recordCount; i++)
{
Label lblItem = (Label)lvProject.Items[i].FindControl("IdLabel");
if (lblItem.Text.Equals(itemToSearch))
{
lvProject.SelectedIndex = i;
break;
}
}
Hope it helps!
How do you bind ListView Items?
If you are using database level paging (stored procedure, query) - you should do search in the same way - using database query/stored procedure by passing a search criteria.
If you are bind ListView Items to a collection of items which are provided by a business/data layer - you have to create search method on layer which provides items so this method will be able to loop through the items.
You should set the SelectedIndex property to i
for (int i = 0; i < lvProject.Items.Count; i++)
{
if (((Label)lvProject.Items[i].FindControl("Project_IDLabel")).Text == project.ToString())
{
lvProject.SelectedIndex = i;
break;
}
}

Populating and deleting items from a database table by looping through checkedlistbox

What I am trying to do is populate a second checkedlistbox based on the selected items in the first checkedlistbox, and remove the items from the database when the parent is unchecked in the first box. I am able to populate the second box by looping through only the checked items, however, I need to include the unchecked items as well if I am to delete them from the table. Here is the code I have at the moment:
for (int i = 0; i < ckbObjectives.Items.Count; i++)
{
objectiveTableAdapter.ClearBeforeFill = false;
if (ckbObjectives.GetItemChecked(i))
{
this.objectiveTableAdapter.FillByParentObjective((CWSToolkitDataSet.ObjectiveDataTable)cWSToolkitDataSet.Tables["ChildObjectives"], Convert.ToInt32(((DataRowView)ckbObjectives.Items[i])[ckbObjectives.ValueMember].ToString()));
}
else
{
this.objectiveTableAdapter.Delete((CWSToolkitDataSet.ObjectiveDataTable)cWSToolkitDataSet.Tables["ChildObjectives"], Convert.ToInt32(((DataRowView)ckbObjectives.Items[i])[ckbObjectives.ValueMember].ToString()));
}
}
cblSubObjectives.DataSource = cWSToolkitDataSet.Tables["ChildObjectives"];
cblSubObjectives.DisplayMember = "Title";
cblSubObjectives.ValueMember = "ObjectiveID";
I am not getting any errors, however the second checkedlistbox isn't getting populated. Any help would be greatly appreciated. Thank you!
Assuming you're checking the right things, this should be working.
Can you double check which CheckedListBox you're looping through, and ensure you're getting the right responses:
for (int i = 0; i < ckbObjectives.Items.Count; i++)
{
MessageBox.Show(String.Format("{0}: {1}",
ckbObjectives.GetItemText(ckbObjectives.Items[i]),
ckbObjectives.GetItemCheckState(i).ToString()));
}
I'm still not sure if you're on WinForms/WebForms/WPF etc, but replace the MessageBox.Show above with whatever is best to output. This will at least assure you you're looking at the right, current, data.
After setting DataSource property, usually you need to call DataBind() on the CheckedListBox in order to show the data in it. Does it help?

Categories

Resources