Use DataBinding to maintain the alphabetic short between listBoxes - c#

I've been programming some little project in Visual Studio using c# (.NET FRAMEWORK) and in some of my windows forms I want to show in different lisboxes the information about some object.
In my program I got a class named Client that has some Properties: an int called DNI (this is the one that identifies a Client), a string called Name and an int called Telephone. I want a form to show 3 different listboxes, each one with the list of elements of a collection of Client objects. The point is that a Client object has it's Properties showed in the same index of each listBox (to be said, reading horizontally you see the three values of the properties). I have implemented a button in top of each one that alphabetically sorts the elements of the listBox, but I would like to make a Binding between them so When you alphabetically sort one listbox, the other two sort their elements to match the new order of elements in the one alphabetically sorted.
I've been told the class DataBindingscan be used to do this. I've tried searching on the internet about it but achieved nothing and reading the documentation didn't help either, so I ended up posting it here. How can I use DataBindings to solve this? Any help or hint will be appreciated, thanks in advance.

What must I write to bind the indexes of them?
I recommended a datagridview; you seem to have tabular data that you want to show in a tabular fashion.. However, we'll demo all of it.
Easiest route:
Make a new winforms project
Add a DataSet type of file, open it, right click the design surface and choose Add .. DataTable
Name the table Client
Right click it and add column, call it DNI, use the props grid to make its type Int32
Add a string column Name
Add an int column Telephone
Save, switch to Form1 designer
Open Data Sources window (View menu, Other Windows)
Drag the Client node onto the form. A datagridview appears along with some stuff in the tray. Remove the navigator; it's not so useful in this context
Add 3 listboxes to the form
For each listbox:
Use the props grid to set its DataSource to the bindingsource
Set the DisplayMember to a different column - one DNI, the other Name and the third Telephone
That's it for now, run the app. Type some data in the datagridview. Note that when you sort by clicking the DGV header, the listboxes follow; this is because the sort instruction is being carried out by the bindingsource, and all controls bind through the bindingsource:
So that's thngs bound through a bindingsource to a strongly typed datatable, which is conceptually not really any different to what you have. The bit of work making the table and its columns in the design surface is the notional equivalent of making a class with properties. Indeed if you were to open the DataSetBlahBlah.Designer.cs file you'd find a class ClientRow with 3 properties DNI, Name, Telephone
You could change up everything you've made to be based on e.g. a List<Client> but your sorting life becomes slightly more awkward because a BindingSource doesn't understand how to sort it. As such you'd end up with something like:
//class property
private List<Client> Clients = new();
//in constructor
clientBindingSource.DataSource = Clients;
//in button that does sorting:
clientBindingSource.DataSource = Clients.OrderBy(c => c.name); //etc
Personally I'd leave it as a strongly typed datatable; for what you'd want out of a List<Client> a ClientDataTable collection of ClientRow are surface similar enough..
Clients.Where(c => c.Name == "John"); //how you might search a List<client>
ClientsDT.Where(c => c.Name == "John"); //how you might search a ClientDataTable
Clients.Add(new Client(){ DNI = 1, Name = "a", Telephone = 1 } ); //add to a List<Client>
ClientsDT.AddClientsRow(1, "a", 1); //how you might add to a ClientDataTable
etc

If you have already implemented a button for each, you've almost finished.
Every ListBox have to be bound with a List of Client, showing a specific property as DisplayMember.
The button of each ListBox can sort the List and refresh all of the ListBoxes.
If you want more information, please post some of your code.

Related

Correct way to bind a GridLookUpEdit to a XtraGridColumn

