how can i remove blank checkbox item from checkbox list? - c#

How can I remove blank items from a checkbox list in asp.net? I use this code but don't know what write inside the if body to remove that item
foreach (ListItem item in chkdisease.Items)
{
if (item.ToString() == "")
{
}
}

You can try
if(chkdisease.Items.Any()) {
foreach (ListItem item in chkdisease.Items.Where(c=>!string.IsNullOrEmpty(c.ToString())))
{
//your code here
}
}

Modify your code and use this :
for(int i=0; i<chkdisease.Items.Count; i++){
if(chkdisease.Items[i].Text.ToString() == "")
chkdisease.Items.Remove(chkdisease.Items[i]);
}
As i am not sure what problem are you facing with the above code, i would suggest you to fix the issue in back-end rather front-end. As you are filling the checkboxlist from database, just use a query like below for your checkboxlist datasource :
select disease from yourtable where disease is not null and disease<>''

Related

Make new dynamic CheckBoxList as per selected items of another checkboxlist in asp.net

I am making ecommerece website where I have certain filters (5 type of checkboxlists). If user apply any items from checkboxlist the selected items should be added to new checkboxlist. Basically I want to display what users have selected. I can able to do this with following code.
if (IsPostBack) {
userSelections.Items.Clear();
foreach (ListItem item in priceFilter.Items) {
if (item.Selected) {
userSelections.Items.Add(item);
}
}
foreach (ListItem item in brandFilter.Items) {
if (item.Selected) {
userSelections.Items.Add(item);
}
}
}
With this code items get added to userselections CheckboxList but now i don't know if something from userselection gets uncheck it should be uncheck from it's main filter as well.Can any one help me to do this.
When item is unchecked from userSelections, you have to take the item value from list and loop through with priceFilter and brandFilter using userSelections CheckedChanged Event. When unchecked item value is match with the priceFilter and brandFilter item ,just uncheck from the priceFilter or brandFilter.
String unchecked_item = userSelections.item.value; (Note : unchecked item value)
foreach (ListItem item in priceFilter.Items) {
if (unchecked_item == item.value) {
priceFilter.Item.selected = false;
}
}
foreach (ListItem item in brandFilter.Items) {
if (unchecked_item == item.value) {
brandFilter.Item.selected = false;
}
}
finally remove the unchecked item from the userSelections.
Oh I think you can using JavaScript or CheckedChanged Event. When the user checks new checkboxlist, just remove it.
You need to apply this on Apply Event for checkbox, not page load event. I think you applied in Page_Load isPostback.

Checked items in a CheckedListBox

In C#, how can I determine if an item in a CheckedListBox is checked or not if I have the text of the CheckListBoxItem?
I am needing to loop through all CheckedListBoxItems, and retrieve the text and the checked state.
Here is what I have so far:
CheckedListBox.ObjectCollection items = checkedListBoxFileNames.Items;
foreach (var item in items)
{
}
I am not sure on how to determine if an item is checked or not.
Thanks in advance.
You don't need this foreach loop.
Try this:
if(this.m_CheckedListbox.CheckedItems.Contains("Item1")
{
//make an action, if it's checked.
}
if(this.m_CheckedListbox.CheckedItems.Contains("Item2")
{
//make an action, if it's checked.
}
// etc...
// this.m_CheckedListbox should be the name of your checked list box.
You can use like
IEnumerable<int> allChecked = (from item in chkBoxList.Items.Cast<ListItem>()
where item.Selected
select int.Parse(item.Value));
more Details click here
Try like this:
foreach(object itemChecked in checkedListBox1.CheckedItems)
{
//your code
}
Also check CheckedListBox.CheckedItems Property
Collection of checked items in this CheckedListBox.

Delete selected items in a GridView

IList<object> itemsSelected = MyGrid.SelectedItems;
foreach (object itemSelected in itemsSelected)
{
MyGrid.SelectedItems.Remove(itemSelected);
}
I try remove selected items from a GridView but not all selected items are removed.
Could someone help me?
object[] itemsSelected = MyGrid.SelectedItems.ToArray<object>();
foreach (object item in itemsSelected)
{
MyGrid.Items.Remove(item);
}
I had the exact problem as well. I wrote something like this but it didn't delete all items too, just some of them :
foreach(var item in MyGridView.SelectedItems)
{
MyGridView.Items.Remove(item);
}
But write this which will delete all your selected items for sure :
while (YourGridView.SelectedItems.Count != 0)
{
YourGridView.Items.Remove(YouGridView.SelectedItem);
}
there isn't exception, when you use foreach with SelectedItems? When you remove item, SelectedItems array is modified and foreach throws an exception. (Though I've tried on ListBox control). Try to use for-operator and remove items from last to first by index.
Based on your answers in the comments, I assume you are working on a C# winforms application.
Is it possible that what you are actualy using is a ListBox and not a GridView?
If that is so, you should use the ClearSelected() method which unselects all items in the ListBox.

replacing an object in listview with new one?

I have a listview with few items. I am using foreach loop to check if there is a match. The code I am using looks like this:
foreach (ListViewItem test in listView1.Items)
{
if (test.SubItems[1].ToString() == item.SubItems[1].ToString())
{
test.Tag = item.Tag;
}
}
What I am trying to do is, check the 2nd index and if there is a match replace the old item 'test' with the new one 'item'.
Apparently there is no change in the listview. Is the way I am replacing the object wrong?
you can clone the item and assign directly to the list view item. but you need to change foreach loop to for loop.
for (int i = 0; i < listView1.Items.Count; i++)
{
if (listView1.Items[i].SubItems[1].ToString() == item.SubItems[1].ToString())
{
listView1.Items[i] = (ListViewItem)item.Clone();
}
}
You have updated the Tag only. You need to change test.SubItems[0], test.SubItems[1],... to see the changes.
Or you could remove old item and insert new item by using listView1.Items.Remove(...) or listView1.Items.RemoveAt(...) and listView1.Items.Insert(...). But if you need to pay account of performance you should use the first algorithm (changing test.SubItems[i]).

Select the specific column of ListView and print it in a new messagebox in C#.net

I've just started to use ListView in C#.net.
I got to know how to add items and subitems. Going through the listview I wanted to fetch all the data from a whole column with multiple rows.
I want to know how to do this.
I found this code to list a specific selected data from a row:
ListView.SelectedIndexCollection sel = listView1.SelectedIndices;
if (sel.Count == 1)
{
ListViewItem selItem = listView1.Items[sel[0]];
MessageBox.Show(selItem.SubItems[2].Text);
}
That was helpful but i want to list all the items in a row, may be i want to add all the column items in array?
private string[] GetListViewItemColumns(ListViewItem item) {
var columns = new string[item.SubItems.Count];
for (int column = 0; column < columns.Length; column++) {
columns[column] = item.SubItems[column].Text;
}
return columns;
}
I would recommend some caution against doing this. A ListView is really meant to display information, it is not a great collection class. Getting the data out of it is slow and crummy, it can only store strings. Keep the data in your program in its original form, maybe a List<Foo>. Now it is simple and fast.
foreach (ListViewItem item in listView1.Items) {
// Do something with item
}
you could do this by
foreach(ListViewItem item in listView1.Items)
{
foreach(var subtem in item.SubItems)
{
// Do what ever you want to do with the items.
}
}

Categories

Resources