我希望填充每個可能的SeriesChartType
下拉框,以便我的用戶可以選擇適當的圖表類型。如何使用所有可能的SeriesChartType選項填充下拉框?
如何遍歷SeriesChartType
集合(位於命名空間System.Web.Ui.DataVisualization.Charting
中)並返回每個可能的選項,以便將其添加到下拉框中?
謝謝。
我希望填充每個可能的SeriesChartType
下拉框,以便我的用戶可以選擇適當的圖表類型。如何使用所有可能的SeriesChartType選項填充下拉框?
如何遍歷SeriesChartType
集合(位於命名空間System.Web.Ui.DataVisualization.Charting
中)並返回每個可能的選項,以便將其添加到下拉框中?
謝謝。
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
}
如果這不是你要找的,請告訴我。
你可以綁定在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);
}
這裏是一個泛型函數:
// ---- 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);
}
}
這在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代碼。
這是我最初嘗試的方法,我認爲這可以在C中工作,但TypeOf在VB中的工作方式不同,我接收到錯誤「System.Web.UI.DataVisualization.Charting」是一個名稱空間,不能用作表達式。「我發佈了在VB中爲我工作的答案,但在C變體中爲+1。 – 2011-03-25 16:33:02