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);
}
Related
I have ComboBoxEdit which holds multiple selected items Binding to total employees collection (Active + InActive employees). But I need to show only active employees in dropdownlist.
I tried filtering (updating) on popupOpening event it showing active employees in dropdown, but the problem is if InActive is already selecteditems, it is clearing from the combobox because changing of item source.
private void CbCraneOps_PopupOpening(object sender, OpenPopupEventArgs e)
{
var combo = sender as ComboBoxEdit;
var item = combo.ItemsSource as IEnumerable<Client.LaborMgmtSystem.RosterEntry>;
var items = item.Where(r => r.IsActive == true);
this.cbCraneOps.ItemsSource = items;
}
please need some clue on this.
thanks in advance.
ComboBoxEdit i Suppose You are using devexpress Controls.
If Yes then there is a FilterCriteria Option is there.
something like this will work more here
combo.FilterCriteria = CriteriaOperator.Parse("IsActive=true");
Ok, very dumb question but i haven't really found an answer on internet.
I have multiple comboboxes on a form. The binding of each combobox is on form_load.
When the form loads, the first item is selected on the form. This is obvious, but I don't want this. So i used in the form_load the following code:
private void InvoiceView_Load(object sender, EventArgs e)
{
// Bind list of customers to combobox
CustomerComboBox.DataSource = invoicePresenter.getCustomers();
CustomerComboBox.DisplayMember = "CustomerName";
CustomerComboBox.ValueMember = "CustomerId";
// Bind list of products to combobox
productCombobox.DataSource = invoicePresenter.getProducts();
productCombobox.DisplayMember = "ProductName";
productCombobox.ValueMember = "ProductId";
// Bind list of vat codes to combobox
vatComboBox.DataSource = invoicePresenter.getTaxCodes();
vatComboBox.DisplayMember = "taxCodeShortDescr";
vatComboBox.ValueMember = "taxCodeId";
// Set comboboxes empty
CustomerComboBox.SelectedItem = null;
productCombobox.SelectedItem = null;
vatComboBox.SelectedItem = null;
}
This works. But the textboxes are still giving me the data of the first item ? My guess is because its in the selectedIndexChanged. But i have no clue what to use else.
If I put the combobox.selectedIndex = -1; I face the same issue.
the code:
private void CustomerComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
// Bind the selected customer itemvalues to the texboxes
txtCustomerName.Text = ((tbl_customer)CustomerComboBox.SelectedItem).CustomerName.ToString();
txtAddress.Text = ((tbl_customer)CustomerComboBox.SelectedItem).CustomerAddress.ToString();
txtPostalCode.Text = ((tbl_customer)CustomerComboBox.SelectedItem).CustomerPostalCode.ToString();
txtCity.Text = ((tbl_customer)CustomerComboBox.SelectedItem).CustomerCity.ToString();
txtCountry.Text = ((tbl_customer)CustomerComboBox.SelectedItem).CustomerCountry.ToString();
txtVatNumber.Text = ((tbl_customer)CustomerComboBox.SelectedItem).CustomerCountryCode.ToString() + ((tbl_customer)CustomerComboBox.SelectedItem).CustomerVat.ToString();
}
One would think, because im using selecteditem in the textbox binding, it would be null also. But this is not the case.
Interestingly, seems like you've hit one of the WF data binding quirks. The problem is caused by the fact that the CurrencyManager class which maintains every list data source does not allow setting the Position property to -1 (thus Current to null) when the list count is not zero. Since the ComboBox is synchronizing the SelectedIndex with the CurrencyManager.Position, this effectively prevents having an unselected item.
As a workaround, if the data bound mode of the list portion is not essential for you, replace the line
CustomerComboBox.DataSource = invoicePresenter.getCustomers();
with
foreach (var customer in invoicePresenter.getCustomers())
CustomerComboBox.Items.Add(customer);
Do the same for the other comboboxes that need such behavior.
You can remove the handler for the SelectedIndex_Changed event of combobox, bind data, then add the handler back. like this :
private void InvoiceView_Load(object sender, EventArgs e)
{
this.CustomerComboBox.SelectedIndexChanged -= new EventHandler(CustomerComboBox_SelectedIndexChanged);
this.productCombobox.SelectedIndexChanged -= new EventHandler(productCombobox_SelectedIndexChanged);
this.vatComboBox.SelectedIndexChanged -= new EventHandler(vatComboBox_SelectedIndexChanged);
// Bind list of customers to combobox
CustomerComboBox.DataSource = invoicePresenter.getCustomers();
CustomerComboBox.DisplayMember = "CustomerName";
CustomerComboBox.ValueMember = "CustomerId";
// Bind list of products to combobox
productCombobox.DataSource = invoicePresenter.getProducts();
productCombobox.DisplayMember = "ProductName";
productCombobox.ValueMember = "ProductId";
// Bind list of vat codes to combobox
vatComboBox.DataSource = invoicePresenter.getTaxCodes();
vatComboBox.DisplayMember = "taxCodeShortDescr";
vatComboBox.ValueMember = "taxCodeId";
this.CustomerComboBox.SelectedIndexChanged += new EventHandler(CustomerComboBox_SelectedIndexChanged);
this.productCombobox.SelectedIndexChanged += new EventHandler(productCombobox_SelectedIndexChanged);
this.vatComboBox.SelectedIndexChanged += new EventHandler(vatComboBox_SelectedIndexChanged);
}
So I am using external API which is providing class called CatInfoType which have for example int number catid and string catname.
I have a combobox with properties
< ComboBox x:Name="listOfCategories_comboBox" ... SelectionChanged="listOfCategories_comboBox_SelectionChanged" DisplayMemberPath="catname" />
Then in MainWindow cs file I have:
1) list of this class
List<CatInfoType> displayedCategories_List = new List<CatInfoType>();
2) in constructor
var comboBox = listOfCategories_comboBox as ComboBox;
comboBox.ItemsSource = displayedCategories_List;
3) after some button is clicked then I am filling values of combobox:
foreach (var item in allCategories_list)
{
if (item.catparent == 0)
{
displayedCategories_List.Add(item);
}
}
Until now everything is fine, but I would like to change combobox items after same comboBoxSelectionChanged is called:
private void listOfCategories_comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
CatInfoType selectedCategory = listOfCategories_comboBox.SelectedItem as CatInfoType;
int selectedCategoryId = selectedCategory.catid;
int selectedCategoryParentId = selectedCategory.catparent;
displayedCategories_List.Clear();
foreach (var item in allCategories_list)
{
if (item.catparent == selectedCategoryId)
displayedCategories_List.Add(item);
}
var comboBox = listOfCategories_comboBox as ComboBox; // I think those two lines are useless
comboBox.ItemsSource = displayedCategories_List;
}
However the combobox items are not getting changed. I was trying to do it in few ways. None of them get the result.
How I may do this ? Change comboBox Items "on live". After one of those item is pressed I want to clear the list and add new items to be displayed.
I hope code above and description is showing what I would like to do. In case you have questions feel free to ask.
try use ObservableCollection<CatInfoType> instead List<CatInfoType>
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 am trying to add some data inside my DataGrid.
I added some columns with the designer. Now I want to add rows with data inside the DataGrid.
Here's my code so far:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var dataContext = new PurchaseOrderDataContext();
var purchaseOrderTable = dataContext.GetTable<PurchaseOrder>();
var query = from a in purchaseOrderTable
select a;
var purchaseOrders = query;
foreach (var purchaseOrder in purchaseOrders)
{
// I believe that this method is the right one, but what do I pass to it?
// dataGrid1.Items.Add(test);
}
}
All I want to know is: What kind of object do I need to use to add something in the DataGrid, and what kind of object do I need to pass to that last method? Also how do I add, let's say, text to a certain column of a row I added?
Thanks a lot!
In general, you would bind the ItemsSource of the grid to a collection that supports change notification (IObservableCollection is idea) and just add to the collection. If the collection supports change notification, the grid will automatically display the new row.
Try this:
dataGrid1.ItemsSource = query;