3

I wish to populate a drop down box with each possible SeriesChartType so that my users may choose an appropriate chart type.

How can I iterate through the SeriesChartType collection (it's in the namespace System.Web.Ui.DataVisualization.Charting) and return each possible option so I can add it to the drop down box?

Thanks.

Oleks
  • 31,955
  • 11
  • 77
  • 132
Andy F
  • 1,517
  • 2
  • 20
  • 35

4 Answers4

2

This worked for me in VB - I had to instantiate a new instance of the SeriesChartType which allowed me to use the [Enum].GetNames Method.

I was then able to add them to the drop down box as shown:

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

Thanks to everyone for your answers. mrK has a great C alternative to this VB code.

Andy F
  • 1,517
  • 2
  • 20
  • 35
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
}

If this isn't what you're looking for, let me know.

mrK
  • 2,208
  • 4
  • 32
  • 46
  • This was the approach I tried originally, and I think this works in C, but TypeOf works differently in VB and I receieved the error `System.Web.UI.DataVisualization.Charting' is a namespace and cannot be used as an expression.` I have posted the answer that worked for me in VB, but +1 for the C variant. – Andy F Mar 25 '11 at 16:33
1

You could bind data in the DataBind event handler:

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

and then retrieve the selected value in the SelectedIndexChanged event handler like this:

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

Here's a generic function:

// ---- 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);
    }
}
Steve Wellens
  • 20,506
  • 2
  • 28
  • 69