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.
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 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.
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
I've got a data binding source, and it has an associated property. When this source property is text/integer based etc, everything works fine. I can bind that to a text box or label okay.
Some of my properties are lists. This is also fine, I can create a new listBindingSource, point it's datasource at the origional binding source, and the datamember as the list and point my, say, listview at that. I set the DisplayMember to the property of the item in the list and it works fine.
However, if the property is neither list, or text/int, but just a single object... I'm stuck. There's no way of telling it I want to bind the properties's property to the object. There's no 'DisplayMember' to help.
An example:
I have an object MeteredSpace. It has the properties
public string Name {get;set;}
public List<MeteredSpace> ChildMeteredSpaces {get;set;}
public MeteredSpace ParentMeteredSpace {get;set;}
Then I define the following met
//underlying datasource
this.meteredSpaceBindingSource.DataSource = typeof(SEMS.LinqObjects.MeteredSpace);
//data source of children
this.childMeteredSpacesBindingSource.DataMember = "ChildMeteredSpaces";
this.childMeteredSpacesBindingSource.DataSource = this.meteredSpaceBindingSource;
Now for my controls:
//nice and easy, picking directly from the underlying data source
this.nameTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text",this.meteredSpaceBindingSource, "Name", true));
//our child metered space list box is pretty easy too:
this.meteredSpaceListBox.DataSource = this.childMeteredSpacesBindingSource;
this.meteredSpaceListBox.DisplayMember = "Name";
//then we come to our parent text box
this.nameTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text",this.meteredSpaceBindingSource, "ParentMeteredSpace", true));
I have no way of pointing it at ParentMeteredSpace's Name property like I do with the list box. I also can't create a nice new binding source for it.
Anyone know of a way around this? I had a look into overriding a control and adding my own DisplayMember, but I started to think there might be a better way of doing it.
Thanks
You should be able to reference through the parent property:
Binding b = new Binding("Text",
this.meteredSpaceBindingSource,
"ParentMeteredSpace.Name",
true);
this.nameTextBox.DataBindings.Add(b);
I have a binary field in my database that is hard to describe in a UI using a single "Is XXXX?"-type checkbox. I'd rather use a pair of radio buttons (e.g. "Do it the Foo way" and "Do it the Bar way"), but right now all the other fields on my form are data-bound to a business object. I'd like to data-bind the pair of radio buttons to the business object as well, but haven't come up with a good way to do it yet. I can bind one of the buttons to the field, such that the field is set "true" if the button is selected, but while selecting the other button does de-select the first one (that is, the two radio buttons are properly paired), the value of the field does not update to reflect this.
I'd like to be able to say
button1.DataBindings.Add(new Binding("checked", source, "useFoo"));
button2.DataBindings.Add(new Binding("checked", source, "!useFoo"));
but I'm pretty sure that will throw when it runs. Is there an easier way, or should I just put more thought into how to word a single checkbox? I don't want to add extra functions to handle something this trivial...
ETA: A commenter has suggested considering a dropdown (ComboBox). I had thought about this, but how would I data-bind that to a boolean field in a database/Property in a business object? If I bind the SelectedItem to the useFoo property, what would go in the Items collection? Would I have to add just "True" and "False", or could I somehow add a key/value pair object that ties a displayed item ("Use Foo" / "Do Not Use Foo") to the boolean value behind it? I'm having trouble finding docs on this.
About the answer: the solution I wound up using involved modifying the business object -- the basic idea is very similar to the one posted by Gurge, but I came up with it separately before I read his response. In short, I added a separate property that simply returns !useFoo. One radio button is bound to source.UseFoo, and the other is bound to source.UseBar (the name of the new property). It's important to make sure the new property has both getters and setters, or you'll wind up with really odd behavior.
Bind the RadioButton that is directly linked to your boolean value (ie is checked when the value is true).
Add an event handler to the CheckedChanged event on this RadioButton that looks like the following :
private void radioButton_CheckedChanged(object sender, EventArgs e)
{
foreach (Binding b in ((Control)sender).DataBindings)
b.WriteValue();
}
I have found a way of doing this using DataSet/DataTable.
I make a calculated column in the DataTable with the expression IIF(Foo=true, false, true). Let's call that column Bar.
Bar is of type Boolean. Now you can bind one RadioButton.Checked to Foo and one to Bar.
To get Bar checking/unchecking to propagate back to Foo you must go to the generated DataTable code and add one line, the last one in this sample:
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public bool Bar {
get {
try {
return ((bool)(this[this.tableradio.BarColumn]));
}
catch (global::System.InvalidCastException e) {
throw new global::System.Data.StrongTypingException("The value for column \'Bar\' in table \'radio\' is DBNull.", e);
}
}
set {
this[this.tableradio.BarColumn] = value;
this[this.tableradio.FooColumn] = !value;
}
}
If your business object implements INotifyPropertyChanged (which makes binding work nicer), you can add the following code to the visual interface where BO is your business object and is declared withevents and BO.Value is the boolean property you would like to bind to.
Public Property NotValue() As Boolean
Get
Return Not BO.Value
End Get
Set(ByVal value As Boolean)
BO.Value = Not value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("NotValue"))
End Set
End Property
Private Sub BO_PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Handles BO.PropertyChanged
If e.PropertyName = "Value" Then
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("NotValue"))
End If
End Sub
The following bindings will hook up the radio buttons.
RBTrue.DataBindings.Add(New Binding("Checked", Me.BO, "Value", False, DataSourceUpdateMode.OnPropertyChanged))
RBFalse.DataBindings.Add(New Binding("Checked", Me, "NotValue", False, DataSourceUpdateMode.OnPropertyChanged))
The visual interface should also implement INotifyPropertyChanged. This method works both ways: the original value gets updated if the interface changes and if the original value changes the interface will update correctly.
I came across the same problem, and found out it's actually possible with standard databinding:
button1.DataBindings.Add(new Binding("checked", source, "useFoo", false, DataSourceUpdateMode.OnPropertyChanged);
// no need to databind button 2, since button1.checked
// is set to false when button2 is checked
By default, DataSourceUpdateMode is set to OnValidation. By setting it to OnPropertyChanged, the change is propagated immediately to the databound object.
My business object implements INotifyPropertyChanged;
By testing this solution, I found out that my OnPropertyChanged event was fired twice when I was clicking button2. To prevent this, simply set
button1.CausesValidation = false;
button2.CausesValidation = false;
as shown here:
TextBox leave causes PropertyChanged get fired twice
Do it manually in code. When you load the form set your radio buttons according to your database. When you press a "save" button store de state as you wish. This example stores radios as bit fields in the database.
// Load radio state
radioButton1.Checked = ((myModel)myBindingSource.DataSource).boolOption1;
radioButton2.Checked = ((myModel)myBindingSource.DataSource).boolOption2;
-
// Save radio state
((myModel)myBindingSource.DataSource).boolOption1 = radioButton1.Checked;
((myModel)myBindingSource.DataSource).boolOption2 = radioButton2.Checked;
I tried to drag items from the "Data Sources" panel in Visual Studio 2015 when I defined my radio buttons as booleans in the table but there is issues with the booleans not being updated to false when another radio button is selected. When changing the "causesValidation" to false radiobuttons unchecked automatically when jumping between other text input fields.
You should only have to bind one of the controls as long as you make sure they are in the same group.
You could also consider a DropDown.