I am implementing MVC in a WinForms Application. In the view there is a combobox control. I have declare a property called SheetLoader with getter and setter:
public BindingSource SheetLoader
{
get { return (BindingSource)comboBox_workSheetList.DataSource; }
set { this.comboBox_workSheetList.DataSource = (BindingSource)value; }
}
In controller I want to access setter above and bind the BindingSource to the combobox.
view.SheetLoader = _bindingSource;
But this way is not working. The combobox will not assign any item.
I have debugged it, However the value gets data.
Please help me to bind data from controller to View -> Control.
Ensure you set the DisplayMember and ValueMember of your combobox.
Related
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 grid wrapped in a user control. I need to pass the datasource of the grid through the control.
// My Pass-Through declaration
public object DataSource
{
get { return internalGrid.DataSource; }
set ( internalGrid.DataSource = value; }
}
So far, that's the easy part. The datasource of the grid is a plain Object. The pass-through Datasource is also a plain Object.
When I drop a datagrid directly on my form, I can set datasource by selecting one of my binding source objects on my form. However, when I include my custom control, the pass-through datasource is grayed out. I can't select the binding sources.
I'm certain that I am simply missing a property attribute, but I don't know which one(s). Any help would be appreciated.
I'm looking to bind a ComboBox created at runtime to a property on the ViewModel.
I've tried something along these lines
combobox.SetBinding(ComboBox.SelectedValueProperty,
new Binding("WCSettings.ViewModels.WinCAPSIniViewModel.selectedItem")
{
Source = combobox.SelectedValue,
Mode = BindingMode.OneWayToSource
});
The binding only needs to go one way (View --> ViewModel), so the value can be stored in a database.
'combobox' is the instance of the ComboBox being created.
Binding the SelectedValue property of a ComboBox and at the same time setting the Source of the binding to the same property doesn't make sense.
You need to have an instance of the view model and use that as the binding source. And unless you also set the SelectedValuePath property of the ComboBox, you should bind the SelectedItem property.
WCSettings.ViewModels.WinCAPSIniViewModel viewModel = ...
combobox.SetBinding(ComboBox.SelectedItemProperty,
new Binding("selectedItem")
{
Source = viewModel ,
Mode = BindingMode.OneWayToSource
});
And just in case you forgot, selectedItem needs to be a public property in class WinCAPSIniViewModel.
I have a custom UserControl that contains several child controls, amongst which is a DataGridView. I don't want to EnableDesignMode for any of the child controls, but instead have exposed and serialized their properties as needed. I'm stuck on DataGridView's DataSource property.
Do I need to make a custom UITypeEditor and use reflection to find all the BindingSource objects on the parent form for selection, or can I somehow invoke the built-in editor of this type? What type is the editor invoked when changing DataGridView's DataSource?
EDIT: Actually, the suggestion from Oliver did not quite work out. I did get the list of bindable objects in the property grid when I select my UserControl and after I chose a binding source, columns of bound dataset appeared on the grid, but columns of datagridview are not serialized to designer.cs after editing the Columns collection. However, if I build a custom ParentControlDesigner and EnableDesignMode for this datagridview, I can set the binding via it's DesignerVerb, and then the Columns collection is serialized after editing.
I exposed datagridview's Columns and DataSource properties in this way
[Editor(typeof(CollectionEditor), typeof(UITypeEditor))]
[Category("Grid")]
//[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public DataGridViewColumnCollection Columns
{
get { return dgvListaBaza.Columns; }
}
[AttributeProvider(typeof(IListSource))]
[Browsable(true)]
[Category("Grid")]
public object DataSource
{
get { return dgvList.DataSource; }
set { dgvList.DataSource = value; }
}
What is the difference between the way DataSource is set when I click on the control's native designerverb and the one through exposed property? Both show the columns of the bindingSource in the grid after i choose a binding, but Columns don't get serialized in the latter case, as if there is something else I need to set when setting the DataSource.
Also, DesignerSerializationVisibility attribute on the exposed Columns makes no difference, and the column Names in the CollectionEditor are different depending on the way I set the DataSource (If it's set through native designerverb, then they are named SomeColumnDataGridViewTextBoxColumn, and if it's set through the property, then the Name property of each column is empty).
Take a look at DataSource for User Control.
I have a usercontrol that contains a datalist, and I want to set the datasource of the datalist to different things depending on what page the usercontrol is on.
So, I think what I need to do is expose a public property of the datalist that will get the datasource and set it, like so:
public datasource UserDataSource
{
get { return DataList1.DataSource; }
set { DataList1.DataSource = value; }
}
but the above obviously doesn't work. I'd then call it like this:
MyUserControl.UserDataSource = datasourcename;
and then somehow databind the datalist inside the usercontrol.
Obviously I'm kind of out of my element here, but hopefully I can get this working. Thanks for any help.
You need to use find control method to find your datalist first and then assign datasource like...
DataList dl = (DataList)yourLoadedusercontrol.FindControl("yourDatalist");
dl.DataSource = yourdatasource;
I know you have already accepted an answer, but I feel I must add my thoughts:
Your original idea was correct - you just needed to call the databind method of your datalist after setting the datasource. I truly do not feel that doing the above mentioned method is the best way. You should really just have a method or writeonly property (like you do) that takes a possible IList or IEnumerable of your custom object and binds it directly to your datalist. Your page or control that contains this user control should not be aware of your type of data control. If you change it from a Datalist to a Repeater or a GridView, you have to change it everywhere you bind to your user control.
Example:
IList<CustomClass> results = new List<CustomClass>(); //you would load your collection from your database
this.myUserControl.LoadData(results);
In your user control:
public void LoadData(IList<CustomClass> data){
this.datalist1.datasource = data;
this.datalist1.databind();
}