2011-03-25 22 views

回答

1
foreach (ChartType in Enum.GetValues(typeof(System.Web.UI.DataVisualization.Charting)) 
{ 
    //Add an option the the dropdown menu 
    // Convert.ToString(ChartType) <- Text of Item 
    // Convert.ToInt32(ChartType) <- Value of Item 
} 

如果這不是你要找的,請告訴我。

+0

這是我最初嘗試的方法,我認爲這可以在C中工作,但TypeOf在VB中的工作方式不同,我接收到錯誤「System.Web.UI.DataVisualization.Charting」是一個名稱空間,不能用作表達式。「我發佈了在VB中爲我工作的答案,但在C變體中爲+1。 – 2011-03-25 16:33:02

1

你可以綁定在DataBind事件處理數據:

public override void DataBind() 
{ 
    ddlChartType.DataSource = 
     Enum.GetValues(typeof(SeriesChartType)) 
      .Cast<SeriesChartType>() 
      .Select(i => new ListItem(i.ToString(), i.ToString())); 
    ddlChartType.DataBind(); 
} 

,然後檢索在SelectedIndexChanged事件處理程序是這樣選擇的值:

protected void ddlChartType_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    // holds the selected value 
    SeriesChartType selectedValue = 
     (SeriesChartType)Enum.Parse(typeof(SeriesChartType), 
            ((DropDownList)sender).SelectedValue); 
} 
0

這裏是一個泛型函數:

// ---- EnumToListBox ------------------------------------ 
// 
// Fills List controls (ListBox, DropDownList) with the text 
// and value of enums 
// 
// Usage: EnumToListBox(typeof(MyEnum), ListBox1); 

static public void EnumToListBox(Type EnumType, ListControl TheListBox) 
{ 
    Array Values = System.Enum.GetValues(EnumType); 

    foreach (int Value in Values) 
    { 
     string Display = Enum.GetName(EnumType, Value); 
     ListItem Item = new ListItem(Display, Value.ToString()); 
     TheListBox.Items.Add(Item); 
    } 
} 
1

這在VB中適用於我 - 我必須實例化例如SeriesChartType,它允許我使用[Enum].GetNames方法。

當時我能夠將它們添加到下拉框,如圖所示:

Dim z As New SeriesChartType 
For Each charttype As String In [Enum].GetNames(z.GetType) 
    Dim itm As New ListItem 
    itm.Text = charttype 
    ddl_ChartType.Items.Add(itm) 
Next 

感謝大家對你的答案。 mrK有一個偉大的C替代這個VB代碼。

相關問題