Correct way to bind a GridLookUpEdit to a XtraGridColumn - c#

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.

Related

Use DataBinding to maintain the alphabetic short between listBoxes

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.

How to display selective items in a binding list in grid control

I have implemented below code:
gridControl.DataSource = CusColumnList
CusColumnList is of type MyBindingList which inherits BindingList, in my case T is class MyColumn. The binding works well.
But now my problem comes, I don't want the data source to bind to every column in CusColumnList, I only want it binds to column whose name contains "ABC" or whose display name contains "XYZ". I tried to set
gridControl.DataSource = CusColumnList.Where(column => column.Name.Contains("ABC") || column.DisplayName.Contains("XYZ"));
But seems it does not work.
I also tried to create another bindinglist collection MyTempCusColumnList of type MyBindingList, and in the Get method of this MyTempCusColumnList, I just return every item in CusColumnList where the name or display name qualifies. But in this way, every time when CusColumnList is updated, I need to manually update MyTempCusColumnList.
I wonder whether there is a better way to archive this goal with just CusColumnList.
Thanks!
Edit : format code
You could use a filter string on a BindingSource object.
Check out the MSDN documentation on it, it's quite good: http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.filter(v=vs.100).aspx

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.

DataView.Table is not obeying DataView.Sort rules

I have a DataView called FubarView, which was created by a call to our database. The columns are Label, Value, RawName & PhoneNumber. After creation of the DataView, I added a sort order to the DataView with...
this.FubarView.Sort = "RawName, Value"
I then (amongst other irrelevant stuff like setting DisplayMember, etc.) bound it to my WinForms ComboBox...
cmbDefault.DataSource = this.FubarView;
This worked perfectly, with the ComboBox, displaying sorted information as intended. HOWEVER, when at a later point I tried to look at FubarView using the SelectedIndex from my ComboBox...
phoneNumber = this.FubarView.Table.Rows[cmbDefault.SelectedIndex]["PhoneNumber"]
...it would return the wrong value, as if FubarView went and sorted itself by Value again! How do you fix this?
This is because you are sorting a view on the table, not the actual table. So if you access the Table trough DataView.Table you get your original data.
If you want to access the sorted rows you should access them trough the DataView.
I wouldn't work with indexes, I would use IDs instead.

Different Value Member, same control

Edit 1
I believe my problem stems from the following. The function that fills the dropdown portion sets the Display Member to the CountyName. Then when I try and set the SelectedText or EditValue, as has been suggested, that function only returns the CountyID which it try's to match to something in the DropDown list DisplayMember. I need it to match it to something in the ValueMember list.
Using the following I got it to work but it is a HACK and I'd greatly appreciate finding a real solution.
lkuResidenceCounty.ItemIndex = Convert.ToInt32(row["ResidencyCountyID"].ToString());
Original Post
I have a lookup box(DevExpress) on a member form that I fill the possible values in from the DB with this code -->
lkuResidenceCounty.Properties.DataSource = ConnectBLL.BLL.Person.CountyList();
lkuResidenceCounty.Properties.PopulateColumns();
lkuResidenceCounty.Properties.DisplayMember = "CountyName";
lkuResidenceCounty.Properties.ValueMember = "CountyID";
lkuResidenceCounty.Properties.Columns[0].Visible = false;
lkuResidenceCounty.Properties.Columns[2].Visible = false;
lkuResidenceCounty.Properties.Columns[3].Visible = false;
This works just fine as the CountyName is displayed as expected.
However, When I try and load an existing member's value for this field using the below, which is part of a function that takes a row from the DataSet -->
lkuResidenceCounty.Properties.ValueMember = row["ResidencyCountyID"].ToString();
I get a blank box. I have stepped through the code and the correct ID is being returned for the member.
Unfortunately the stored procedure to fill the dropdown options pulls from a Maintenance Table with the columns "CountyName" & "CountyID". So that is correct. Unfortunately, the stored procedure to load a specific person's current county pulls from the Person Table where there is a column called "ResidencyCountyID". It is so named because there is also a "ResponsibilityCountyID" column.
I need a way for them both to coexist, any solutions?
Thanks!
DisplayMember and ValueMember are used to populate the control with the list of selectable values. To set the selected value of a populated LookUpEdit control, set its EditValue property:
lkuResidenceCounty.EditValue = row["ResidencyCountyID"].ToString();
In response to your edit: According to the documentation:
The currently selected row determines values for the editor's edit value and display text. The value for BaseEdit.EditValue is obtained from the RepositoryItemLookUpEditBase.ValueMember field, while the text to display in the edit box is obtained from the RepositoryItemLookUpEditBase.DisplayMember field of the selected row.
When you change BaseEdit.EditValue,
the editor locates and selects the row
whose
RepositoryItemLookUpEditBase.ValueMember
field contains the new value. The text
in the edit box is changed to reflect
the newly selected row.
I don't use these controls but it sounds to me that it shouldn't be working as you described. I think the ToString() is the problem because EditValue accepts an object so it's probably expecting an int for the value. Try:
lkuResidenceCounty.EditValue = (int)row["ResidencyCountyID"];
The ValueMember property tells the list what field to pull from when setting the Value property. Once you've set the ValueMember to "CountyID", then when the list is Databound, it will set all the list items' value properties to their respect objects' CountyID fields.
Hence, you should not do:
lkuResidenceCounty.Properties.ValueMember = row["ResidencyCountyID"].ToString();
but rather
lkuResidenceCounty.Properties.ValueMember = "CountyID";
was perfectly fine, as long as you've correctly identified the field you're trying to databind. Once the list get's databound you should see the results you're expecting.
However, after looking at your code, it seems that you're mismatching your field. In one place you're using ResidencyCountyID and in another you're using CountyID. That is most likely your source of confusion. Figure out what the actual field name is and make sure you set the ValueMember to that.
UPDATE
After reading your comment, what your looking for is the SelectedValue property. The SelectedValue property tells the list to force the selected value to whatever input you give it.
Essentially you have two things going on here. ValueMember tells the list what to use as the value from your data source, and SelectedValue which tells the list what the currently selected value should be.
Why is it that you need the same LookUpEdit to have two value members? Is it being used standalone or in a grid? If standalone, you could swap out the two repository editors depending on the current row. But, are there more than 2 possible values for the ValueMember? That would also complicate things.
UPDATE
Looking at your edit, I think I understand what's going on a little more. So, you don't wish to change your ValueMember (which refers to a data column), but rather to change the value of the editor? If so, then you should definitely use EditValue (not SelectedText, which I don't believe is meant to be set), and assign it to row["value_field"] like so:
lkuResidenceCounty.EditValue = row["ResidencyCountyID"];
What happens when you do that?

Categories

Resources