I want to do the following:
I have an XtraGrid (short Grid) on top of an Dialog to choose e.g. offers.
A offer has a dealer from a list of all dealers.
'offer' and 'dealer' are XPO Objects.
In the lower part there is a GridLookUpEdit (short LookUp)
Maybe I'm totally in a wrong way, because we used another data mapper before, but I'm completly stuck.
I have defined an XPCollection which is bound to the Grid and another XPCollection bound to the LookUp.
If I select a row in the Grid, values of Lookups which are not XPO-objects get changed according to the selected row.
As soon as the LookUp contains an XPO Object nothing is displayed. Session of all XPCollections are the same.
If i click in the LookUp, a list of all Dealers is shown.
If i select an entry I do the following:
Order order = gridView.GetRow(gridView.FocusedRowHandle); => shows an order
Dealer actDealer = this.gridLookUpEditDealer.EditValue as Dealer; => shows a Dealer
order.Dealer = actDealer; => lets the selected value vanish.
this.gridLookUpEditDealer.EditValue stays the same, but is no longer displayed.
The DataBinging of the LookUp is:
this.gridLookUpEditDealer.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this.mainGrid, "Dealer", true));
this.gridLookUpEditDealer.Properties.DataSource = this.dealers; // dealers = XPCollection
If i try:
this.gridLookUpEditDealer.EditValue = current.Dealer;
The LookUpEdit shows the value, but also "Value not valid"
I think the root cause is something that maybe the value in the Grid is not correctly bound as value in the LookUp. But what would be a correct way?
I think the problem is, that you directly binding the whole Dealer. Set up your GridLookUpEdit as described in DevExpress Documentation.
So you should do following for your LookupEdit:
Set the LookupEdit.ValueMember = "DealerId";
Bound the EditValue to this DealerId.
Set the LookupEdit.DisplayMember = "Name"; //Or sth. you like to show
Set your LookupEdit.DataSource = YourDealer-List
With something like this you get your DealerObject back:
Dealer dealer = GridLookUpEdit.Properties.GetRowByKeyValue(lookUpEdit.EditValue) as Dealer;
Because the EditValue will be your DealerId now. Further notice that you can use repositoryGridLookupEdit directly in your Offer Grid. So you don't need more then one Grid to show your information in a usable manner. Each offer is able to has a own Dealer-Column which holds a dealer. The repositoryLookupEdit allows you to edit the dealer within the Grid.
I hope this will help you. If not clarify your problem, i will assist you ;)
Just another way to achieve the same result. With XPO, if you change the binding code as shown below, the GridLookUpEdit will update the order.Dealer property automatically.
this.gridLookUpEditDealer.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this.mainGrid, "Dealer!", true));
I only added the exclamation sign behind the property name and can now remove this code:
Order order = gridView.GetRow(gridView.FocusedRowHandle); => shows an order
Dealer actDealer = this.gridLookUpEditDealer.EditValue as Dealer; => shows a Dealer
order.Dealer = actDealer; => lets the selected value vanish.
This feature was introduced specially for look-up editors and is described in this article: Property Descriptors.

Disassociate row selection between two devexpress gridcontrols that are bound to the same bindingsource

I have created a form that has a GridControl in it that is bound to a BindingSource upon instantiation. I intend to have the user be able to have multiple instances of this form open at the same time, while being able to have different filters applied on the separate views. This all currently works great. One thing I'd like to be able to do is disassociate the row selection between the forms. When I click a row in one GridControl, the same row is selected in all other instantiations of the form.
Is there a way to do this? I don't want to create a copy of the BindingSource because having to manage updates to any of the sources across all of them is going to be a real mess considering the complexity and size of the data to begin with. Can I have multiple GridControls bind to the same BindingSource and be able to select rows independently between them?
Update:
I call this method in my Forms constructor:
public virtual void UpdateDataSource()
{
if (_dataFeatures != CurrentInspectionFile.BoundFeatureList)
{
gridControl1.BeginUpdate();
_dataFeatures = CurrentInspectionFile.BoundFeatureList;
DetachEventHandlers();
AttachEventHandlers();
gridControl1.EndUpdate();
}
SetFeatureDataBindings();
gridControl1.DataSource = _dataFeatures;
UpdateLookupLists();
UpdateGridColumns();
}
_dataFeatures is set to a BindingSource object that is constructor in the getter for the BoundFeatureList property. Each Feature is a very complex object in and of itself.
Thanks

edit multiple rows selected in a GridControl in TextEdits bound to the same bindingsource

My setup:
C#.Net 4.0, Windows Forms, DevExpress 13.1.5 although I doubt its a DX issue
I have a form with a GridControl (with GridView) on the top and a detail area that holds TextEdits and other edits in a LayoutControl below.
Both the grid and the edits below are bound to the properties of the objects contained in a list in the binding source.
The grid is set to ReadOnly, MultiSelect, RowSelect and all its columns are set to ReadOnly, not focusable.
Editing is only happening in the detail area below.
The behavior I want to create:
When multiple rows are selected in the grid the edits below should show the following:
the value of field in question is the same in all selected rows -> show the value
the value of the field in question differs between the rows -> show nothing
if the user writes into the TextEdit while multiple rows are selected:
the value of the edit should update the values of the same field of all the selected rows
Where I am with this:
I'm working on a solution by building a custom BindingSource that would be aware of the selection. It would bind the list of object to the grid and a single object that is not part of the list to the edits. Depending on the selection I would set the properties of that single object or forward its changes to the selected objects in the list.
I got that working for a single property with 2 binding sources and would now extend it to use reflection to do this for all of the public properties. I also want to encapsulate the whole behavior into a class that looks and acts like BindingSource just with that added behavior.
The question:
Is there a simpler way to achieve this? Does something already exist which can do this that I overlooked in either .Net or DevExpress? Are there traps to my approach that I should consider or why I should go about this totally differently?
I think that you can achieve your goal in a simpler manner:
Just bind a single BindingSource with all the data that you need to your grid. That should display the data.
Then, bind the required fields from that same BS do the edits through the DataBindings propriety.
You can then implement a save object (through a control or programatically) so the changes made in the edits are shown in the grid.
To check the grid values, you can use:
//get the handles of the rows
gridView.GetSelectedRows();
//get the value of the desirable cells
gridView.GetRowCellValue(handle, column);
Also, in future projects, consider using the Entity Framework to construct a data-aware model and custom objects based on the elements of your Database.
Hope this helped!

