I am trying to create a gridview with list as you can see
I add the item of the list using this code :
private void frmDocument_Load(object sender, EventArgs e)
{
gridControlDocument.DataSource = new BindingList<Document>(_documentRepository.Get().ToList()) { AllowNew = true };
DisciplineList.Items.Add("ali");
}
but i need to get data from the database ,but the DisciplineList doesn't have the datasource property .
The ComboBoxEdit control is not meant to be bound to a data source. You would need to either loop through your DisciplineList collection and add each item manually, or use the LookUpEdit control, which does offer a data source property.
In your case, you can add a RepsositoryItemLookUpEdit to the GridControl (See: Assigning Editors for In-Place Editing) and set its DataSource property to your collection. Additionally, set the ValueMember and DisplayMember properties to a property within the Discipline class.
Related
So I have this radclv_peças which is a radCheckedListBox control populated with Peça custom objects and I'm trying to get the object currently selected using the SelectedItem property. The problem is I don´t know how to access these objects which I bound using the DataSource property like this:
radclv_peças.DataSource = Program.M_Wardrobe.ListaPeças;
radclv_peças.DisplayMember = "Name";
radclv_peças.ValueMember = "Id";
I need to change the image in a pictureBox according to the Peça currently selected in the listView. The idea is to get the currently selected item by ID and compare it with all Peça objects contained in Program.M_Wardrobe.ListaPeças(MVC pattern) which is of type List<Peça>, until I find the one with the same ID and send it to the pictureBox.
So, how can I access the Id, or other properties, of the items bound in the radCheckedListBox (Telerik) with the DataSource property?
Telerik's RadCheckedListBox.SelectedItem has a DataBoundItem property. This represents a specific object that the SelectedItem is bound to out of the list of objects that the RadCheckedListBox is bound to. By casting this at runtime to your object type, you can access its properties in your event handler.
private void RadCheckedListBox_SelectedIndexChanged(object sender, EventArgs e)
{
var selectedItem = radCheckedListBox.SelectedItem?.DataBoundItem as Peça;
}
Once you have the item, you are free to use it how you want from there.
I tried to set DataSource of CheckedListBox like this:
private void Form1_Load(object sender, EventArgs e)
{
checkedListBox1.DisplayMember = "Name";
checkedListBox1.ValueMember = "Checked";
_bindingList = new BindingList<CustomBindingClass>(
new List<CustomBindingClass>
{
new CustomBindingClass {Checked = CheckState.Checked, Name = "Item1"},
new CustomBindingClass {Checked = CheckState.Checked, Name = "Item2"},
new CustomBindingClass {Checked = CheckState.Unchecked, Name = "Item3"},
});
checkedListBox1.DataSource = _bindingList;
}
And It's working but partially. I'm able to do the fallowing later
_bindingList.RemoveAt(0);
or _bindingList[0].Name = "TestTest"; and CheckedListBox updates well except items are not checked. This is not working
_bindingList[0].Checked=CheckState.Checked;
I also tested to do it when CheckedProperty from my CustomBindingClass is of type bool, but doesn't works either. Any suggestion what should be the type of ValueMember property ?
Consider these facts:
CheckedListBox does't have a built-in data-binding support for checking items. You need to handle check state of items yourself.
You set checkedListBox1.ValueMember = "Checked";. You didn't set item check state, you just said when you select the item, the value which returns by SelectedValue comes from Checked property of your object which is behind the seected item. For example you can use this code in a Click event of a Button to see the result; regardless of check-state of items, the message box, will show value of Checked property of the object behind the item:
MessageBox.Show(checkedListBox1.SelectedValue.ToString());
Selecting and checking items are completely different.
I prefer to use DataGridView for such purpose. You can simply have a CheckBox column and a readonly TextBox column and bind DataGridView to the list of your objects.
If you need to have two-way data binding, you need to implement INotifyPropertyChanged interface regardless of what control you are using to show data. If you don't implement that interface, when changing properties on your model ListChange event will not raise and you can not see changes in UI automatically.
If you take a look at CheckedListBox class, you'll notice that DataSource, DisplayMember and ValueMember are marked with
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
This a common technique used in Windows Forms controls to indicate that some public properties inherited from a base class (hence cannot be removed) are not applicable for that concrete derived class and should not be used.
There must be a reason for doing that for the aforementioned properties of the CheckedListBox. As you already saw, it's "sort of working", but the point is that it isn't guaranteed to work at all. So don't use them. If you wish, create a helper class that holds CheckedListBox and BindingList, listens to ListChanged event and synchronizes the control.
I have a combobox populated via Datasource and when the application runs it shows the first item instead of the default text setup in the properties.
How can I accomplish this?
EDIT:
The data comes from an API:
loteamentos = JsonConvert.DeserializeObject<List<Loteamento>>(dataObj.Result);
and once I have the data, I populate de ComboBox:
cb_loteamentos.DataSource = loteamentos;
cb_loteamentos.ValueMember = "id";
cb_loteamentos.DisplayMember = "nome";
Because you are binding your combobox from a data source, it is going to populate the items from the data source and anything set via property pane will be overridden.
You need to add that item as first item in your data source also, to make it appear in combo.
While assigning the DataSource to the comboBox the current associated collection and hence the Default text will be changed. So Assigning default text after assignment of the DataSource will solve your issue:
// Bind the combobox
comboBox1.SelectedIndex = -1;
comboBox1.Text = "Please select any value";
On the form load:
combobox1.SelectedIndex = 0
I have a ComboBox which is bound to a BindingList.
The background is that I want to have an option to choose from the dropdown-list (as a suggestion), but I don't want to change the displayed text if the ItemsSource changes (for example, when I'm adding an item to the BindingList).
Is there a way to prevent the ComboBox to update the displayed text when the items source is changed?
Some code:
this.comboBox1.DataSource = database.ListItems; // database.ListItems is of type BindingList<string>
public void update_ListItems(BindingList<string> ListItems)
{
ListItems.Add("Item"); // Causes an update of the displayed text in the ComboBox
}
If you need to break the binding between ComboBox and data source you can convert the BindingList to List like this:
this.comboBox1.DataSource = database.ListItems.ToList();
This will prevent updating of ComboBox items when you change the BindingList
I have a ComboBox bound to a List via a DataSource. For some reason, when the datasource items change, the items in the combo box don't seem to automatically update. I can see in the debugger the datasource contains the correct items.
There are lots of answers on StackOverflow about this, but most are either unanswered, don't work for me, or require changing from using Lists to BindingLists which I cannot do this instance due to the volume of code which uses methods BindingLists don't have.
Surely there must be a simple way of just telling the ComboBox to refresh it's items? I can't believe this doesn't exist. I already have an event which fires when the Combo needs to be updated, but my code to update the values has no effect.
Combo declaration:
this.devicePortCombo.DataBindings.Add(
new System.Windows.Forms.Binding("SelectedValue",
this.deviceManagementModelBindingSource, "SelectedDevice", true,
DataSourceUpdateMode.OnPropertyChanged));
this.devicePortCombo.DataSource = this.availableDevicesBindingSource;
Code to update the combobox:
private void Instance_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "AvailableDevices")
{
// Rebind dropdown when available device list changes.
this.Invoke((MethodInvoker)delegate
{
devicePortCombo.DataSource = AvailableDevicesList;
devicePortCombo.DataBindings[0].ReadValue();
devicePortCombo.Refresh();
});
}
}
You are not binding the DataGridview's DataSource to same BindingSource object in your case this.availableDevicesBindingSource which bound first time. but later you are binding to different object AvailableDevicesList. again you are using another binding source for SelectedValue i.e this.deviceManagementModelBindingSource.
use one BindingSource only, may solve your issue