Find Object in List which is Data Bound to ComboBox - c#

This question has probably already been answered, but i couldn't find a solution during 30 minutes of research.
The situation is as follows:
I am working on a C# Windows Forms project.
I have a Form called 'viewMain'.
Also these two classes are important:
With the winforms designer i added two bindingSources to the Form. One for objects of Auftrag and one for objects of AuftragErp. The DataSource for auftragBindingSource is set to an Instance of Auftrag. The DataSource for auftragErpBindingSource is set to the AuftraegeErp property of the Auftrag-Instance.
Now i can begin describing the problem. The ComboBox Auswahl on the Form is bound to auftragErpBindingSource and therefore always reflects the objects in the AuftraegeErp list. When i click the button on the form i want to get a reference to the currently selected object in the combobox.
I found that this can be achieved by using uftragErpBindingSource.Current.
But since the BindingSources are private i can't access them from the Auftrag Instance.
And i don't want to get this reference in an eventhandler of the form, since then i still would have to pass it on to the Auftrag instance. And if i where to do that, then i feel as though i could go without data binding altogether. The main reason for me to use data binding is not having to worry about keeping the objects and the gui synced.
So basically i have a list in a class. This list is bound to a comboBox. From inside of the class i want to get the object from the list which is currently selected in the comboBox.
I am thankful for every answer and hope that somebody can help me.
Edit for clarification: This Drawing shows the relation between the classes and the GUI. After the user has made his selection with the combobox he can click the Button. The onClick eventhandler then calls the 'doSomething' method. This method needs the selected object to function. There are multiple ways how the method can get the necessary object. I have shown two in the drawing. a) The method gets the objects from the list in its own class, or b) The method gets the object by using the .Current Property of the bindingSource on the Form.
I want to use approach a, because then i wouldn't have to pass any Variables or Objects between the Model and the View. Data binding alone would make sure that the GUI is always up to date. But i don't know how to implement approach a.

Related

C# Class that can return or be multiple types of controls

I am building a class that displays either a combobox or a listview (more to follow in the future). Part of the class is a list called OptionList that holds all the values. In the class are methods to update the list and read the list and set the displaystyle (combobox or listview). The List will be bound to either the combobox or the Listview. During runtime the displaystyle can be changed.
I am having a hard time figuring out what the best setup for this would be, should I add a Control member in the class that can be either the listview or the combobox or are there better ways to implement this?
In the calling class I simply want to show the control and update / read the list without worrying what the actual control is
You might be better thinking about the MVC pattern. The "View" would be a wrapper around the particular control you are using. The update methods could move to the controller and the raw data lives in the model.
Support for listview or combobox would be handled by different implementations of your view interface.

Making a grid of objects

I'm trying to make something like a quiz application where 3 questions will be brought up on screen at a time, allowing the user the check a radio button containing "Yes" or "No", and have an answer come up appropriately to his response. The questions will continually come from a database I'm using.
After a few attempts I've figured that using x:Name property is not a great solution, since it doesn't allow me to use a loop in order to change the questions and answers. Is there any other way to make a grid with the same types of objects in each cell, being able to access each object inside each cell of the grid in the code-behind?
Here is list of steps you need to implement,
Need to create QuestionModel, contains question properties, make sure your model inherits INotifyPropertyChanged.
Need to create ViewModel, which contains data objects, public/dependency properties
Need to bind/set data objects/properties on viewmodel constructor
Need to set your ViewModel as a DataContext of your View(.xaml) (You can create this on zammel directly and codebehind as well
Need to bind your UI objects like Question/answers/yes-no with viewmodel properties accordingly
WPF/Silverlight has their own fundamentals like Data Binding, Resources, Compiler, Dependency Properties. Above steps comprises MVVM design pattern. During each steps, please google for specific stuff.

Copy data from DataGridView

I have two forms having each one a datagridview with the same columns; and I need to copy the same data from the DataGridView in form1 to the DataGridView in the form2.
Any ideas ?
Thanks.
Once you have parsed your data from the text file, ensure that it is in an IEnumerable format - i.e. an array or List of strings or your custom data objects, let's call it myListOfStuff.
You can then bind this to as many DataGrids as you like. As you will be binding the same object reference to multiple DataGrids, any changes you make to myListOfStuff in one form will be visible (but not necessarily rendered) to anything else using myListOfStuff in another form.
How you pass myListOfStuff around depends upon whether you are using Web or WinForms.
If you are using WinForms then you can pass myListOfStuff between forms in several different ways - you could have a property on the forms which you assign the variable to, or you could pass it in on a constructor of the form, etc etc.
If you are using WebForms, then the simplest way to pass myListOfStuff between forms (pages) is to store it in Session, and both pages can access it from there. Or you could store it in Cache and access it via a static method.
I like slugster's idea and yes you could have a List of objects that would have a type of "myListOfStuff" that could in fact have properties that could hold the information you are parsing from your text file. Each grid would then be able to bind to perhaps a generic list, List and the fields mapped to the properties represented by the myListOfStuff class.

Silverlight: Use a collection for multiple options in a Data Form?

I have a data form that is working pretty well. One of the properties of my data object is an ObservableCollection. Right now, in the data form, it only displays the ToString() of the colletion. Not very helpful.
I'd like some sort of autocomplete box with valid options, and when the user selects them they will be added to the data object's collection. (I discussed doing that in this question, but it's not using data forms.)
Is there a way to do this, while using the rest of the controls automatically generated by the data form? I'd rather not specify everything myself, just to get this one control.
I'd also like to override a normal text input box for a field to make it an autocomplete box.
You need to bind to a public property of the object in the collection.

C# How do I use an event to get my GUI update on change of an object?

C# How do I use an even to get my GUI update on change of an object?
I have a GUI program that creates an object and displays the object in a data grid through reflection.
When the user updates the information I want to be able to verify the new information and send feedback to the user. I have a method that does the verification of the information, I just need to figure out how to update the GUI with the new information.
thx.
Another general approach would be to support IObservable on your object, and IObserver on any classes (such as user interface elements) that wish to be notified of changes to your object. You can have any number of observers of changes on your object. It's a little more work than the "out of the box" data binding on controls such as data grids, but I would say more flexible.
Perhaps you could be more specific or show some code, but check that each Column Object of the .Net Datagrid has a property named DataPropertyName, which binds by reflection to the property of your objects, it should work..
Other thing is to implement the INotifyPropertyChanged on your objects, and Refresh the Grid on the PropertyChanged event.

Categories

Resources