How To Hide ListView ColumnHeader?

I am struggling to figure out the correct control to use for a list of predefined jobs in the included form. I currently have a ListBoxControl in the Predefined Job Name group that lists all of the predefined jobs for a marine service shop (i.e. oil change, tune up, etc...). Then, based on the item (i.e. job name) that is selected in my ListBox, I need to display the items that correspond to that job. For example, if oil change is the selected job I need to show 4 quarts oil, 1 oil filter, labor, etc...and so on.
Currently, when I load the form data I have a DAO that retrieves all of my jobs from the database using LINQ to SQL. Then I iterate over the results and put the job names into the ListBox. The problem that I am having is that there is no tag for ListBox items like there is for ListView items. So each time the user selects another item in the ListBox, I have to perform another LINQ query to get the job from the database again so that I can display its' corresponding items. If I could use a ListView and hide the column header I could set the entire job on the tag so that each time the user selects a new item I would have access to the details without having to make another call to the database. Is there a way that I can hide the column header of a ListView without hiding the entire column?
You can set the HeaderStyle member of the ListView to None.
listView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
Checkout the ListView HeaderStyle property. It has the following options:
None
Nonclickable
Clickable
From MSDN:
The HeaderStyle property allows you to specify whether the column headers are visible or, if they are visible, whether they will function as clickable buttons. If the HeaderStyle property is set to ColumnHeaderStyle.None, the column headers are not displayed, although the items and subitems of the ListView control are still arranged in columns
You can also create simple object like ListItem which has two poperties: Text (string) and Tag (object). Then implement ListItem.ToString() and you can use these in the ListBox as well.
You can also check out Better ListView Express component, which is free and allows displaying items in Details view without columns. The advantage over ListBox and ListView is a native look and many extra features.
Easy way is using the ColumnWidthChanging event
private void listViewExtended1_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
{
if (e.ColumnIndex == 0)
{
e.Cancel = true;
e.NewWidth = listViewExtended1.Columns[e.ColumnIndex].Width;
}
}
I found that if you know for a fact you are not displaying the headers it may be best to set the HeaderStyle property to None, as Rajesh mentions above.
When setting in the .CS when screen initially loads the headers are displayed until screen is fully rendered.

How to keep properties of separate collections in sync?

I have a WPF application with a screen containing a tab control with two tabs. On each tab is a datagrid, each one bound to an ObservableCollection of Part objects. The part has a few "quantity" properties, which need to be synchronized between the grids. For example, if the user changes the quantity of partABC on grid1, partABC either needs to be added to grid2 with the same quantity, or if grid2 already contains partABC, then its quantity must be changed to reflect grid1.
My problem is that this must work in both directions. If I set a PropertyChanged handler on every part in both grids, I end up with an infinite loop as they constantly update each other's quantities. Up until now, I was handling this during the tab control selection changed event, just iterating through one of the lists and setting quantities one-by-one. This worked well enough until I realized that the users could potentially add thousands of parts to their lists, and at that point this process takes an unacceptable amount of time to complete (around 25 seconds for 4500 part objects).
edit
The first grid contains every part in the database, serving as sort of a "pick-list" where users simply scroll to the part they are looking for and enter quantities. The second grid contains only parts which have been manually entered by the user, in the event that they prefer to type in the numbers of the parts they want. Grid2 is always a subset of grid1.
You can accomplish this through databinding. You should not create duplicate Part objects. Instead duplicate the collections that hold the parts.
Part sharedPart = new Part();
Part onlyInTabA = new Part();
Part onlyInTabB = new Part();
ObservableCollection<Part> tabAParts = new ObservableCollection<Part>() { sharedPart, onlyinTabA };
ObservableCollection<Part> tabBParts = new ObservableCollection<Part>() { sharedPar, onlyInTabB };
Now use tabAParts to databind to the grid on tab A and tabBParts to databind to the grid on tab B
If your Part class implements INotifyPropertyChanged then changing a property of sharedPart will update both grids on both tabs. When you add a new part you can choose to make it shared (add it to both collections) or to keep it tab-specific

Categories

Resources