2011-03-25 25 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代码。

相关问题