I am working with windows form application using c#. I have two list boxes i.e listbox1 and listbox2 and one button i.e btnall. listbox1 is bind using databind and listbox2 is empty. I want to copy all the items from listbox1 which is binded to other listbox2 which is empty. On the click of btnAll_click event.
I am trying this
private void btnAll_Click(object sender, EventArgs e)
{
listbox2.Items.AddRange(listbox1.Items);
}
but I am getting data.datarowview instead of the values.
You could loop through the items in LIstbox1 and add them one at a time like this:
foreach (var item in Listbox1.Items)
{
listbox2.Items.Add(item.ToString());
}
Would that do what you want?
Try this
listbox2.DataSource = listbox1.Items;
If you are populating the list control via the DataSource property try setting DataSource and DataMember. and dont forget :
listBox.DisplayMember = "displayMember";
listBox.ValueMember = "valueMember";
else try this:
var mylistSource = new List<string>();
foreach (var item in Listbox1.Items)
{
mylistSource.Add(item.ToString());
}
listBox2.DataSource = mylistSource;
Related
I have a listbox "listBox_users" binded to a bindingsource, and another listbox "listBox_map" which is binded also. I want to drag and drop a user from listBox_users to listBox_map.I have done this very well when i delete the bindingsource of listBox_map.
My problem is that the listBox_map don't add new items when data source property is defined:
Items collection cannot be modified when the DataSource property is set.
private void listBox_map_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.StringFormat))
{
string str = listBox_users.Text;
listBox_map.Items.Add(str); // Error here!
}
}
How can i add new items to binded listbox?
Thank you.
Instead of use
listBox_map.Items.Add(str); // Error here!
Do it:
yourBindingSourceReference.Add(str);
It will automatically reflect on your listBox_map.
MSDN - BindingSource.Add
I fix it by myself, so i deleted the datasource property and i added every row in the listbox:
var q =
from user in SundicappEntities.Instance.users
join map in SundicappEntities.Instance.user_profile_map on user.id_user equals map.id_user
where map.id_profile==id_profile
select new
{
ListBoxItemDisplayText = user.name_user + " " + user.f_name_user
};
foreach (var u in q)
{
listBox_map.Items.Add(u.ListBoxItemDisplayText);
}
I have 2 comboboxes created with Ajax Toolkit. One of them has a list of systems. I wanted to fill the other combobox with a list of subsystems whenever a system is selected. I did not use DisplayMember or ValueMember and most examples are using them.
.aspx side just in case:
<ajaxToolkit:ComboBox ID="cbox1" runat="server" OnSelectedIndexChanged="cbox1_SelectedIndexChanged" />
Is this doable with what I'm trying? Is the event I used correct for the situation?(I guess not,but other events seem to be unrelated) Let me show you the code:
protected void fillSystemCombo()
{
var sysOperations = new ModelOperations.ConstantSystem.ConstantSystemOperations();
var sys = sysOperations.GetSystemList().TransactionResultList;
foreach (var item in sys)
{
cbox1.Items.Add(item.description);
}
}
This works fine and I can see the systems in my first combobox.
This is what I tried for populating the second one:
protected void cbox1_SelectedIndexChanged(object sender, EventArgs e)
{
var subSysOperations = new ModelOperations.ConstantSubSystem.ConstantSubSystemOperations();
int index = Convert.ToInt32(cbox1.SelectedItem.Value);//i think this should get the id...
var subsys = subSysOperations.GetSubSystemList().TransactionResultList;
foreach (var item in subsys)
{
if (item.sysID == index)
{
cbox2.Items.Add(item.description);
}
}
}
sysID is the foreign key in SubSystem which is the ID of System. By the way, my SelectedIndexChanged event never fired when I was debugging the program even though I clicked on an item in combobox.
I've actually found the answer after carefully reading the parameters taken by Items.Add. It wants a ListItemso if I create a ListItem inside the loop I can finally add my items with both a Text and a Value like this:
foreach (var item in sys)
{
combo1.Items.Add(new ListItem { Text = item.description, Value = item.ID.ToString() });
}
After that I can get the index with
int index = Convert.ToInt32(combo1.SelectedValue);
I found that Items.Clear does not always clear a listbox when the listbox has been filled via a DataSource. Setting the DataSource to Null allows it to be cleared with Items.Clear().
Is this the wrong way to do it this way? Is my thinking a bit wrong to do this?
Thanks.
Below is the code I prepared to illustrate my problem. It includes one Listbox and three buttons.
If you click the buttons in this order everything Everything works:
Fill List With Array button
Fill List Items With Array button
Fill List Items With DataSource button
But if you click the "Fill List Items With DataSource" button first, clicking on either of the other two buttons causes this error: "An unhandled exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll" with "Items collection cannot be modified when the DataSource property is set."
Comments?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnFillListWithArray_Click(object sender, EventArgs e)
{
string[] myList = new string[4];
myList[0] = "One";
myList[1] = "Two";
myList[2] = "Three";
myList[3] = "Four";
//listBox1.DataSource = null; <= required to clear list
listBox1.Items.Clear();
listBox1.Items.AddRange(myList);
}
private void btnFillListItemsWithList_Click(object sender, EventArgs e)
{
List<string> LStrings = new List<string> { "Lorem", "ipsum", "dolor", "sit" };
//listBox1.DataSource = null; <= required to clear list
listBox1.Items.Clear();
listBox1.Items.AddRange(LStrings.ToArray());
}
private void btnFillListItemsWithDataSource_Click(object sender, EventArgs e)
{
List<string> LWords = new List<string> { "Alpha", "Beta", "Gamma", "Delta" };
//listBox1.DataSource = null; <= required to clear list
listBox1.Items.Clear();
listBox1.DataSource = LWords;
}
}
According to Microsoft it looks like setting the Datasource to Null then Clearing the list is acceptable.
Source: http://support.microsoft.com/kb/319927
If your listbox is bound to a datasource, then that datasource becomes the 'master' of the listbox. You then don't clear the listbox, but you need to clear the datasource.
So if the listbox is bound to LWords, you do Lwords.clear() and the listbox would be cleared.
And that is correct behaviour, because that is what being databound is all about.
If you set the datasource to null, you are basically telling the listbox that it is no longer databound. And of course as a side effect of that it becomes empty.
But depending on the situation you might not want the listbox just to be cleared, but you might want to clear the datasource and the listbox both.
Suppose you want to clear LWords via your GUI, and that LWords is the source of your listbox, you press a button and you set the datasource to null, you see the listbox becoming empty, thinking that LWords is not empty, but LWords is not empty at all, and then in this situation that would be a bug.
I've got this code that populates a ListBox when a flyout is opened:
private void flyoutOpenPhotosets_Opened(object sender, object e)
{
lstbxPhotosets.ItemsSource = PhotraxSQLiteUtils.GetPhotosets();
foreach (String pset in App.CurrentlyMappedPhotosets)
{
int lstbxIndex = lstbxPhotosets.Items.IndexOf(pset);
if (lstbxIndex >= 0)
{
lstbxPhotosets.Items[lstbxIndex].? what now?
}
}
}
GetPhotosets returns a List. That part works (the list box is populated with the appropriate string values)
The problem is with the rest of the code (the foreach block).
CurrentlyMappedPhotosets is also a List. I want matching members among the strings in CurrentlyMappedPhotosets and those in the ListBox to cause the item in the ListBox to be selected when the flyout displays.
I was hoping to do be able to do something like this:
lstbxPhotosets.Items[lstbxIndex].Selected = true;
...but lstbxPhotosets is disallowing that.
So how can I programmatically select specified ListBox items?
Use
lstbxPhotosets.SelectedIndex = lstbxIndex
I have ListView which shows images from an ImageList. Now wanted to get index of all checked images in ListView.
List<int> list = new List<int>(); // in list index of all checked images on clicking button should be saved.
private void button2_Click(object sender, EventArgs e)
{
ListView.CheckedListViewItemCollection checkedItems = lstview1.CheckedItems;
foreach (ListViewItem item in checkedItems)
{
list.add[// How can i get index of checked item ];
}
}
ListView already has the CheckedIndices property. You probably ought to use it directly, but you can get a List<> out of it with a Linq one-liner:
var list = listView1.CheckedIndices.Cast<int>().ToList();
Well, I'm not sure I understand your question completely, but you can get the index of a ListViewItem with item.Index.
ListView.CheckedListViewItemCollection checkedItems = lstview1.CheckedItems;
foreach (ListViewItem item in checkedItems)
{
// This will fill the list with ListViewItems that are checked
list.add(listview1.Items[item.Index]);
}