Skip to main content

how to implement option group in asp.net drop down list

In my previous article i have explained about Bind Specific Columns Of Datatable To Gridview In C#.Net, ASP.Net. In this tutorial i am explaining about how to implement option group in asp.net drop down list. In html select list option group is used to group the similiar items. But in asp.net dropdown list we don't have that option. But we can achieve this with the help of jQuery. Dropdownlist is a asp.net control used to show list of values from that the user can select one value.

Here for explanation purpose I have a datatable which contains employees from different department. And the drop down list will group the items based on the department. To use the datatable add the below namespace first.
using System.Data;
Below is the complete cs code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class OptionGroup : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //creating data table
        DataTable dtable = new DataTable();
        
        //adding columns to data table
        dtable.Columns.Add("EmpId", typeof(int));
        dtable.Columns.Add("EmployeeName", typeof(string));
        dtable.Columns.Add("Departmaent", typeof(string));
        
        //adding rowa to datatable
        dtable.Rows.Add(1, "Daniel", "Development");
        dtable.Rows.Add(1, "Nancy", "Development");
        dtable.Rows.Add(1, "Alen", "Development");

        dtable.Rows.Add(1, "Baskar", "Design");
        dtable.Rows.Add(1, "Saleem", "Design");
        dtable.Rows.Add(1, "Mohammed", "Design");

        dtable.Rows.Add(1, "Akbar", "Quality Control");
        dtable.Rows.Add(1, "Crystal", "Quality Control");
        dtable.Rows.Add(1, "Iren", "Quality Control");

        // adding datatable rows to drop down list 
        ListItem newItem;
        foreach(DataRow drow in dtable.Rows)
        {
            newItem = new ListItem(drow["EmployeeName"].ToString(), drow["EmpId"].ToString());
            newItem.Attributes["OptionGroup"] = drow["Departmaent"].ToString();

            ddlOptionGroup.Items.Add(newItem);
        }
    }
}

Equivalent vb.net code is given below
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data

Public Partial Class OptionGroup
 Inherits System.Web.UI.Page
 Protected Sub Page_Load(sender As Object, e As EventArgs)
  'creating data table
  Dim dtable As New DataTable()

  'adding columns to data table
  dtable.Columns.Add("EmpId", GetType(Integer))
  dtable.Columns.Add("EmployeeName", GetType(String))
  dtable.Columns.Add("Departmaent", GetType(String))

  'adding rowa to datatable
  dtable.Rows.Add(1, "Daniel", "Development")
  dtable.Rows.Add(1, "Nancy", "Development")
  dtable.Rows.Add(1, "Alen", "Development")

  dtable.Rows.Add(1, "Baskar", "Design")
  dtable.Rows.Add(1, "Saleem", "Design")
  dtable.Rows.Add(1, "Mohammed", "Design")

  dtable.Rows.Add(1, "Akbar", "Quality Control")
  dtable.Rows.Add(1, "Crystal", "Quality Control")
  dtable.Rows.Add(1, "Iren", "Quality Control")

  ' adding datatable rows to drop down list 
  Dim newItem As ListItem
  For Each drow As DataRow In dtable.Rows
   newItem = New ListItem(drow("EmployeeName").ToString(), drow("EmpId").ToString())
   newItem.Attributes("OptionGroup") = drow("Departmaent").ToString()

   ddlOptionGroup.Items.Add(newItem)
  Next
 End Sub
End Class
Refer the jquery library in your asp.net web page.
    

Once the drop down list is loaded by using the jQuery wrapAll function we are grouping the drop down list items using the department attribute of the each list item. wrapAll() function Wraps a HTML structure around all elements in the set of matched elements.. Below is the jQuery code used to group the drop down list items.


Below is the complete aspx code.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="OptionGroup.aspx.cs" Inherits="OptionGroup" %>




    Asp.Net Option Group Example 
    


    
    

Asp.Net Dropdown List Option Group Example


The final output will look like the below image.


Comments

Post a Comment

Popular posts from this blog

Code To Convert rupees(numbers) into words using C#.Net

Introduction: In my previous article I have explained about how to validate emailid using javascript . In this article I am going to explain about code used to convert rupees(numbers) into words using C#.Net . Explanation: For explanation purpose I have a page. It has a textbox to input the numbers. And when you click on the convert to words button then it will convert the input numbers into words and shows it in the below label. Below is the C# code used to do this functionality. public static string NumbersToWords( int inputNumber) {     int inputNo = inputNumber;     if (inputNo == 0)         return "Zero" ;     int [] numbers = new int [4];     int first = 0;     int u, h, t;     System.Text. StringBuilder sb = new System.Text. StringBuilder ();     if (inputNo < 0)     { ...

Puzzles for kids - 12 Days Of Christmas

According to the traditional song, on the first day of Christmas (25th December), my true love sent to me: . A partridge in a pair tree On the second day of Christmas (26th December), my true love sent to me THREE presents: . Two turtle doves . A partridge in a pear tree On the third day of Christmas (27th December and so on) my true love sent to me SIX presents: . Three French hens . Two turtle doves . A partridge in a pear tree This carries on until the the twelfth day of Christmas, when my true love sends me: Twelve drummers drumming Eleven pipers piping Ten lords a-leaping Nine ladies dancing Eight maids a-milking Seven swans a-swimming Six geese a-laying Five gold rings Four calling birds Three French hens Two turtle doves A partridge in a pear tree After the twelve days of Christmas are over, how many presents has my true love sent me altogether? Our Solution: 1 + 3 + 6 + 10 + 15 + 21 + 28 + 36 + 45 + 55 + 66 + 78 = 364 presents Which is really interesting when you think ...

Asp.Net DataTable Manipulation - Add, Update,Delete & Sort DataTable in C#.Net & VB.Net

In the asp.net developer's life it is very common to come across the datatable manipulation. So here i have decided to explain the datatable manipulation like adding records to datatable, editing and updating datatable records,deleting records from datatable based on the condition and finally sorting datatable based on columns.