views:

735

answers:

3

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!

+1  A: 

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.

Joseph
The field name is different for the 2 scenario's. For the filling of the possible values is pulled from a County maintanence table and the filling in of the current member's info is from the Member Table.
Refracted Paladin
@Refracted Paladin Oh I think I understand now. I updated my answer, but essentially you want to use the SelectedValue property.
Joseph
hmm, except there is only a SelectedText property....ideas?
Refracted Paladin
@Refracted Paladin What kind of control is lkuResidenceCounty?
Joseph
lookupedit box from devexpress
Refracted Paladin
@Refracted Paladin Ah then you might want to try the SelectedText property and see if that works in the place of SelectedValue.
Joseph
I think it would if my sproc was returning the DisplayMember instead of the ValueMember. Sounds like I may have a problem unique to DevExpress, which I suspected, I had just hoped I could find help here vs there site as it is not the greatest support site around.
Refracted Paladin
+1  A: 

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?

Dov
+2  A: 

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"];
Jamie Ide
See EDIT above and I will elaborate.
Refracted Paladin