c# - gridview bind dropdownlist to List<keyvaluePair<int, string>> -


I have several tables in my DB that capture the reference of the key value pair:

type Phone Number:

  • 1 - Home
  • 2 - Work
  • 3 - Mobile
  • 4 - Fax

etc.

So I have a type of table and when they are used in other tables, they reference the int value as a foreign key when I I'm pulling them out so I can call them Lt; Int, string & gt; I am storing those sections that are using them.

When I needed to get a list, I thought that I make a list & lt;> instead of using two types of data types to get only one data.

My problem has come when I need to create a dropdown list within the gridview I am using the edittemplate bit if I use a data source to pull it out It will write [1 home] in the text, instead of writing the value to the value and displaying the home as text instead of writing it in.

I think that I have a lot more questions really in fact.

one:

Am I stupid? Is this an important way to obtain and store data (keyweeper part)? Should I just be storing it all in one figure? I did not like to put it all in figures. My DAL has taken me to BLL and objects or list & lt; & Gt; have tried to complete the objects. Most often it has worked well.

Two:

If I was using an object instead of a dataset to bind in my object data for the dropdown list, then instead of the currently selected value, the list What is the value instead of selecting the first item in?

Edit

As I was told below, I was stupid and just

  SelectedValue = '& lt ;% # DataBinder.Eval (Container, URL) "Dataite.photoType.key")% & gt; ' 

The reason for this was not to show that it was a straight one because it was not showing in my intelligence, but when I manually typed it did it Was there.

Use a dictionary & lt; Int, string & gt; Set your dropdown data field for key and value .

  // A sample dictionary: var dictionary = new dictionary & lt; Int, string & gt; (); dictionary. Add (1, "Home"); dictionary. Add (2, "Work"); dictionary. Add (3, "Mobile"); dictionary. Add (4, "Fax"); Dictionary dropdown in dropdown list: dropdown.datextfold = "value"; Dropdown.DataValueField = "key"; Dropdown.DataSource = Dictionary; // dictionary & lt; Int, string & gt; DropDown.DataBind ();  

Comments