Question

Is there an way of storing both the ID and Test ID values in the Dropdown list but I need to display the DisplayName? I need both of these values when the item is selected

ID | TestID | DisplayName
 1 |   2    |  Test

Sample Code:

ddList.DataSource = DvName;
ddList.DataTextField = "DisplayName";
ddList.DataValueField = "ID";   // + "TestID" ???
Était-ce utile?

La solution

You could use a delimiter to put both values together and then delimit them once you pull them out.

ddList.DataValueField = obj.ID + "~" + obj.TestID;

Otherwise you could create a dictionary that hold the ID and the Test ID, so that when you select your item from the dropdown, you can use the value field as the key to the dictionary and select the TestID.

private Dictionary<string, string> referenceValues = new Dictionary<string, string>();
referenceValues.Add("ID", "TestID");
ddList.DataValueField = "ID";
//When pulling then value back
var referencedItem = referenceValues[ddList.selectedValue];

Autres conseils

Assuming you're getting this values from a model you could do something like:

public string NewValue
{
    get
    {
         return ID+ "," + TestId;
    }
}

ddList.DataSource = DvName;
ddList.DataTextField = "DisplayName";
ddList.DataValueField = "NewValue";

Assuming that you are using a class for each instance of your data; add another property to the class and bind to that property:

public class MyObject
{
  //other stuff
  public string DisplayValue
  {
     get { return String.Format("{0} ({1})", ID, TestID); }
  }
}

ddList.DataSource = DvName;
ddList.DataTextField = "DisplayName";
ddList.DataValueField = "DisplayValue";
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top