gridview bind dropdownlist to List>

2019-04-07 04:31发布

问题:

I have a number of tables in my DB that hold references to an key value pair:

types of phone numbers:

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

etc

So I have a table for the types and when they are used in other tables they reference the int value as a foreign key. When I have been pulling them out I have been storing them as keyvaluepair<int, string> items in the class that is using them.

When I needed to get a list of them I thought I would just create a List<> of them rather than use two different types of data types to get the same data.

My problem has arrived when I need to populate a dropdownlist within a gridview when I am using the edittemplate bit. If I use a datasource to pull this out it will write [1 Home] in the text rather than put the int as the value and Home as the text to display.

I guess I have a multiple part question really.

One:

Am I being stupid? Is this a really bad way to get the data out and store it (the keyvaluepair part)? Should I just be storing it all in a datatable? I didn't like to put it all in datatable. I have my DAL taking to my BLL and have tried to encapsulate everything as objects or List<>'s of objects rather than tables of everything. Most the time this has worked well.

Two:

If I was using some object rather than a datatable to bind into my objectdatasource for the dropdownlist, how can I set the currently selected value, rather than have it just have the first item in the list selected?

EDIT

As was pointed out below I was being an idiot and just needed to set the DataValueField and DataKeyField.

To get the dropdownlist to bind I just had to do:

SelectedValue='<%# DataBinder.Eval(Container, "DataItem.PhoneType.Key") %>'

The reason I didn't see that one straight away was because it was not showing up in my intellisense but when I manually typed it in, it worked.

回答1:

Use a Dictionary<int, string> and set your DropDown DataValueField to Key and DataTextField to Value.

    // A sample dictionary:
    var dictionary = new Dictionary<int, string>();
    dictionary.Add(1, "Home");
    dictionary.Add(2, "Work");
    dictionary.Add(3, "Mobile");
    dictionary.Add(4, "Fax");

    // Binding the dictionary to the DropDownList:
    dropDown.DataTextField = "Value";
    dropDown.DataValueField = "Key";
    dropDown.DataSource = dictionary;  //Dictionary<int, string>
    dropDown.DataBind();


回答2:

and its my custom method

// Define enum
public enum ServiceType : int
{
    MinimumService = 1,
    NormalService = 2,
    VipService = 99
}

// custom method to get my custom text name for each enum type
public string GetServiceTypeName(ServiceType serviceType)
{
    string retValue = "";
    switch (serviceType)
    {
        case ServiceType.Print:
            retValue = "We have some services for you";
            break;
        case ServiceType.BookBinding:
            retValue = "We ar glad to meet you";
            break;
        default:
            retValue = "We alywas are ready to make you happy";
            break;
    }
    return retValue;
}

// making dictionary (key and value) for dropdown datasource
public static Dictionary<string, int> GetAllServiceTypeName()
{
    Dictionary<string, int> dataSource = new Dictionary<string, int>();

    foreach (int item in Enum.GetValues(typeof(ServiceType)))
        dataSource.Add(GetServiceTypeName((ServiceType)item), item);

    return dataSource;
}


   // bind the dropdown to dictionary
    ddlServiceType.DataSource = GetAllServiceTypeName();
    ddlServiceType.DataBind();

   // aspx markup code sample
    <asp:DropDownList ID="ddlServiceType" runat="server" 
        DataTextField="Key" DataValueField="Value">
    </asp:DropDownList>