Tuesday, 8 May 2012

Beginning .Net , C# Tips : Fill or Populate dropdownlist from Enum

There are a situations where you want to fill or populate dropdownlist of listbox using Enum.
Here are sample example for this.

C# Example :
public enum ProgrammingLanguage
{
    CSharp,
    VB,
    JAVA
}
foreach (ProgrammingLanguage enmLnaguage  in Enum.GetValues(typeof(ProgrammingLanguage)))
{
     cboProgrammingLanguage.Items.Add(new ListItem(enmLnaguage.ToString(), Convert.ToInt32( enmLnaguage).ToString()));
}

VB.net Example :
Public Enum ProgrammingLanguage
   CSharp
   VB
   JAVA
End Enum 
For Each enmLnaguage As ProgrammingLanguage In [Enum].GetValues(GetType(ProgrammingLanguage))
   cboProgrammingLanguage.Items.Add(New ListItem(enmLnaguage.ToString(), enmLnaguage))
Next

Click Here to view "Populating or Fill dropdownlist from Database table with C# Examples and VB.Net Examples". Click Here...

This type of C# Tips is very useful in day to day programming life.

Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.


2 comments:

  1. very nice . and short code. thanks.

    ReplyDelete
    Replies
    1. Hi,
      I added new article related to LINQ To XML in this blog so check it out.

      Delete